Skip to content

Commit

Permalink
bug #58618 [DependencyInjection] Fix linting factories implemented vi…
Browse files Browse the repository at this point in the history
…a __callStatic (KevinVanSonsbeek)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[DependencyInjection] Fix linting factories implemented via __callStatic

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #58614
| License       | MIT

This PR is an extension of an old fix made back in symfony/symfony#44710.
Factory calls to static methods handled by the `__callStatic` method currently throw the "Invalid service DYZ: method Class::method() does not exist".

Commits
-------

e2f69af6bd3 [DependencyInjection] Fix linting factories implemented via __callStatic
  • Loading branch information
nicolas-grekas committed Oct 22, 2024
2 parents 68110b9 + 711fca5 commit 0c199da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Compiler/AbstractRecursivePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ protected function getReflectionMethod(Definition $definition, string $method)
return new \ReflectionMethod(static function (...$arguments) {}, '__invoke');
}

if ($r->hasMethod('__callStatic') && ($r = $r->getMethod('__callStatic')) && $r->isPublic()) {
return new \ReflectionMethod(static function (...$arguments) {}, '__invoke');
}

throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/Compiler/CheckTypeDeclarationsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,17 @@ public function testCallableClass()
$this->addToAssertionCount(1);
}

public function testStaticCallableClass()
{
$container = new ContainerBuilder();
$container->register('foo', StaticCallableClass::class)
->setFactory([StaticCallableClass::class, 'staticMethodCall']);

(new CheckTypeDeclarationsPass())->process($container);

$this->addToAssertionCount(1);
}

public function testIgnoreDefinitionFactoryArgument()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -1050,3 +1061,10 @@ public function __call($name, $arguments)
{
}
}

class StaticCallableClass
{
public static function __callStatic($name, $arguments)
{
}
}

0 comments on commit 0c199da

Please sign in to comment.