0

A colleague is cloning a repo and getting the following error when running bake commands through our own plugin:

PHP Fatal error:  Declaration of OurPlugin\Command\ModelCommand::findBelongsTo(Cake\ORM\Table $model, array $associations): array must be compatible with Bake\Command\ModelCommand::findBelongsTo(Cake\ORM\Table $model, array $associations, ?Cake\Console\Arguments $args = null): array in C:\Users\colleague_name_here\Downloads\xampp_php81\htdocs\OurApp\plugins\OurPlugin\src\Command\ModelCommand.php on line 334

I don't get that issue with my existing local copy. I was able to replicate cloning and duplicating to another folder. Where is that vendor change coming from?

In composer.json:

"cakephp/bake": "^2.3",

vendor file, where the function definition seems to have changed

public function findBelongsTo(Table $model, array $associations, ?Arguments $args = null): array

Current plugin:

public function findBelongsTo(Table $model, array $associations): array

In GitHub, https://github.com/cakephp/bake/blob/2.3.0/src/Command/ModelCommand.php

public function findBelongsTo(Table $model, array $associations): array
TechFanDan
  • 3,329
  • 6
  • 46
  • 89
  • This arg was introduced _after_ 2.3.0. Note that `^2.3.0` doesn't mean "exactly 2.3.0", so you're likely getting a more recent version. Also, it's possible your colleague is using PHP 8.0 or later, while you're on 7.4 or earlier. At 8.0, non-matching signatures went from warning to fatal. – Alex Howansky May 09 '23 at 16:32
  • @alexHowansky we're all on PHP 8.1.10. I've been developing this app for close to 2 years, have had a few developers come and go without this issue arising. I'm deploying to Azure, and my latest deployment done Monday, isn't featuring this issue. Very strange. – TechFanDan May 09 '23 at 17:08
  • In order to get this message, you need to be on PHP 8+ and cakephp/bake 2.9.0+. Perhaps you have a `composer.lock` that's locked to 2.8.0 or earlier and that's what everybody else is using, and that's what you deployed, so everything worked fine -- but your colleague just ran a `composer update` which brought them to the offending version 2.9.3. – Alex Howansky May 09 '23 at 17:14
  • The lock file indicates CakePHP 4.3.10, Bake 2.8.2. If I hard code CakePHP 4.3.10 and Bake 2.3.0, I see: - Removing brick/varexporter (0.3.7), - Downgrading cakephp/bake (2.8.2 => 2.3.0). I get a different function definition issue. – TechFanDan May 09 '23 at 17:27
  • [2.3.0](https://github.com/cakephp/bake/blob/2.3.0/src/Command/ModelCommand.php) and [2.8.2](https://github.com/cakephp/bake/blob/2.8.2/src/Command/ModelCommand.php) both have `(Table $model, array $associations)` as the definition. The extra arg comes from [2.9.3](https://github.com/cakephp/bake/blob/2.9.3/src/Command/ModelCommand.php). So if you do `composer install` you'll get 2.8.2 and you'll be fine, but if you do `composer update` you'll get 2.9.3 and you'll break. – Alex Howansky May 09 '23 at 17:35
  • Until the code is adjusted, you might update your `composer.json` to specifically lock at the last working version `"cakephp/bake": "2.8.3",` and then even a `composer update` won't go past that. – Alex Howansky May 09 '23 at 17:45
  • @alexhowansky That worked! If you provide an answer, I can quickly mark it as such. – TechFanDan May 09 '23 at 18:21

1 Answers1

1

This third argument was introduced in bake 2.9.0. Perhaps you have a composer.lock that's locked to 2.8.0 or earlier and that's what everybody else is using, and that's what you deployed, so everything worked fine when you do composer install -- but your colleague just ran a composer update which brought them to the offending version 2.9.3.

Until the code is adjusted, you're probably best off just updating your composer.json to specifically lock at exactly the last known working version, and then even a composer update won't go past that:

"cakephp/bake": "2.8.3"

Once you get the code updated appropriately, change this to require 2.9.3 at a minimum and do composer update again:

"cakephp/bake": "^2.9.3"
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98