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 | ||
| 4 | trait LengthTrait | ||
| 5 | { | ||
| 6 | /** | ||
| 7 | * @Exclude | ||
| 8 | * @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero | ||
| 9 | */ | ||
| 10 | private $maxLength = null; | ||
| 11 | /** | ||
| 12 | * @Exclude | ||
| 13 | * @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero | ||
| 14 | */ | ||
| 15 | private $minLength = null; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * @param integer $value Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero | ||
| 19 | */ | ||
| 20 | protected function setLengthFacet($value) | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @param integer $value Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero | ||
| 28 | */ | ||
| 29 | protected function setMinLengthFacet($value) | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @param integer $value | ||
| 37 | */ | ||
| 38 | private function checkValidMinMaxLength($value, $min = 0) | ||
| 47 | |||
| 48 | /** | ||
| 49 | * @param integer $value Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero | ||
| 50 | */ | ||
| 51 | protected function setMaxLengthFacet($value) | ||
| 56 | |||
| 57 | private function checkLength($v) | ||
| 66 | |||
| 67 | View Code Duplication | private function checkMinLength($v) | |
| 77 | |||
| 78 | private function getLengthForValue($v) | ||
| 85 | |||
| 86 | View Code Duplication | private function checkMaxLength($v) | |
| 96 | } | ||
| 97 | 
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.