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 |
||
15 | View Code Duplication | class Version_Loader { |
|
|
|||
16 | |||
17 | /** |
||
18 | * The Version_Selector object. |
||
19 | * |
||
20 | * @var Version_Selector |
||
21 | */ |
||
22 | private $version_selector; |
||
23 | |||
24 | /** |
||
25 | * A map of available classes and their version and file path. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $classmap; |
||
30 | |||
31 | /** |
||
32 | * A map of PSR-4 namespaces and their version and directory path. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $psr4_map; |
||
37 | |||
38 | /** |
||
39 | * A map of all the files that we should load. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $filemap; |
||
44 | |||
45 | /** |
||
46 | * The constructor. |
||
47 | * |
||
48 | * @param Version_Selector $version_selector The Version_Selector object. |
||
49 | * @param array $classmap The verioned classmap to load using. |
||
50 | * @param array $psr4_map The versioned PSR-4 map to load using. |
||
51 | * @param array $filemap The versioned filemap to load. |
||
52 | */ |
||
53 | public function __construct( $version_selector, $classmap, $psr4_map, $filemap ) { |
||
59 | |||
60 | /** |
||
61 | * Finds the file path for the given class. |
||
62 | * |
||
63 | * @param string $class_name The class to find. |
||
64 | * |
||
65 | * @return string|null $file_path The path to the file if found, null if no class was found. |
||
66 | */ |
||
67 | public function find_class_file( $class_name ) { |
||
78 | |||
79 | /** |
||
80 | * Load all of the files in the filemap. |
||
81 | */ |
||
82 | public function load_filemap() { |
||
95 | |||
96 | /** |
||
97 | * Compares different class sources and returns the newest. |
||
98 | * |
||
99 | * @param array|null $classmap_data The classmap class data. |
||
100 | * @param array|null $psr4_data The PSR-4 class data. |
||
101 | * |
||
102 | * @return array|null $data |
||
103 | */ |
||
104 | private function select_newest_file( $classmap_data, $psr4_data ) { |
||
117 | |||
118 | /** |
||
119 | * Finds the file for a given class in a PSR-4 namespace. |
||
120 | * |
||
121 | * @param string $class_name The class to find. |
||
122 | * |
||
123 | * @return array|null $data The version and path path to the file if found, null otherwise. |
||
124 | */ |
||
125 | private function find_psr4_file( $class_name ) { |
||
164 | } |
||
165 |
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.