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:
Complex classes like JsDumper 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 JsDumper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class JsDumper |
||
21 | { |
||
22 | const DISCLAIMER = <<<JS |
||
23 | /* |
||
24 | * This file was generated by the "elao/enum" PHP package. |
||
25 | * The code inside belongs to you and can be altered, but no BC promise is guaranteed. |
||
26 | */ |
||
27 | JS; |
||
28 | /** @var string */ |
||
29 | private $libPath; |
||
30 | |||
31 | /** @var string|null */ |
||
32 | private $baseDir; |
||
33 | |||
34 | public function __construct(string $libPath, string $baseDir = null) |
||
40 | |||
41 | public function dumpLibrarySources() |
||
48 | |||
49 | /** |
||
50 | * @param class-string<EnumInterface> $enumFqcn |
||
1 ignored issue
–
show
|
|||
51 | */ |
||
52 | public function dumpEnumToFile(string $enumFqcn, string $path) |
||
71 | |||
72 | /** |
||
73 | * @param class-string<EnumInterface> $enumFqcn |
||
1 ignored issue
–
show
|
|||
74 | */ |
||
75 | public function dumpEnumClass(string $enumFqcn): string |
||
86 | |||
87 | private function dumpImports(string $path, string $enumFqcn): string |
||
104 | |||
105 | private function startClass(string $enumFqcn): string |
||
118 | |||
119 | private function generateEnumerableValues(string $enumFqcn): string |
||
129 | |||
130 | private function generateMasks(string $enumFqcn) |
||
144 | |||
145 | private function generateReadables(string $enumFqcn): string |
||
194 | |||
195 | /** |
||
196 | * @param class-string<EnumInterface> $enumFqcn |
||
1 ignored issue
–
show
|
|||
197 | */ |
||
198 | private function getEnumerableValues(string $enumFqcn): array |
||
221 | |||
222 | /** |
||
223 | * @param class-string<FlaggedEnum> $enumFqcn |
||
1 ignored issue
–
show
|
|||
224 | */ |
||
225 | private function getMasks(string $enumFqcn): array |
||
252 | |||
253 | /** |
||
254 | * @param class-string<EnumInterface> $enumFqcn |
||
1 ignored issue
–
show
|
|||
255 | */ |
||
256 | private function getShortName(string $enumFqcn): string |
||
262 | |||
263 | /** |
||
264 | * @param class-string<EnumInterface> $enumFqcn |
||
1 ignored issue
–
show
|
|||
265 | */ |
||
266 | private function getConstants(string $enumFqcn): array |
||
292 | |||
293 | public function normalizePath(string $path): string |
||
309 | } |
||
310 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: