Assign AllCommand subcommands in the constructor (fix uninitialized typed property on CakePHP 5.4)#1091
Open
dereuromark wants to merge 2 commits into
Open
Assign AllCommand subcommands in the constructor (fix uninitialized typed property on CakePHP 5.4)#1091dereuromark wants to merge 2 commits into
dereuromark wants to merge 2 commits into
Conversation
ControllerAllCommand, ModelAllCommand and TemplateAllCommand declared their
wrapped subcommand as a non-nullable typed property and assigned it in
initialize(). On CakePHP 5.4 BaseCommand::run() builds the option parser
before calling initialize(), so buildOptionParser() reads the property while
it is still uninitialized:
Typed property Bake\Command\ControllerAllCommand::$controllerCommand
must not be accessed before initialization
Move the assignment into the constructor so the subcommand is available
regardless of the lifecycle ordering. The override of initialize() is dropped;
the inherited BakeCommand::initialize() (table-locator fallback) still runs.
TemplateAllCommand never read the property in buildOptionParser(), so its
breakage was latent; the change keeps the three commands consistent.
The constructor approach broke on cakephp/cakephp 5.1, where BaseCommand has no constructor yet (Error: Cannot call constructor via parent::__construct()). Bake supports ^5.1. Assign the wrapped subcommand lazily in buildOptionParser() with ??= instead. buildOptionParser() always runs before execute() on every supported version, and on 5.4 it runs before initialize() too, so the property is always set before use regardless of lifecycle ordering. ??= is safe on an uninitialized typed property and idempotent across repeated parser builds.
ADmad
approved these changes
Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes Cluster B of cakephp/cakephp#19491.
quite a heavy change though that most likely still leaves other third party commands affected.
Problem
ControllerAllCommand,ModelAllCommandandTemplateAllCommanddeclare their wrapped subcommand as a non-nullable typed property and assign it ininitialize():On CakePHP 5.4,
BaseCommand::run()builds the option parser before callinginitialize().buildOptionParser()reads the subcommand, which is still uninitialized at that point:This surfaced as errors in
ControllerAllCommandTest::testExecuteandModelAllCommandTest::testExecute.TemplateAllCommandnever reads its subcommand inbuildOptionParser(), so its breakage was latent.Fix
Assign the subcommand in the constructor, so it is available regardless of the run/initialize ordering:
The
initialize()override is dropped; the inheritedBakeCommand::initialize()(table-locator fallback) still runs. All three commands are kept consistent.Tests
Added a
testGetOptionParserBeforeInitializeto each command test that callsgetOptionParser()directly on a freshly constructed command - the exact pre-initialize()path the runner takes. These reproduce the reported error on the old code and pass with the fix, independent of the installed CakePHP version.