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 |
||
12 | class ClassLoader |
||
|
|||
13 | { |
||
14 | |||
15 | const EXT = '.php'; |
||
16 | const DS = DIRECTORY_SEPARATOR; |
||
17 | |||
18 | private static $instance; |
||
19 | |||
20 | private $namespacedPaths = []; |
||
21 | |||
22 | private $aliases = []; |
||
23 | |||
24 | private $classesPaths = []; |
||
25 | |||
26 | private function __construct() { |
||
29 | |||
30 | private function __clone() { |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * @return ClassLoader |
||
37 | */ |
||
38 | public static function getInstance() { |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * @param array $array |
||
48 | * @param string $path |
||
49 | * @param string (optional) $key |
||
50 | * @param string $key |
||
51 | * @throws \Exception |
||
52 | */ |
||
53 | private function addPath(array &$array, $path, $key = null) { |
||
67 | |||
68 | /** |
||
69 | * |
||
70 | * @param string $path |
||
71 | * @return \ClassLoader |
||
72 | */ |
||
73 | public function registerNamespacedPath($path) { |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @param string $path |
||
81 | * @return \ClassLoader |
||
82 | */ |
||
83 | public function registerClassesPath($path) { |
||
87 | |||
88 | /** |
||
89 | * |
||
90 | * @param string $path |
||
91 | * @param string $name |
||
92 | * @return \ClassLoader |
||
93 | */ |
||
94 | public function registerAlias($path, $name) { |
||
98 | |||
99 | /** |
||
100 | * Function for registering in spl_autoload_register() |
||
101 | * @param string $className |
||
102 | */ |
||
103 | public function autoload($className) { |
||
111 | |||
112 | /** |
||
113 | * @param string $className |
||
114 | */ |
||
115 | private function lookInAliases($className) { |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | * @param string $className |
||
137 | * @return array [namespace, fileName] |
||
138 | */ |
||
139 | private function splitClassName($className) { |
||
144 | |||
145 | /** |
||
146 | * @param string $className |
||
147 | */ |
||
148 | private function loadNamespacedClass($className) { |
||
161 | |||
162 | /** |
||
163 | * @param string $className |
||
164 | */ |
||
165 | private function loadClass($className) { |
||
175 | |||
176 | /** |
||
177 | * @param string $classPath |
||
178 | */ |
||
179 | private function includeClass($classPath) { |
||
186 | |||
187 | } |