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 |
||
32 | class IntString extends Object implements IEquatable, IComparable |
||
33 | { |
||
34 | /** |
||
35 | * @param int|null $intValue Integer part. Default: ``0`` (zero). |
||
36 | * @param string|null $stringValue String part. Default: ``''`` (empty). |
||
37 | */ |
||
38 | 49 | public function __construct($intValue = 0, $stringValue = '') |
|
39 | { |
||
40 | 49 | unset($this->IntValue, $this->StringValue); |
|
41 | |||
42 | 49 | View Code Duplication | if (!(is_integer($intValue) || $intValue === null)) { |
|
|||
43 | $args = [ |
||
44 | 'position' => '1st', |
||
45 | 'expected' => typeof(0).'" or "'.typeof(null), |
||
46 | 'actual' => typeof($intValue), |
||
47 | ]; |
||
48 | |||
49 | $msg = msg('Invalid argument type.'); |
||
50 | $msg .= msg( |
||
51 | ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', |
||
52 | $args |
||
53 | ); |
||
54 | |||
55 | throw new InvalidArgumentException($msg); |
||
56 | } |
||
57 | |||
58 | 49 | View Code Duplication | if (!typeof($stringValue)->canBeString()) { |
59 | $args = [ |
||
60 | 2 | 'position' => '2nd', |
|
61 | 2 | 'expected' => typeof('string').'", "'.typeof(null).'" or "any object convertible to string', |
|
62 | 2 | 'actual' => typeof($stringValue), |
|
63 | ]; |
||
64 | |||
65 | 2 | $msg = msg('Invalid argument type.'); |
|
66 | 2 | $msg .= msg( |
|
67 | 2 | ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', |
|
68 | 2 | $args |
|
69 | ); |
||
70 | |||
71 | 2 | throw new InvalidArgumentException($msg); |
|
72 | } |
||
73 | |||
74 | 47 | $this->intValue = (integer) $intValue; |
|
75 | 47 | $this->stringValue = (string) $stringValue; |
|
76 | 47 | } |
|
77 | |||
78 | /** |
||
79 | * Convert the object to an instance of ``IntString``. |
||
80 | * |
||
81 | * @param string|IntString $obj Object to convert to ``IntString``. |
||
82 | * |
||
83 | * @return IntString |
||
84 | * @throws InvalidArgumentException if object is not a string or format is invalid. |
||
85 | */ |
||
86 | 36 | public static function parse($obj) |
|
87 | { |
||
88 | 36 | if ($obj instanceof IntString) { |
|
89 | return $obj; |
||
90 | } |
||
91 | |||
92 | 36 | if (is_integer($obj)) { |
|
93 | 19 | return new VersionComponent($obj); |
|
94 | } |
||
95 | |||
96 | try { |
||
97 | 18 | $intValue = (integer) Text::ensureIsString($obj); |
|
98 | 1 | } catch (InvalidArgumentException $e) { |
|
99 | $args = [ |
||
100 | 1 | 'position' => '1st', |
|
101 | 1 | 'expected' => 'string" or "integer', |
|
102 | 1 | 'actual' => typeof($obj), |
|
103 | ]; |
||
104 | |||
105 | 1 | $msg = msg('Invalid argument type.'); |
|
106 | 1 | $msg .= msg( |
|
107 | 1 | ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', |
|
108 | 1 | $args |
|
109 | ); |
||
110 | |||
111 | 1 | throw new InvalidArgumentException($msg, 1, $e); |
|
112 | } |
||
113 | |||
114 | 17 | $stringValue = ltrim($obj, "$intValue"); |
|
115 | |||
116 | // Validate that 0 (zero) is not interpreted as '' (empty string) |
||
117 | 17 | if ($stringValue === $obj) { |
|
118 | 3 | $msg = msg('Invalid argument value.'); |
|
119 | 3 | $msg .= msg(' "{0}" (string) must to start with an integer.', $obj); |
|
120 | |||
121 | 3 | throw new InvalidArgumentException($msg); |
|
122 | } |
||
123 | |||
124 | 14 | return new IntString($intValue, $stringValue); |
|
125 | } |
||
126 | |||
127 | protected $intValue; |
||
128 | protected $stringValue; |
||
129 | |||
130 | /** |
||
131 | * @var int |
||
132 | */ |
||
133 | public $IntValue; |
||
134 | 14 | public function getIntValue() |
|
138 | |||
139 | /** |
||
140 | * @var string |
||
141 | */ |
||
142 | public $StringValue; |
||
143 | 100 | public function getStringValue() |
|
147 | |||
148 | 30 | public function toString() |
|
152 | |||
153 | 15 | public function equals($other) |
|
154 | { |
||
165 | |||
166 | |||
167 | /** |
||
168 | * Determina la posición relativa de esta instancia con respecto al |
||
169 | * objeto especificado. |
||
170 | * Nota: Cualquier objeto que no sea instancia de IntString se |
||
171 | * considerará menor. |
||
172 | * |
||
173 | * @param IntString|mixed $other Objeto con el que se va a comparar. |
||
174 | * |
||
175 | * @return int Cero (0), si esta instancia es igual a $other; mayor |
||
176 | * a cero (>0), si es mayor a $other; menor a cero (<0), si es menor. |
||
177 | * */ |
||
178 | public function compareTo($other) |
||
196 | } |
||
197 |
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.