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:
Complex classes like DecimalInfinite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DecimalInfinite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DecimalInfinite extends Decimal |
||
22 | { |
||
23 | /** |
||
24 | * @var DecimalInfinite |
||
25 | */ |
||
26 | protected static $infPositive = null; |
||
27 | |||
28 | /** |
||
29 | * @var DecimalInfinite |
||
30 | */ |
||
31 | protected static $infNegative = null; |
||
32 | |||
33 | /** |
||
34 | * @param float $value |
||
35 | * |
||
36 | * @return \Cubiche\Domain\System\DecimalInfinite |
||
37 | */ |
||
38 | public static function fromNative($value) |
||
48 | |||
49 | /** |
||
50 | * @return \Cubiche\Domain\System\DecimalInfinite |
||
51 | */ |
||
52 | public static function infPositive() |
||
60 | |||
61 | /** |
||
62 | * @return \Cubiche\Domain\System\DecimalInfinite |
||
63 | */ |
||
64 | public static function infNegative() |
||
72 | |||
73 | /** |
||
74 | * @param string $value |
||
75 | */ |
||
76 | protected function __construct($value) |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | View Code Duplication | public function add(Number $x) |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | public function addInteger(Integer $x) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function addReal(Real $x) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function addDecimal(Decimal $x, $scale = null) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | View Code Duplication | public function sub(Number $x) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function subInteger(Integer $x) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function subReal(Real $x) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function subDecimal(Decimal $x, $scale = null) |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function mult(Number $x) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function multInteger(Integer $x) |
||
187 | |||
188 | /** |
||
189 | * {@inheritdoc} |
||
190 | */ |
||
191 | public function multReal(Real $x) |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function multDecimal(Decimal $x, $scale = null) |
||
203 | |||
204 | protected function divSpecialCases(Number $x) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function div(Number $x) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function divInteger(Integer $x) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function divReal(Real $x) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function divDecimal(Decimal $x, $scale = null) |
||
250 | |||
251 | /** |
||
252 | * @param Number $x |
||
253 | * |
||
254 | * @throws \DomainException |
||
255 | * |
||
256 | * @return \Cubiche\Domain\System\DecimalInfinite |
||
257 | */ |
||
258 | View Code Duplication | protected function powSpecialCases(Number $x) |
|
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function powInteger(Integer $x) |
||
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | public function powReal(Real $x) |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function powDecimal(Decimal $x, $scale = null) |
||
317 | |||
318 | /** |
||
319 | * {@inheritdoc} |
||
320 | */ |
||
321 | public function sqrt($scale = null) |
||
325 | } |
||
326 |
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.