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 |
||
7 | class RulesParser implements Arrayable |
||
8 | { |
||
9 | /** |
||
10 | * The set of rules. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $rules; |
||
15 | |||
16 | /** |
||
17 | * Create new instance. |
||
18 | * |
||
19 | * @param string|null $rules |
||
20 | */ |
||
21 | public function __construct($rules = null) |
||
25 | |||
26 | /** |
||
27 | * Convert string migration to array. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | public function toArray() |
||
35 | |||
36 | /** |
||
37 | * Parse a string to array of formatted rules. |
||
38 | * |
||
39 | * @param string $rules |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | View Code Duplication | public function parse($rules) |
|
55 | |||
56 | /** |
||
57 | * Get array of rules. |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function getRules() |
||
69 | |||
70 | /** |
||
71 | * Get column name from rules. |
||
72 | * |
||
73 | * @param string $rules |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getColumn($rules) |
||
83 | |||
84 | /** |
||
85 | * Get column attributes. |
||
86 | * |
||
87 | * @param string $column |
||
88 | * @param string $rules |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getAttributes($column, $rules) |
||
96 | } |
||
97 |
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.