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 |
||
| 10 | View Code Duplication | class MetaObject implements ObjectInterface { |
|
| 11 | use AtMemberManager; |
||
| 12 | |||
| 13 | /** @var array */ |
||
| 14 | protected $meta = []; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * human api |
||
| 18 | */ |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param array $meta |
||
| 22 | * @return MetaObject |
||
| 23 | */ |
||
| 24 | public static function fromArray(array $meta) { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param object $meta |
||
| 36 | * @return MetaObject |
||
| 37 | */ |
||
| 38 | public static function fromObject($meta) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * spec api |
||
| 46 | */ |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $key |
||
| 50 | * @param mixed $value |
||
| 51 | */ |
||
| 52 | public function add($key, $value) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * ObjectInterface |
||
| 64 | */ |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @inheritDoc |
||
| 68 | */ |
||
| 69 | public function isEmpty() { |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @inheritDoc |
||
| 75 | */ |
||
| 76 | public function toArray() { |
||
| 79 | } |
||
| 80 |
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.