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 Cnpj implements ValidatorInterface |
||
7 | { |
||
8 | |||
9 | /** |
||
10 | * Returns true if and only if $value meets the validation requirements |
||
11 | * |
||
12 | * If $value fails validation, then this method returns false, and |
||
13 | * getMessages() will return an array of messages that explain why the |
||
14 | * validation failed. |
||
15 | * |
||
16 | * @param mixed $value |
||
17 | * @return bool |
||
18 | * @throws Exception\RuntimeException If validation of $value is impossible |
||
19 | */ |
||
20 | 2 | public function isValid($value) |
|
48 | |||
49 | /** |
||
50 | * Returns an array of messages that explain why the most recent isValid() |
||
51 | * call returned false. |
||
52 | * The array keys are validation failure message identifiers, |
||
53 | * and the array values are the corresponding human-readable message strings. |
||
54 | * |
||
55 | * If isValid() was never called or if the most recent isValid() call |
||
56 | * returned true, then this method returns an empty array. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getMessages() |
||
64 | } |
||
65 |
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.