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 |
||
| 20 | class FileLoader extends BaseFileLoader |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Identifies a plugin type configuration |
||
| 24 | */ |
||
| 25 | const PLUGIN = 'plugin'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Contains all loaded config trees. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $configs = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Contains all loaded plugin config trees. |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $plugins = array(); |
||
| 40 | protected $pluginPaths = array(); |
||
| 41 | protected $sourceFiles = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor. |
||
| 45 | * |
||
| 46 | * @param FileLocatorInterface $locator |
||
| 47 | * @param \Zicht\Version\Version $version |
||
| 48 | */ |
||
| 49 | 4 | public function __construct(FileLocatorInterface $locator, Version $version = null) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param array $sourceFiles |
||
| 58 | */ |
||
| 59 | public function setSourceFiles($sourceFiles) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | public function getSourceFiles() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @{inheritDoc} |
||
| 74 | */ |
||
| 75 | 4 | public function load($resource, $type = null) |
|
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Parse the annotations contained in commented lines, starting with # |
||
| 125 | * |
||
| 126 | * Annotation format is '@' followed by a word, followed by an optional ':' or '=', followed by a quoted value, |
||
| 127 | * e.g. |
||
| 128 | * <code>@foo="bar"</code> |
||
| 129 | * |
||
| 130 | * @param string $fileContents |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | 4 | public function parseAnnotations($fileContents) |
|
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @{inheritDoc} |
||
| 145 | */ |
||
| 146 | 2 | public function supports($resource, $type = null) |
|
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Processes plugin definitions |
||
| 154 | * |
||
| 155 | * @param array $plugins |
||
| 156 | * @param string $dir |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 3 | protected function processPlugins($plugins, $dir) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Add a plugin at the passed location |
||
| 168 | * |
||
| 169 | * @param string $name |
||
| 170 | * @param string $dir |
||
| 171 | * @return void |
||
| 172 | * |
||
| 173 | * @throws \InvalidArgumentException |
||
| 174 | * @throws \UnexpectedValueException |
||
| 175 | */ |
||
| 176 | 3 | public function addPlugin($name, $dir) |
|
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * Processes imports |
||
| 216 | * |
||
| 217 | * @param array $imports |
||
| 218 | * @param string $dir |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | 1 | protected function processImports($imports, $dir) |
|
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns all loaded configs |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 1 | public function getConfigs() |
|
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns all loaded plugins |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 1 | public function getPlugins() |
|
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns all loaded paths |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function getPluginPaths() |
||
| 261 | } |
||
| 262 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: