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 // phpcs:ignore WordPress.Files.FileName |
||
22 | class AutoloadProcessor { |
||
23 | |||
24 | /** |
||
25 | * A callable for scanning a directory for all of its classes. |
||
26 | * |
||
27 | * @var callable |
||
28 | */ |
||
29 | private $classmapScanner; |
||
30 | |||
31 | /** |
||
32 | * A callable for transforming a path into one to be used in code. |
||
33 | * |
||
34 | * @var callable |
||
35 | */ |
||
36 | private $pathCodeTransformer; |
||
37 | |||
38 | /** |
||
39 | * The constructor. |
||
40 | * |
||
41 | * @param callable $classmapScanner A callable for scanning a directory for all of its classes. |
||
42 | * @param callable $pathCodeTransformer A callable for transforming a path into one to be used in code. |
||
43 | */ |
||
44 | public function __construct( $classmapScanner, $pathCodeTransformer ) { |
||
48 | |||
49 | /** |
||
50 | * Processes the classmap autoloads into a relative path format including the version for each file. |
||
51 | * |
||
52 | * @param array $autoloads The autoloads we are processing. |
||
53 | * @param bool $scanPsrPackages Whether or not PSR packages should be converted to a classmap. |
||
54 | * |
||
55 | * @return array $processed |
||
56 | */ |
||
57 | public function processClassmap( $autoloads, $scanPsrPackages ) { |
||
106 | |||
107 | /** |
||
108 | * Processes the PSR-4 autoloads into a relative path format including the version for each file. |
||
109 | * |
||
110 | * @param array $autoloads The autoloads we are processing. |
||
111 | * @param bool $scanPsrPackages Whether or not PSR packages should be converted to a classmap. |
||
112 | * |
||
113 | * @return array $processed |
||
114 | */ |
||
115 | public function processPsr4Packages( $autoloads, $scanPsrPackages ) { |
||
138 | |||
139 | /** |
||
140 | * Processes the file autoloads into a relative format including the version for each file. |
||
141 | * |
||
142 | * @param array $autoloads The autoloads we are processing. |
||
143 | * |
||
144 | * @return array|null $processed |
||
145 | */ |
||
146 | public function processFiles( $autoloads ) { |
||
162 | } |
||
163 |
It seems like you are relying on a variable being defined by an iteration: