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 |
||
30 | class VersionComponent extends IntString implements IEquatable |
||
31 | { |
||
32 | /** |
||
33 | * |
||
34 | * |
||
35 | * @param int|null $intValue |
||
36 | * @param string|null $stringValue |
||
37 | */ |
||
38 | 49 | public function __construct($intValue = null, $stringValue = null) |
|
39 | { |
||
40 | // Validates filters for only null or int/string value types. |
||
41 | 49 | parent::__construct($intValue, $stringValue); |
|
42 | |||
43 | 47 | if ($intValue === null) { |
|
44 | 22 | $this->intValue = $intValue; |
|
45 | |||
46 | // Ignore string value if intValue is null. |
||
47 | 22 | $this->stringValue = ''; |
|
48 | } else { |
||
49 | // Validation of values |
||
50 | 38 | View Code Duplication | if ($this->IntValue < 0) { |
|
|||
51 | $args = [ |
||
52 | 5 | 'position' => '1st', |
|
53 | 5 | 'actual' => $intValue, |
|
54 | ]; |
||
55 | |||
56 | 5 | $msg = msg('Invalid argument value.'); |
|
57 | 5 | $msg .= msg( |
|
58 | 5 | ' {position} argument must to be a positive number; "{actual}" given.', |
|
59 | 5 | $args |
|
60 | ); |
||
61 | |||
62 | 5 | throw new InvalidArgumentException($msg); |
|
63 | } // Integer is valid |
||
64 | |||
65 | 34 | if ($stringValue !== null) { |
|
66 | 33 | if ($this->StringValue != '') { |
|
67 | 15 | $pattern = '~^([a-z])$~'; // 1 char |
|
68 | |||
69 | 15 | if (strlen($this->StringValue) > 1) { |
|
70 | 15 | $start = '~^([a-z]|-)'; |
|
71 | 15 | $middle = '([a-z]|[0-9]|-)*'; |
|
72 | 15 | $end = '([a-z]|[0-9])$~'; |
|
73 | |||
74 | 15 | $pattern = $start.$middle.$end; |
|
75 | } |
||
76 | |||
77 | 15 | $correct = (boolean) preg_match($pattern, $this->StringValue); |
|
78 | |||
79 | 15 | if ($correct) { |
|
80 | //Último chequeo: que no hayan 2 '-' consecutivos. |
||
81 | 12 | $correct = strpos($this->StringValue, '--') == false ? true : false; |
|
82 | } |
||
83 | |||
84 | 15 | View Code Duplication | if (!$correct) { |
85 | $args = [ |
||
86 | 4 | 'position' => '2nd', |
|
87 | 4 | 'actual' => $stringValue, |
|
88 | ]; |
||
89 | |||
90 | 4 | $msg = msg('Invalid argument value.'); |
|
91 | 4 | $msg .= msg( |
|
92 | 4 | ' {position} parameter has invalid chars; "{actual}" given.', |
|
93 | 4 | $args |
|
94 | ); |
||
95 | |||
96 | 4 | throw new InvalidArgumentException($msg); |
|
97 | } |
||
98 | } |
||
99 | } // String is valid |
||
100 | } |
||
101 | 40 | } |
|
102 | |||
103 | 43 | public static function parse($obj) |
|
117 | |||
118 | /** |
||
119 | * Determina si este componente tiene los valores predeterminados (0). |
||
120 | * |
||
121 | * @return boolean |
||
122 | * */ |
||
123 | 9 | public function isDefault() |
|
124 | { |
||
125 | 9 | if ($this->IntValue === 0) { |
|
126 | 1 | if ($this->StringValue === '') { |
|
127 | 1 | return true; |
|
128 | } |
||
129 | } |
||
130 | |||
131 | 8 | return false; |
|
132 | } |
||
133 | |||
134 | |||
135 | /** |
||
136 | * Getter method for VersionComponent::IntValue property. |
||
137 | * |
||
138 | * @return integer|NULL |
||
139 | * */ |
||
140 | 126 | public function getIntValue() |
|
144 | |||
145 | |||
146 | /** |
||
147 | * Determina si este componente NO tiene los valores predeterminados. |
||
148 | * |
||
149 | * @return boolean |
||
150 | * */ |
||
151 | 9 | public function isNotDefault() |
|
155 | |||
156 | /** |
||
157 | * Determina si esta instancia es nula. |
||
158 | * |
||
159 | * @return boolean |
||
160 | * */ |
||
161 | 51 | public function isNull() |
|
169 | |||
170 | /** |
||
171 | * Determina si esta instancia NO es nula. |
||
172 | * |
||
173 | * @return boolean |
||
174 | * */ |
||
175 | 38 | public function isNotNull() |
|
179 | |||
180 | 40 | public function equals($other) |
|
181 | { |
||
194 | |||
195 | 21 | public function compareTo($other) |
|
229 | } |
||
230 |
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.