Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php declare(strict_types=1); |
||
| 8 | class ArgumentsBuilder |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var ContainerInterface |
||
| 12 | */ |
||
| 13 | protected $container; |
||
| 14 | |||
| 15 | 48 | public function setContainer(ContainerInterface $container): void |
|
| 19 | |||
| 20 | /** |
||
| 21 | * Build arguments by dependencies and parameters |
||
| 22 | * |
||
| 23 | * @param DependencyInterface[] $dependencies |
||
| 24 | * @param ParameterInterface[] $parameters |
||
| 25 | * @return array |
||
| 26 | * @throws NotFoundException |
||
| 27 | */ |
||
| 28 | 41 | public function buildArguments(array $dependencies, array $parameters): array |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @param DependencyInterface[] $dependencies |
||
| 43 | * @param ParameterInterface[] $parameters |
||
| 44 | * @return array |
||
| 45 | * @throws NotFoundException |
||
| 46 | */ |
||
| 47 | 33 | protected function buildArgumentsFromDependencies(array $dependencies, array $parameters): array |
|
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $parameters |
||
| 76 | * @param array $usedParameters |
||
| 77 | * @param array $arguments |
||
| 78 | * @return array |
||
| 79 | * @throws NotFoundException |
||
| 80 | */ |
||
| 81 | 30 | protected function appendUnusedParamsToArguments( |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param ParameterInterface $parameter |
||
| 96 | * @return mixed |
||
| 97 | * @throws NotFoundException |
||
| 98 | */ |
||
| 99 | 18 | View Code Duplication | protected function makeArgumentByParameter(ParameterInterface $parameter) |
| 110 | |||
| 111 | /** |
||
| 112 | * @param DependencyInterface $dependency |
||
| 113 | * @return mixed |
||
| 114 | * @throws NotFoundException |
||
| 115 | */ |
||
| 116 | 25 | View Code Duplication | protected function makeArgumentByDependency(DependencyInterface $dependency) |
| 127 | |||
| 128 | /** |
||
| 129 | * @param $id |
||
| 130 | * @return mixed |
||
| 131 | * @throws NotFoundException |
||
| 132 | */ |
||
| 133 | 23 | protected function retrieveRequiredDependencyFromContainer($id) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param $id |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | 4 | protected function retrieveOptionalDependencyFromContainer($id) |
|
| 152 | } |
||
| 153 |
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.