1 | <?php |
||
16 | final class TurnOnAutowireCompilerPass implements CompilerPassInterface |
||
17 | { |
||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | 2 | public function process(ContainerBuilder $containerBuilder) |
|
29 | |||
30 | /** |
||
31 | * @param string $name |
||
32 | * @param Definition $definition |
||
33 | * |
||
34 | * @return bool |
||
35 | */ |
||
36 | 2 | private function shouldDefinitionBeAutowired($name, Definition $definition) |
|
|
|||
37 | { |
||
38 | 2 | if (!$this->isDefinitionValid($definition)) { |
|
39 | 1 | return false; |
|
40 | } |
||
41 | |||
42 | 2 | $classReflection = new ReflectionClass($definition->getClass()); |
|
43 | 2 | if (!$classReflection->hasMethod('__construct')) { |
|
44 | 1 | return false; |
|
45 | } |
||
46 | |||
47 | 2 | if (!$this->hasConstructorArguments($classReflection)) { |
|
48 | return false; |
||
49 | } |
||
50 | |||
51 | 2 | if ($this->areAllConstructorArgumentsRequired($definition, $classReflection)) { |
|
52 | 1 | return false; |
|
53 | } |
||
54 | |||
55 | 2 | $constructorReflection = $classReflection->getConstructor(); |
|
56 | 2 | if (!$this->haveMissingArgumentsTypehints($definition, $constructorReflection)) { |
|
57 | return false; |
||
58 | } |
||
59 | |||
60 | 2 | return true; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | 2 | private function isDefinitionValid(Definition $definition) |
|
67 | { |
||
68 | 2 | if (null === $definition->getClass()) { |
|
69 | 1 | return false; |
|
70 | } |
||
71 | |||
72 | 2 | if (!$definition->isPublic()) { |
|
73 | 1 | return false; |
|
74 | } |
||
75 | |||
76 | 2 | if ($definition->isAbstract()) { |
|
77 | 1 | return false; |
|
78 | } |
||
79 | |||
80 | 2 | if (!class_exists($definition->getClass())) { |
|
81 | 1 | return false; |
|
82 | } |
||
83 | |||
84 | 2 | return true; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return bool |
||
89 | */ |
||
90 | 2 | private function areAllConstructorArgumentsRequired(Definition $definition, ReflectionClass $classReflection) |
|
91 | { |
||
92 | 2 | $constructorMethodReflection = $classReflection->getConstructor(); |
|
93 | |||
94 | 2 | $constructorArgumentsCount = count($definition->getArguments()); |
|
95 | 2 | $constructorRequiredArgumentsCount = $constructorMethodReflection->getNumberOfRequiredParameters(); |
|
96 | |||
97 | 2 | if ($constructorArgumentsCount === $constructorRequiredArgumentsCount) { |
|
98 | 1 | return true; |
|
99 | } |
||
100 | |||
101 | 2 | return false; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return bool |
||
106 | */ |
||
107 | 2 | private function hasConstructorArguments(ReflectionClass $classReflection) |
|
116 | |||
117 | /** |
||
118 | * @return bool |
||
119 | */ |
||
120 | 2 | private function haveMissingArgumentsTypehints(Definition $definition, ReflectionMethod $constructorReflection) |
|
137 | } |
||
138 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.