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 |
||
| 6 | class V2PropertyContainer extends ContainerMagic implements IPropertyContainer { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Property descriptions |
||
| 10 | * |
||
| 11 | * @var array[] |
||
| 12 | */ |
||
| 13 | protected $properties = array(); |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Array of accessors - getters/setters/etc |
||
| 17 | * |
||
| 18 | * Getter is a callable like |
||
| 19 | * function ($value) use ($that) {} |
||
| 20 | * |
||
| 21 | * Setter is a callable like |
||
| 22 | * function () use ($that) {} |
||
| 23 | * |
||
| 24 | * Importer is a callable like |
||
| 25 | * function (&$row) use ($this) {} |
||
| 26 | * |
||
| 27 | * Exporter is a callable like |
||
| 28 | * function (&$row) use ($this) {} |
||
| 29 | * |
||
| 30 | * @var callable[][] |
||
| 31 | */ |
||
| 32 | protected $accessors; |
||
| 33 | |||
| 34 | public function setProperties($properties) { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Is container contains no data |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function isEmpty() { |
||
| 46 | |||
| 47 | public function assignAccessor($varName, $type, $callable) { |
||
| 58 | |||
| 59 | View Code Duplication | public function __set($name, $value) { |
|
| 66 | |||
| 67 | View Code Duplication | public function __get($name) { |
|
| 74 | |||
| 75 | View Code Duplication | public function importRow($row) { |
|
| 91 | |||
| 92 | View Code Duplication | public function exportRow() { |
|
| 106 | |||
| 107 | } |
||
| 108 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.