Complex classes like CompilerPass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CompilerPass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class CompilerPass implements CompilerPassInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $injectables; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $filter; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $exclude; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 2 | public function process(ContainerBuilder $container) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Find all services which should be injected with services via traits. |
||
| 79 | * |
||
| 80 | * @param ContainerBuilder $container |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 2 | private function findInjectableServices(ContainerBuilder $container) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Check if service definition should be injected with service via traits. |
||
| 103 | * |
||
| 104 | * @param string $serviceId |
||
| 105 | * @param Definition $definition |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 2 | private function isInjectable($serviceId, Definition $definition) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Check if service definition should be excluded from service injection via traits. |
||
| 135 | * |
||
| 136 | * @param string $serviceId |
||
| 137 | * @param Definition $definition |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 2 | private function isExcluded($serviceId, Definition $definition) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Process service injections via traits. |
||
| 175 | * |
||
| 176 | * @param Definition $definition |
||
| 177 | */ |
||
| 178 | 2 | private function processInjections(Definition $definition) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Process service injection parameters. |
||
| 194 | * |
||
| 195 | * @param array $arguments |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | 2 | private function processArguments(array $arguments) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Process service as argument. |
||
| 211 | * |
||
| 212 | * @param array $argument |
||
| 213 | * @return Reference |
||
| 214 | */ |
||
| 215 | 2 | private function processServiceAsArgument(array $argument) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Process expression as argument. |
||
| 232 | * |
||
| 233 | * @param array $argument |
||
| 234 | * @return Expression |
||
| 235 | */ |
||
| 236 | 2 | private function processExpressionAsArgument(array $argument) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Process string as argument |
||
| 243 | * |
||
| 244 | * @param array $argument |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | 2 | private function processStringAsArgument(array $argument) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Process constant as argument. |
||
| 254 | * |
||
| 255 | * @param array $argument |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | 2 | private function processConstantAsArgument(array $argument) |
|
| 262 | } |
||
| 263 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..