Skip to content

fix(console): inject the key factory into the generator commands through a setter - #812

Merged
Spomky merged 1 commit into
4.3.xfrom
fix/generator-command-factory-setter
Nov 9, 2026
Merged

Spomky merged 1 commit into
4.3.xfrom
fix/generator-command-factory-setter

Conversation

@Spomky

@Spomky Spomky commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #712, which was merged without its last revision.

What 4.3.x has

#712 gave GeneratorCommand a constructor, __construct(?string $name = null, ?JWKFactoryInterface $jwkFactory = null), and a protected readonly JWKFactoryInterface $jwkFactory property. In 4.2 the class declares neither, and both reach the commands an application extends:

  • a subclass that declares its own non-readonly $jwkFactory property, private or not, typed or not, fails to load: Cannot redeclare readonly property Jose\Component\Console\GeneratorCommand::$jwkFactory as non-readonly AppCommand::$jwkFactory;
  • the second constructor argument takes the place of $code, which recent versions of the Symfony command accept for an invokable command: passing one raises a TypeError.

What this changes

  • The factory is held in a private property. setJwkFactory() sets it, and getJwkFactory() reads it, falling back to a default JWKFactory so that the commands keep working outside of a container.
  • The constructor is the one inherited from the Symfony command again.
  • The bundle calls the setter on every generator command it registers (instanceof(GeneratorCommand::class)->call('setJwkFactory', …) in commands.php), the ML-DSA commands included.

BC check

I compared the public and protected API of Jose\Component\Console with 4.2.7 by reflection. There are two differences: the two methods added to GeneratorCommand, and the new ML-DSA commands. The constructors are those of 4.2.7.

The probes below are subclasses written the way an application would write them. Each runs in its own process (PHP 8.4, symfony/console 8.1), because a class that fails to load is a fatal error:

Subclass or usage 4.2.7 4.3.x this PR
declares private $jwkFactory OK fatal error OK
declares protected $jwkFactory OK fatal error OK
declares protected ?JWKFactory $jwkFactory = null OK fatal error OK
declares public $jwkFactory OK fatal error OK
has its own constructor with a dependency and calls parent::__construct('app:name') OK OK OK
passes an invokable command as the second constructor argument OK TypeError OK
new EcKeyGeneratorCommand(), run outside of a container OK OK OK
new EcKeyGeneratorCommand('my:name') OK OK OK
declares public function getJwkFactory(): JWKFactory OK OK OK
declares public function setJwkFactory(string $factory): void OK OK fatal error
declares private function getJwkFactory(): JWKFactory OK OK fatal error
reads $this->jwkFactory Error OK Error

The last three rows show what remains:

  • a subclass that already declares a method of the same name with an incompatible signature fails to load, as with any method added in a minor version;
  • the protected property only ever existed on the development branch.

Tests

  • GeneratorCommandBcTest (new): 4 of its 5 tests fail on 4.3.x, because the constructor is declared by GeneratorCommand, the class declares a protected property, and the setter and the getter do not exist. The fifth test, which checks that the name given to the constructor is kept, passes on both. A stub factory checks that the command builds its key with the injected factory.
  • JWKFactoryServiceTest (bundle): checks that the generator command from the container holds the container's factory. It also passes on 4.3.x, where the constructor was autowired.
  • Full suite (PHPUnit 11.5, PHP 8.4): 2834 tests, OK. ECS, Rector, PHPStan, Deptrac, the license check, the lint and composer validate pass with cold caches.
  • Infection on the lines of the diff, run the way the CI runs it: 8 mutants, all killed.

Open point

A subclass of GeneratorCommand that the application registers itself no longer receives the container's factory:

  • the instanceof of commands.php applies to the services of that file only, so such a command falls back to a default JWKFactory, as every command did in 4.2;
  • on 4.3.x, the autowired constructor gave it the container's factory;
  • registering the setter call for autoconfiguration, with registerForAutoconfiguration(GeneratorCommand::class)->addMethodCall('setJwkFactory', …), would cover both kinds of command. I checked that on a ContainerBuilder, but this PR does not do it.

5.0.x will get this change through the merge-up.

…ugh a setter

#712 gave GeneratorCommand a constructor taking the key factory as its
second argument, and kept the factory in a protected readonly property.
4.2 has neither, and both reach the commands an application extends:

- a subclass that declares its own non-readonly $jwkFactory property,
  private or not, typed or not, no longer loads: PHP refuses to redeclare
  a readonly property;
- the second argument of the constructor takes the place of the one that
  recent versions of the Symfony command accept for an invokable command
  ($code): passing one raises a TypeError.

The factory is now held in a private property, set by setJwkFactory()
and read through getJwkFactory(), which falls back to a default factory
so that the commands stay usable outside of a container. The constructor
is the one inherited from the Symfony command again. The bundle calls the
setter on every generator command it registers, the ML-DSA ones included.

Follow-up to #712
@Spomky Spomky added this to the 4.3.0 milestone Nov 9, 2026
@Spomky Spomky added the bug label Nov 9, 2026
@Spomky
Spomky merged commit 78b56db into 4.3.x Nov 9, 2026
18 checks passed
@Spomky
Spomky deleted the fix/generator-command-factory-setter branch November 9, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant