These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping; |
||
5 | |||
6 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
7 | use Symfony\Component\DependencyInjection\Definition; |
||
8 | use ReflectionClass; |
||
9 | use function method_exists; |
||
10 | |||
11 | /** |
||
12 | * Routes commands based on typehints in the handler. |
||
13 | * |
||
14 | * If your handler has a public method with a single, non-scalar, no-interface type hinted |
||
15 | * parameter, we'll assume that typehint is a command and route it to this |
||
16 | * service definition as the handler. |
||
17 | * |
||
18 | * So, a class like this: |
||
19 | * |
||
20 | * class MyHandler |
||
21 | * { |
||
22 | * public function handle(RegisterUser $command) {...} |
||
23 | * private function foobar(SomeObject $obj) {...} |
||
24 | * public function checkThings(OtherObject $obj, WhatObject $obj2) |
||
25 | * public function setADependency(ManagerInterface $interface) {...} |
||
26 | * } |
||
27 | * |
||
28 | * would have RegisterUser routed to it, but not SomeObject (because it's |
||
29 | * used in a private method), not OtherObject or WhatObject (because they |
||
30 | * don't appear as the only parameter) and not setADependency (because it |
||
31 | * has an interface type hinted parameter). |
||
32 | */ |
||
33 | final class TypeHintMapping extends TagBasedMapping |
||
34 | { |
||
35 | 78 | protected function isSupported(ContainerBuilder $container, Definition $definition, array $tagAttributes): bool |
|
36 | { |
||
37 | 78 | return isset($tagAttributes['typehints']) && $tagAttributes['typehints'] === true; |
|
38 | } |
||
39 | |||
40 | 51 | protected function findCommandsForService(ContainerBuilder $container, Definition $definition, array $tagAttributes): array |
|
41 | { |
||
42 | 51 | $results = []; |
|
43 | |||
44 | 51 | $reflClass = new ReflectionClass($container->getParameterBag()->resolveValue($definition->getClass())); |
|
45 | |||
46 | 51 | foreach ($reflClass->getMethods() as $method) { |
|
47 | |||
48 | 51 | if (!$method->isPublic() |
|
49 | 48 | || $method->isConstructor() |
|
50 | 45 | || $method->isStatic() |
|
51 | 42 | || $method->isAbstract() |
|
52 | 39 | || $method->isVariadic() |
|
53 | 51 | || $method->getNumberOfParameters() !== 1 |
|
54 | ) { |
||
55 | 27 | continue; |
|
56 | } |
||
57 | |||
58 | 36 | $parameter = $method->getParameters()[0]; |
|
59 | 36 | if (!$parameter->hasType() |
|
60 | 33 | || $parameter->getType() instanceof \ReflectionUnionType |
|
0 ignored issues
–
show
|
|||
61 | 36 | || $parameter->getType()->isBuiltin() |
|
62 | || (new ReflectionClass($parameter->getType()->getName()))->isInterface() |
||
63 | 9 | ) { |
|
64 | continue; |
||
65 | } |
||
66 | 27 | ||
67 | 27 | $type = $parameter->getType(); |
|
68 | 18 | if (version_compare(PHP_VERSION, '7.1.0') >= 0) { |
|
69 | $results[] = $type->getName(); |
||
70 | 18 | } else { |
|
71 | $results[] = (string)$type; |
||
72 | } |
||
73 | |||
74 | } |
||
75 | 51 | ||
76 | return $results; |
||
77 | } |
||
78 | } |
||
79 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.