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 | TwigDocumentInterface, |
||
| 20 | JailedDocumentInterface |
||
| 21 | { |
||
| 22 | protected $data; |
||
| 23 | |||
| 24 | private $fileExtension; |
||
| 25 | private $namespace; |
||
| 26 | private $fileName; |
||
| 27 | private $filePath; |
||
| 28 | private $fs; |
||
|
|
|||
| 29 | |||
| 30 | public function __construct($filePath) |
||
| 46 | |||
| 47 | /// |
||
| 48 | // Jailed Document implementation |
||
| 49 | /// |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function createJail() |
||
| 60 | |||
| 61 | /// |
||
| 62 | // ArrayAccess implementation |
||
| 63 | /// |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function offsetExists($offset) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function offsetGet($offset) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function offsetSet($offset, $value) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * {@inheritdoc} |
||
| 91 | */ |
||
| 92 | public function offsetUnset($offset) |
||
| 96 | |||
| 97 | /// |
||
| 98 | // Twig Document implementation |
||
| 99 | /// |
||
| 100 | |||
| 101 | public function getNamespace() |
||
| 105 | |||
| 106 | public function setNamespace($namespace) |
||
| 110 | |||
| 111 | public function getExtension() |
||
| 115 | |||
| 116 | public function getFilePath() |
||
| 120 | |||
| 121 | public function getName() |
||
| 125 | |||
| 126 | public function getRelativeFilePath() |
||
| 136 | |||
| 137 | public function refreshFileContent() |
||
| 157 | |||
| 158 | /// |
||
| 159 | // File parsing helpers |
||
| 160 | /// |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Convert from CSV into an associative array. |
||
| 164 | * |
||
| 165 | * @param string $content CSV formatted text |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | private function fromCsv($content) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Convert from JSON into an associative array. |
||
| 185 | * |
||
| 186 | * @param string $content JSON formatted text |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | private function fromJson($content) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Convert from XML into an associative array. |
||
| 197 | * |
||
| 198 | * @param string $content XML formatted text |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | private function fromXml($content) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Convert from YAML into an associative array. |
||
| 209 | * |
||
| 210 | * @param string $content YAML formatted text |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | private function fromYaml($content) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * An alias for handling `*.yml` files. |
||
| 221 | * |
||
| 222 | * @param string $content YAML formatted text |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | private function fromYml($content) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $extension |
||
| 233 | * |
||
| 234 | * @todo 0.1.0 Create a help page on the main stakx website for this topic and link to it |
||
| 235 | * |
||
| 236 | * @throws DependencyMissingException |
||
| 237 | */ |
||
| 238 | private function handleDependencies($extension) |
||
| 245 | } |
||
| 246 |
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.