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 |
||
| 15 | class CodeGeneratorService extends AbstractService { |
||
| 16 | |||
| 17 | private $codegen; |
||
| 18 | |||
| 19 | 7 | public function getCodegenFile() { |
|
| 23 | |||
| 24 | /** |
||
| 25 | * Loads the contents from codegen.json into a collection |
||
| 26 | * |
||
| 27 | * @throws JsonEmptyException |
||
| 28 | * @throws \RuntimeException |
||
| 29 | * @return CodegenSchema |
||
| 30 | */ |
||
| 31 | 12 | public function getCodegen() { |
|
| 41 | |||
| 42 | // /** |
||
| 43 | // * Returns the codegen part for the given action name or an empty map |
||
| 44 | // * |
||
| 45 | // * @param string $name |
||
| 46 | // * @return Map |
||
| 47 | // */ |
||
| 48 | // public function getCodegenAction($name) { |
||
| 49 | // $codegen = $this->getCodegen(); |
||
| 50 | |||
| 51 | // if (isset($codegen['actions'])) { |
||
| 52 | // $actions = $codegen['actions']; |
||
| 53 | |||
| 54 | // if (isset($actions[$name])) { |
||
| 55 | // return $actions[$name]; |
||
| 56 | // } |
||
| 57 | // } |
||
| 58 | |||
| 59 | // return null; |
||
| 60 | // } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Adds authors to the docblock of the given struct |
||
| 64 | * |
||
| 65 | 15 | * @param AbstractPhpStruct $struct |
|
| 66 | 15 | * @param array $package |
|
| 67 | */ |
||
| 68 | 15 | public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package) { |
|
| 83 | 15 | ||
| 84 | /** |
||
| 85 | * Returns code for hydrating a propel model |
||
| 86 | * |
||
| 87 | * @param string $modelName |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function getWriteFields($modelName) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the fields for a model |
||
| 123 | * |
||
| 124 | * @param string $modelName |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getReadFields($modelName) { |
||
| 146 | |||
| 147 | // /** |
||
| 148 | // * Returns conversions for model columns |
||
| 149 | // * |
||
| 150 | 5 | // * @param string $model |
|
| 151 | 5 | // * @param string $type |
|
| 152 | // * @return array |
||
| 153 | // */ |
||
| 154 | 5 | // public function getConversions($model, $type) { |
|
| 155 | 5 | // return $this->getActionProp($model, $type, 'conversion'); |
|
| 156 | 5 | // } |
|
| 157 | 5 | ||
| 158 | 5 | // /** |
|
| 159 | 5 | // * Returns model columns that should be filtered |
|
| 160 | // * |
||
| 161 | 5 | // * @param string $model |
|
| 162 | // * @param string $type |
||
| 163 | // * @return array |
||
| 164 | 5 | // */ |
|
| 165 | 5 | // public function getFilter($model, $type) { |
|
| 166 | // return $this->getActionProp($model, $type, 'filter'); |
||
| 167 | 5 | // } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Returns computed model fields |
||
| 171 | * |
||
| 172 | * @param Table $table |
||
| 173 | * @return array<string> |
||
| 174 | */ |
||
| 175 | public function getComputedFields(Table $table) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Helper to represent an array as php code |
||
| 197 | 15 | * |
|
| 198 | 15 | * @param array $array |
|
| 199 | 15 | * @return string |
|
| 200 | 15 | */ |
|
| 201 | 15 | View Code Duplication | public function arrayToCode(array $array) { |
| 213 | |||
| 214 | 15 | View Code Duplication | public function mapToCode(array $array) { |
| 226 | |||
| 227 | public function getFilename(AbstractPhpStruct $struct) { |
||
| 241 | |||
| 242 | public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) { |
||
| 258 | } |
||
| 259 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.