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 | final class Validator |
||
14 | { |
||
15 | /** |
||
16 | * @var ObjectFacade |
||
17 | */ |
||
18 | private $facade; |
||
19 | |||
20 | /** |
||
21 | * Validator constructor. |
||
22 | * |
||
23 | * @param ObjectFacade $facade |
||
24 | */ |
||
25 | public function __construct(ObjectFacade $facade) |
||
29 | |||
30 | /** |
||
31 | * @param ObjectFacade $facade |
||
32 | * |
||
33 | * @return Validator |
||
34 | */ |
||
35 | public static function new(ObjectFacade $facade): self |
||
39 | |||
40 | /** |
||
41 | * @param ReflectionProperty $property |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | View Code Duplication | public function isValidProperty(ReflectionProperty $property): bool |
|
|
|||
46 | { |
||
47 | if (!$property->isPublic()) { |
||
48 | debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is not public', $property->getName()); |
||
49 | |||
50 | return false; |
||
51 | } |
||
52 | |||
53 | if ($property->isStatic()) { |
||
54 | debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Property %s is static', $property->getName()); |
||
55 | |||
56 | return false; |
||
57 | } |
||
58 | |||
59 | return true; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param ReflectionMethod $method |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | View Code Duplication | public function isValidMethod(ReflectionMethod $method): bool |
|
68 | { |
||
69 | if (!$method->isPublic()) { |
||
70 | debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is not public', $method->getName()); |
||
71 | |||
72 | return false; |
||
73 | } |
||
74 | |||
75 | if ($method->isStatic()) { |
||
76 | debug(ObjectFacade::DEBUG_LABEL)->output('[Error] Method %s is static', $method->getName()); |
||
77 | |||
78 | return false; |
||
79 | } |
||
80 | |||
81 | return true; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param ReflectionMethod $method |
||
86 | * @param $value |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function isValidSetterMethod(ReflectionMethod $method, $value): bool |
||
91 | { |
||
92 | if (!$this->isValidMethod($method)) { |
||
93 | return false; |
||
94 | } |
||
95 | |||
96 | View Code Duplication | if ($value === null && $method->getNumberOfParameters() !== 0 && !$method->getParameters()[0]->allowsNull()) { |
|
97 | debug(ObjectFacade::DEBUG_LABEL)->output('[Error] First parameter of method %s is not allowed to be null', $method->getName()); |
||
98 | |||
99 | return false; |
||
100 | } |
||
101 | |||
102 | if ($method->getNumberOfParameters() === 0) { |
||
103 | debug(ObjectFacade::DEBUG_LABEL)->output('[Warning] Method %s does not accept any parameters', $method->getName()); |
||
104 | } |
||
105 | |||
106 | return true; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param ReflectionMethod $method |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function validateGetterMethod(ReflectionMethod $method): bool |
||
129 | } |
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.