Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); | ||
| 14 | class Builder implements BuilderInterface | ||
| 15 | { | ||
| 16 | /** | ||
| 17 | * @var ContainerInterface | ||
| 18 | */ | ||
| 19 | protected $container; | ||
| 20 | |||
| 21 | /** | ||
| 22 | * @var DependenciesResolver | ||
| 23 | */ | ||
| 24 | protected $dependenciesResolver; | ||
| 25 | |||
| 26 | protected $autoWire = true; | ||
| 27 | |||
| 28 | 61 | public function __construct(bool $autoWire = true, ?DependenciesResolverInterface $dependenciesResolver = null) | |
| 33 | |||
| 34 | /** | ||
| 35 |      * {@inheritDoc} | ||
| 36 | */ | ||
| 37 | 48 | public function setContainer(ContainerInterface $container): void | |
| 41 | |||
| 42 | /** | ||
| 43 |      * {@inheritDoc} | ||
| 44 | */ | ||
| 45 | 39 | public function construct(DefinitionInterface $definition): object | |
| 54 | |||
| 55 | /** | ||
| 56 |      * {@inheritDoc} | ||
| 57 | */ | ||
| 58 | 10 | public function configure(object $object, DefinitionInterface $definition): object | |
| 64 | |||
| 65 | /** | ||
| 66 |      * {@inheritDoc} | ||
| 67 | */ | ||
| 68 | 1 | public function invoke(callable $callable, array $arguments = []) | |
| 72 | |||
| 73 | /** | ||
| 74 |      * {@inheritDoc} | ||
| 75 | */ | ||
| 76 | 2 | public function inflect(object $object, InflectionInterface $inflection): object | |
| 82 | |||
| 83 | /** | ||
| 84 | * @param object $object | ||
| 85 | * @param PropertyInterface[] $properties | ||
| 86 | */ | ||
| 87 | 12 | protected function applyProperties(object $object, array $properties): void | |
| 93 | |||
| 94 | /** | ||
| 95 | * @param object $object | ||
| 96 | * @param CallInterface[] $calls | ||
| 97 | * @throws NotFoundException | ||
| 98 | */ | ||
| 99 | 12 | protected function applyCalls(object $object, array $calls): void | |
| 105 | |||
| 106 | /** | ||
| 107 | * @param callable $callable | ||
| 108 | * @param array $arguments | ||
| 109 | * @return mixed | ||
| 110 | * @throws NotFoundException | ||
| 111 | */ | ||
| 112 | 5 | protected function call(callable $callable, array $arguments = []) | |
| 122 | |||
| 123 | /** | ||
| 124 | * Make object without factory | ||
| 125 | * | ||
| 126 | * @param DefinitionInterface $definition | ||
| 127 | * @return mixed | ||
| 128 | * @throws NotFoundException | ||
| 129 | */ | ||
| 130 | 27 | protected function makeObjectSelf(DefinitionInterface $definition) | |
| 146 | |||
| 147 | /** | ||
| 148 | * Make object with factory | ||
| 149 | * | ||
| 150 | * @param DefinitionInterface $definition | ||
| 151 | * @return mixed | ||
| 152 | * @throws InvalidConfigurationException | ||
| 153 | * @throws InvalidFactoryException | ||
| 154 | * @throws NotFoundException | ||
| 155 | */ | ||
| 156 | 12 | protected function makeObjectWithFactory(DefinitionInterface $definition) | |
| 172 | |||
| 173 | /** | ||
| 174 | * Build callable factory from non-callable | ||
| 175 | * | ||
| 176 | * @param DefinitionInterface $definition | ||
| 177 | * @return callable | ||
| 178 | * @throws InvalidConfigurationException | ||
| 179 | * @throws InvalidFactoryException | ||
| 180 | */ | ||
| 181 | 9 | protected function buildFactoryFromNonCallable(DefinitionInterface $definition): callable | |
| 204 | |||
| 205 | /** | ||
| 206 | * Create object with provided arguments and optional construct method | ||
| 207 | * | ||
| 208 | * @param string $className | ||
| 209 | * @param array $arguments | ||
| 210 | * @param string|null $constructMethod | ||
| 211 | * @return mixed | ||
| 212 | */ | ||
| 213 | 24 | protected function constructObject(string $className, array $arguments, ?string $constructMethod = null) | |
| 222 | |||
| 223 | /** | ||
| 224 | * Build function attributes for type-value representation | ||
| 225 | * | ||
| 226 | * @param array $attributes | ||
| 227 | * @return Parameter[] | ||
| 228 | */ | ||
| 229 | 41 | protected function buildParameters($attributes = []): array | |
| 237 | |||
| 238 | /** | ||
| 239 | * Create parameter from provided argument | ||
| 240 | * | ||
| 241 | * @param $value | ||
| 242 | * @return ParameterInterface | ||
| 243 | */ | ||
| 244 | 18 | protected function buildParameter($value): ParameterInterface | |
| 261 | |||
| 262 | /** | ||
| 263 | * Try fetch dependency name from string value | ||
| 264 | * | ||
| 265 | * @param string $value | ||
| 266 | * @return string|null | ||
| 267 | */ | ||
| 268 | 7 | protected function fetchDependencyId(string $value): ?string | |
| 275 | |||
| 276 | /** | ||
| 277 | * Build arguments by dependencies and parameters | ||
| 278 | * | ||
| 279 | * @param DependencyInterface[] $dependencies | ||
| 280 | * @param ParameterInterface[] $parameters | ||
| 281 | * @return array | ||
| 282 | * @throws NotFoundException | ||
| 283 | */ | ||
| 284 | 41 | protected function buildArguments(array $dependencies, array $parameters): array | |
| 296 | |||
| 297 | /** | ||
| 298 | * @param DependencyInterface[] $dependencies | ||
| 299 | * @param ParameterInterface[] $parameters | ||
| 300 | * @return array | ||
| 301 | * @throws NotFoundException | ||
| 302 | */ | ||
| 303 | 33 | protected function buildArgumentsFromDependencies(array $dependencies, array $parameters): array | |
| 328 | |||
| 329 | /** | ||
| 330 | * @param array $parameters | ||
| 331 | * @param array $usedParameters | ||
| 332 | * @param array $arguments | ||
| 333 | * @return array | ||
| 334 | * @throws NotFoundException | ||
| 335 | */ | ||
| 336 | 30 | protected function appendUnusedParamsToArguments(array $parameters, array $usedParameters, array $arguments): array | |
| 345 | |||
| 346 | /** | ||
| 347 | * @param ParameterInterface $parameter | ||
| 348 | * @return mixed | ||
| 349 | * @throws NotFoundException | ||
| 350 | */ | ||
| 351 | 18 | protected function makeArgumentByParameter(ParameterInterface $parameter) | |
| 370 | |||
| 371 | /** | ||
| 372 | * @param DependencyInterface $dependency | ||
| 373 | * @return mixed | ||
| 374 | * @throws NotFoundException | ||
| 375 | */ | ||
| 376 | 25 | protected function makeArgumentByDependency(DependencyInterface $dependency) | |
| 395 | |||
| 396 | /** | ||
| 397 | * Fetch dependency from container | ||
| 398 | * | ||
| 399 | * @param $value | ||
| 400 | * @return mixed|null | ||
| 401 | */ | ||
| 402 | 27 | protected function retrieveDependencyFromContainer($value) | |
| 409 | } | ||
| 410 | 
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.