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 |
||
| 17 | class DataItem implements |
||
| 18 | \ArrayAccess, |
||
| 19 | \IteratorAggregate, |
||
| 20 | TwigDocumentInterface, |
||
| 21 | JailedDocumentInterface |
||
| 22 | { |
||
| 23 | protected $data; |
||
| 24 | |||
| 25 | private $fileExtension; |
||
| 26 | private $namespace; |
||
| 27 | private $fileName; |
||
| 28 | private $filePath; |
||
| 29 | private $fs; |
||
|
|
|||
| 30 | |||
| 31 | public function __construct($filePath) |
||
| 47 | |||
| 48 | public function getIterator() |
||
| 52 | |||
| 53 | /// |
||
| 54 | // Jailed Document implementation |
||
| 55 | /// |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function createJail() |
||
| 66 | |||
| 67 | /// |
||
| 68 | // ArrayAccess implementation |
||
| 69 | /// |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function offsetExists($offset) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function offsetGet($offset) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function offsetSet($offset, $value) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function offsetUnset($offset) |
||
| 102 | |||
| 103 | /// |
||
| 104 | // Twig Document implementation |
||
| 105 | /// |
||
| 106 | |||
| 107 | public function getNamespace() |
||
| 111 | |||
| 112 | public function setNamespace($namespace) |
||
| 116 | |||
| 117 | public function getExtension() |
||
| 121 | |||
| 122 | public function getFilePath() |
||
| 126 | |||
| 127 | public function getName() |
||
| 131 | |||
| 132 | public function getRelativeFilePath() |
||
| 142 | |||
| 143 | public function refreshFileContent() |
||
| 164 | |||
| 165 | /// |
||
| 166 | // File parsing helpers |
||
| 167 | /// |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Convert from CSV into an associative array. |
||
| 171 | * |
||
| 172 | * @param string $content CSV formatted text |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | private function fromCsv($content) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Convert from JSON into an associative array. |
||
| 192 | * |
||
| 193 | * @param string $content JSON formatted text |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | private function fromJson($content) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Convert from XML into an associative array. |
||
| 204 | * |
||
| 205 | * @param string $content XML formatted text |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | private function fromXml($content) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Convert from YAML into an associative array. |
||
| 216 | * |
||
| 217 | * @param string $content YAML formatted text |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | private function fromYaml($content) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * An alias for handling `*.yml` files. |
||
| 228 | * |
||
| 229 | * @param string $content YAML formatted text |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | private function fromYml($content) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $extension |
||
| 240 | * |
||
| 241 | * @todo 0.1.0 Create a help page on the main stakx website for this topic and link to it |
||
| 242 | * |
||
| 243 | * @throws DependencyMissingException |
||
| 244 | */ |
||
| 245 | private function handleDependencies($extension) |
||
| 252 | } |
||
| 253 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.