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 |
||
| 13 | class Email extends AbstractConstraint |
||
| 14 | { |
||
| 15 | use UtilContainerAwareTrait; |
||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * Check email domain through dns |
||
| 20 | * |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected $dnsCheck = false; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | protected $messageTemplates = [ |
||
| 30 | 'default' => 'The input should be valid email address' |
||
| 31 | ]; |
||
| 32 | |||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | * |
||
| 37 | * @link http://www.linuxjournal.com/article/9585 |
||
| 38 | */ |
||
| 39 | protected function doValidate($value) |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Setter of $dnsCheck |
||
| 71 | * |
||
| 72 | * @param boolean $dnsCheck |
||
| 73 | * @return static |
||
| 74 | */ |
||
| 75 | public function setDnsCheck($dnsCheck) |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $domain |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | protected function validateDomainPart($domain) |
||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $local |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | protected function validateLocalPart($local) |
||
| 145 | } |
||
| 146 |
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.