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 namespace Propaganistas\LaravelIntl; |
||
9 | class Number extends Intl |
||
10 | { |
||
11 | /** |
||
12 | * @var \CommerceGuys\Intl\NumberFormat\NumberFormatRepository |
||
13 | */ |
||
14 | protected $data; |
||
15 | |||
16 | /** |
||
17 | * Number constructor. |
||
18 | * |
||
19 | * @param \CommerceGuys\Intl\NumberFormat\NumberFormatRepository $data |
||
20 | */ |
||
21 | 27 | public function __construct(NumberFormatRepository $data) |
|
25 | |||
26 | /** |
||
27 | * Format an value in the given locale (or app locale if not specified). |
||
28 | * |
||
29 | * @param int|float $value |
||
30 | * @return string |
||
31 | */ |
||
32 | 12 | public function format($value) |
|
39 | |||
40 | /** |
||
41 | * Format an value as percents in the given locale (or app locale if not specified). |
||
42 | * |
||
43 | * @param int|float $value |
||
44 | * @return string |
||
45 | */ |
||
46 | 3 | public function percent($value) |
|
53 | |||
54 | /** |
||
55 | * Parse a localized number into native PHP format. |
||
56 | * |
||
57 | * @param string|int|float $value |
||
58 | * @return int|float |
||
59 | */ |
||
60 | 3 | View Code Duplication | public function parse($value) |
70 | |||
71 | /** |
||
72 | * Get a localized entry. |
||
73 | * |
||
74 | * @param string|null $code |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 21 | public function get($code = null) |
|
81 | |||
82 | /** |
||
83 | * Get a localized list of entries, keyed by their code. |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | 3 | public function all() |
|
92 | |||
93 | /** |
||
94 | * Get the 'digits' property of a given NumberFormat. |
||
95 | * |
||
96 | * @param \CommerceGuys\Intl\NumberFormat\NumberFormat $format |
||
97 | * @return array |
||
98 | */ |
||
99 | private function getDigitsOfFormat(NumberFormat $format) |
||
109 | } |
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.