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 ClassNameMapper 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 ClassNameMapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ClassNameMapper |
||
12 | { |
||
13 | /** |
||
14 | * |
||
15 | * @var array<namespace, path[]> |
||
16 | */ |
||
17 | private $psr0Namespaces = array(); |
||
18 | |||
19 | /** |
||
20 | * |
||
21 | * @var array<namespace, path[]> |
||
22 | */ |
||
23 | private $psr4Namespaces = array(); |
||
24 | |||
25 | /** |
||
26 | * Registers a PSR-0 namespace. |
||
27 | * |
||
28 | * @param string $namespace The namespace to register |
||
29 | * @param string|array $path The path on the filesystem (or an array of paths) |
||
30 | */ |
||
31 | View Code Duplication | public function registerPsr0Namespace($namespace, $path) { |
|
50 | |||
51 | /** |
||
52 | * Registers a PSR-4 namespace. |
||
53 | * |
||
54 | * @param string $namespace The namespace to register |
||
55 | * @param string|array $path The path on the filesystem (or an array of paths) |
||
56 | */ |
||
57 | View Code Duplication | public function registerPsr4Namespace($namespace, $path) { |
|
76 | |||
77 | /** |
||
78 | * Create a class name mapper from the composer.json file generated by Composer. |
||
79 | * This class name mapper can only map classes in the project directory (not in the vendor directory). |
||
80 | * |
||
81 | * @param string $composerJsonPath |
||
82 | * @param string $rootPath |
||
83 | * @param bool $useAutoloadDev |
||
84 | * @return ClassNameMapper |
||
85 | */ |
||
86 | public static function createFromComposerFile($composerJsonPath = null, $rootPath = null, $useAutoloadDev = false) { |
||
97 | |||
98 | /** |
||
99 | * Create a class name mapper from the autoload.php file generated by Composer. |
||
100 | * This class name mapper can map classes in the project directory AND in the vendor directory. |
||
101 | * |
||
102 | * @param string|null $composerAutoloadPath |
||
103 | * @return ClassNameMapper |
||
104 | */ |
||
105 | public static function createFromComposerAutoload($composerAutoloadPath = null) |
||
117 | |||
118 | /** |
||
119 | * |
||
120 | * @param string $composerJsonPath Path to the composer file |
||
121 | * @param string $rootPath Root path of the project (or null) |
||
122 | */ |
||
123 | public function loadComposerFile($composerJsonPath, $rootPath = null, $useAutoloadDev = false) { |
||
197 | |||
198 | /** |
||
199 | * @param string $composerAutoloadPath |
||
200 | * @return self |
||
201 | */ |
||
202 | public function loadComposerAutoload($composerAutoloadPath) |
||
221 | |||
222 | /** |
||
223 | * Given an existing path, convert it to a path relative to a given starting path. |
||
224 | * Shamelessly borrowed to Symfony :). Thanks guys. |
||
225 | * Note: we do not include Symfony's "FileSystem" component to avoid adding too many dependencies. |
||
226 | * |
||
227 | * @param string $endPath Absolute path of target |
||
228 | * @param string $startPath Absolute path where traversal begins |
||
229 | * |
||
230 | * @return string Path of target relative to starting path |
||
231 | */ |
||
232 | private static function makePathRelative($endPath, $startPath) |
||
256 | |||
257 | /** |
||
258 | * Returns a list of all namespaces that are managed by the ClassNameMapper. |
||
259 | * |
||
260 | * @return string[] |
||
261 | */ |
||
262 | public function getManagedNamespaces() { |
||
265 | |||
266 | /** |
||
267 | * Returns a list of paths that can be used to store $className. |
||
268 | * |
||
269 | * @param string $className |
||
270 | * @return string[] |
||
271 | */ |
||
272 | public function getPossibleFileNames($className) { |
||
336 | |||
337 | /** |
||
338 | * Takes in parameter an array like |
||
339 | * [{ "Mouf": "src/" }] or [{ "Mouf": ["src/", "src2/"] }] . |
||
340 | * returns |
||
341 | * [ |
||
342 | * {"namespace"=> "Mouf", "directory"=>"src/"}, |
||
343 | * {"namespace"=> "Mouf", "directory"=>"src2/"} |
||
344 | * ] |
||
345 | * |
||
346 | * @param array $autoload |
||
347 | * @return array<int, array<string, string>> |
||
348 | */ |
||
349 | private static function unfactorizeAutoload(array $autoload) { |
||
368 | |||
369 | /** |
||
370 | * Makes sure the directory ends with a / (unless the string is empty) |
||
371 | * |
||
372 | * @param string $dir |
||
373 | * @return string |
||
374 | */ |
||
375 | private static function normalizeDirectory($dir) { |
||
378 | } |
||
379 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.