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 | abstract class ValidatedValue |
||
14 | { |
||
15 | /** @var mixed */ |
||
16 | private $value; |
||
17 | |||
18 | /** |
||
19 | * ValidatedValue constructor. |
||
20 | * @param mixed $value |
||
21 | */ |
||
22 | 6 | public function __construct($value) |
|
27 | |||
28 | /** |
||
29 | * @return mixed |
||
30 | */ |
||
31 | 6 | public function getValue() |
|
35 | |||
36 | /** |
||
37 | * @param mixed $value |
||
38 | */ |
||
39 | 6 | public function setValue($value) |
|
43 | |||
44 | /** |
||
45 | * @return string |
||
46 | */ |
||
47 | 1 | public function __toString() |
|
51 | |||
52 | /** |
||
53 | * @param int $length |
||
54 | * @throws BpostInvalidLengthException |
||
55 | */ |
||
56 | 1 | public function validateLength($length) |
|
62 | |||
63 | /** |
||
64 | * @param array $allowedValues |
||
65 | * @throws BpostInvalidValueException |
||
66 | */ |
||
67 | 3 | public function validateChoice(array $allowedValues) |
|
73 | |||
74 | /** |
||
75 | * @param string $regexPattern |
||
76 | * @throws BpostInvalidPatternException |
||
77 | */ |
||
78 | 1 | public function validatePattern($regexPattern) |
|
84 | |||
85 | /** |
||
86 | * @throws BpostLogicException |
||
87 | */ |
||
88 | public abstract function validate(); |
||
89 | |||
90 | } |
||
91 |
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.