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 ModificationBehavior extends SActiveRecordBehavior |
||
| 18 | {
|
||
| 19 | private $child; |
||
| 20 | |||
| 21 | View Code Duplication | public function init() |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @return bool |
||
| 36 | */ |
||
| 37 | public function isModification() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return Product |
||
| 44 | */ |
||
| 45 | public function getParentModel() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return Product[] |
||
| 52 | */ |
||
| 53 | public function getModifications() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param $id |
||
| 60 | * |
||
| 61 | * @return Product|null |
||
| 62 | */ |
||
| 63 | public function getChild($id) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | public function isParent() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return Product|null |
||
| 81 | */ |
||
| 82 | public function getFirstModification() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Если у модификации нет изображения, метод вернет изображение родителя |
||
| 89 | * @param string $type |
||
| 90 | * |
||
| 91 | * @return FActiveImage |
||
| 92 | */ |
||
| 93 | public function getImageModification($type = 'main') |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $type |
||
| 114 | * |
||
| 115 | * @return FActiveImage |
||
| 116 | */ |
||
| 117 | public function getImagesModification($type = 'main') |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $property |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getNotEmptyProperty($property) |
||
| 152 | |||
| 153 | public function getModificationUrl($absolute = false) |
||
| 159 | } |
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.