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 |
||
| 49 | class ClassLoader { |
||
| 50 | |||
| 51 | protected $namespaces = array(); |
||
| 52 | protected $prefixes = array(); |
||
| 53 | protected $fallbacks = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Elgg\ClassMap Map of classes to files |
||
| 57 | */ |
||
| 58 | protected $map; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | * |
||
| 63 | * @param \Elgg\ClassMap $map Class map |
||
| 64 | */ |
||
| 65 | 6 | public function __construct(\Elgg\ClassMap $map) { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Get the class map |
||
| 71 | * |
||
| 72 | * @return \Elgg\ClassMap |
||
| 73 | */ |
||
| 74 | public function getClassMap() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Gets the configured namespaces. |
||
| 80 | * |
||
| 81 | * @return array A hash with namespaces as keys and directories as values |
||
| 82 | */ |
||
| 83 | public function getNamespaces() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Gets the configured class prefixes. |
||
| 89 | * |
||
| 90 | * @return array A hash with class prefixes as keys and directories as values |
||
| 91 | */ |
||
| 92 | public function getPrefixes() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Registers an array of namespaces |
||
| 98 | * |
||
| 99 | * @param array $namespaces An array of namespaces (namespaces as keys and locations as values) |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function registerNamespaces(array $namespaces) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Registers a namespace. |
||
| 110 | * |
||
| 111 | * @param string $namespace The namespace |
||
| 112 | * @param array|string $paths The location(s) of the namespace |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function registerNamespace($namespace, $paths) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Registers an array of classes using the PEAR naming convention. |
||
| 121 | * |
||
| 122 | * @param array $classes An array of classes (prefixes as keys and locations as values) |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function registerPrefixes(array $classes) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Registers a set of classes using the PEAR naming convention. |
||
| 133 | * |
||
| 134 | * @param string $prefix The classes prefix |
||
| 135 | * @param array|string $paths The location(s) of the classes |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public function registerPrefix($prefix, $paths) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add a directory to search if no registered directory is found. |
||
| 144 | * |
||
| 145 | * @param string $path The directory |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | public function addFallback($path) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Registers this instance as an autoloader. |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | 6 | public function register() { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Loads the given class or interface, possibly updating the class map. |
||
| 163 | * |
||
| 164 | * @param string $class The name of the class |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | 1 | public function loadClass($class) { |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Finds the path to the file where the class is defined. |
||
| 184 | * |
||
| 185 | * @param string $class The name of the class |
||
| 186 | * |
||
| 187 | * @return string|null The path, if found |
||
| 188 | */ |
||
| 189 | 1 | public function findFile($class) { |
|
| 239 | } |
||
| 240 | |||
| 241 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: