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 |
||
34 | class Autoloader { |
||
35 | /** @var bool */ |
||
36 | private $useGlobalClassPath = true; |
||
37 | /** @var array */ |
||
38 | private $validRoots = []; |
||
39 | |||
40 | /** |
||
41 | * Optional low-latency memory cache for class to path mapping. |
||
42 | * |
||
43 | * @var \OC\Memcache\Cache |
||
44 | */ |
||
45 | protected $memoryCache; |
||
46 | |||
47 | /** |
||
48 | * Autoloader constructor. |
||
49 | * |
||
50 | * @param string[] $validRoots |
||
51 | */ |
||
52 | 32 | public function __construct(array $validRoots) { |
|
57 | |||
58 | /** |
||
59 | * Add a path to the list of valid php roots for auto loading |
||
60 | * |
||
61 | * @param string $root |
||
62 | */ |
||
63 | 1074 | public function addValidRoot($root) { |
|
67 | |||
68 | /** |
||
69 | * disable the usage of the global classpath \OC::$CLASSPATH |
||
70 | */ |
||
71 | public function disableGlobalClassPath() { |
||
74 | |||
75 | /** |
||
76 | * enable the usage of the global classpath \OC::$CLASSPATH |
||
77 | */ |
||
78 | public function enableGlobalClassPath() { |
||
81 | |||
82 | /** |
||
83 | * get the possible paths for a class |
||
84 | * |
||
85 | * @param string $class |
||
86 | * @return array|bool an array of possible paths or false if the class is not part of ownCloud |
||
87 | */ |
||
88 | 273 | public function findClass($class) { |
|
89 | 273 | $class = trim($class, '\\'); |
|
90 | |||
91 | 273 | $paths = array(); |
|
92 | 273 | if ($this->useGlobalClassPath && array_key_exists($class, \OC::$CLASSPATH)) { |
|
93 | 4 | $paths[] = \OC::$CLASSPATH[$class]; |
|
94 | /** |
||
95 | * @TODO: Remove this when necessary |
||
96 | * Remove "apps/" from inclusion path for smooth migration to mutli app dir |
||
97 | */ |
||
98 | 4 | if (strpos(\OC::$CLASSPATH[$class], 'apps/') === 0) { |
|
99 | \OCP\Util::writeLog('core', 'include path for class "' . $class . '" starts with "apps/"', \OCP\Util::DEBUG); |
||
100 | $paths[] = str_replace('apps/', '', \OC::$CLASSPATH[$class]); |
||
101 | } |
||
102 | 273 | } elseif (strpos($class, 'OC_') === 0) { |
|
103 | 37 | $paths[] = \OC::$SERVERROOT . '/lib/private/legacy/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php'); |
|
104 | 37 | $paths[] = \OC::$SERVERROOT . '/lib/private/' . strtolower(str_replace('_', '/', substr($class, 3)) . '.php'); |
|
105 | 272 | } elseif (strpos($class, 'OC\\') === 0) { |
|
106 | 144 | $split = explode('\\', $class, 3); |
|
107 | |||
108 | 144 | if (count($split) === 3) { |
|
109 | 131 | $split[1] = strtolower($split[1]); |
|
110 | |||
111 | 131 | if ($split[1] === 'core') { |
|
112 | 27 | $paths[] = \OC::$SERVERROOT . '/core/' . strtolower(str_replace('\\', '/', $split[2])) . '.php'; |
|
113 | 131 | } else if ($split[1] === 'settings') { |
|
114 | 12 | $paths[] = \OC::$SERVERROOT . '/settings/' . strtolower(str_replace('\\', '/', $split[2])) . '.php'; |
|
115 | 105 | } else if ($split[1] === 'repair') { |
|
116 | 11 | $paths[] = \OC::$SERVERROOT . '/lib/repair/' . strtolower(str_replace('\\', '/', $split[2])) . '.php'; |
|
117 | |||
118 | 11 | } else { |
|
119 | 83 | $paths[] = \OC::$SERVERROOT . '/lib/private/' . $split[1] . '/' . strtolower(str_replace('\\', '/', $split[2])) . '.php'; |
|
120 | } |
||
121 | |||
122 | 131 | } else { |
|
123 | 16 | $paths[] = \OC::$SERVERROOT . '/lib/private/' . strtolower(str_replace('\\', '/', $split[1])) . '.php'; |
|
124 | } |
||
125 | 239 | View Code Duplication | } elseif (strpos($class, 'OCP\\') === 0) { |
|
|||
126 | 56 | $paths[] = \OC::$SERVERROOT . '/lib/public/' . strtolower(str_replace('\\', '/', substr($class, 4)) . '.php'); |
|
127 | 129 | } elseif (strpos($class, 'OCA\\') === 0) { |
|
128 | 57 | list(, $app, $rest) = explode('\\', $class, 3); |
|
129 | 57 | $app = strtolower($app); |
|
130 | 57 | $appPath = \OC_App::getAppPath($app); |
|
131 | 57 | if ($appPath && stream_resolve_include_path($appPath)) { |
|
132 | 57 | $paths[] = $appPath . '/' . strtolower(str_replace('\\', '/', $rest) . '.php'); |
|
133 | // If not found in the root of the app directory, insert '/lib' after app id and try again. |
||
134 | 57 | $paths[] = $appPath . '/lib/' . strtolower(str_replace('\\', '/', $rest) . '.php'); |
|
135 | 57 | } |
|
136 | 77 | View Code Duplication | } elseif (strpos($class, 'Test_') === 0) { |
137 | 1 | $paths[] = \OC::$SERVERROOT . '/tests/lib/' . strtolower(str_replace('_', '/', substr($class, 5)) . '.php'); |
|
138 | 20 | } elseif (strpos($class, 'Test\\') === 0) { |
|
139 | 1 | $paths[] = \OC::$SERVERROOT . '/tests/lib/' . strtolower(str_replace('\\', '/', substr($class, 5)) . '.php'); |
|
140 | 1 | } |
|
141 | 273 | return $paths; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param string $fullPath |
||
146 | * @return bool |
||
147 | */ |
||
148 | 244 | protected function isValidPath($fullPath) { |
|
156 | |||
157 | /** |
||
158 | * Load the specified class |
||
159 | * |
||
160 | * @param string $class |
||
161 | * @return bool |
||
162 | */ |
||
163 | 261 | public function load($class) { |
|
194 | |||
195 | /** |
||
196 | * Sets the optional low-latency cache for class to path mapping. |
||
197 | * |
||
198 | * @param \OC\Memcache\Cache $memoryCache Instance of memory cache. |
||
199 | */ |
||
200 | public function setMemoryCache(\OC\Memcache\Cache $memoryCache = null) { |
||
203 | } |
||
204 |
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.