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 |
||
6 | class UseSortFixer |
||
7 | { |
||
8 | /** |
||
9 | * @var int |
||
10 | * |
||
11 | * Sort type Alphabetic |
||
12 | */ |
||
13 | const SORT_TYPE_ALPHABETIC = 'alph'; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | * |
||
18 | * Sort type length |
||
19 | */ |
||
20 | const SORT_TYPE_LENGTH = 'length'; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | * |
||
25 | * Sort direction ascendent |
||
26 | */ |
||
27 | const SORT_DIRECTION_ASC = 'asc'; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | * |
||
32 | * Sort direction descendent |
||
33 | */ |
||
34 | const SORT_DIRECTION_DESC = 'desc'; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | * |
||
39 | * Group type one USE |
||
40 | */ |
||
41 | const GROUP_TYPE_ONE = 'one'; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | * |
||
46 | * Group type each USE |
||
47 | */ |
||
48 | const GROUP_TYPE_EACH = 'each'; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | * |
||
53 | * Groups |
||
54 | */ |
||
55 | private $groups = []; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | * |
||
60 | * Sort type |
||
61 | */ |
||
62 | private $sortType = self::SORT_TYPE_ALPHABETIC; |
||
63 | |||
64 | /** |
||
65 | * @var int |
||
66 | * |
||
67 | * Sort direction |
||
68 | */ |
||
69 | private $sortDirection = self::SORT_DIRECTION_ASC; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | * |
||
74 | * Group type |
||
75 | */ |
||
76 | private $groupType = self::GROUP_TYPE_EACH; |
||
77 | |||
78 | /** |
||
79 | * @var bool |
||
80 | * |
||
81 | * Skip empty groups |
||
82 | */ |
||
83 | private $groupSkipEmpty = false; |
||
84 | |||
85 | /** |
||
86 | * Sets Groups. |
||
87 | * |
||
88 | * @param array $groups |
||
89 | * |
||
90 | * @return UseSortFixer |
||
91 | */ |
||
92 | public function setGroups($groups) |
||
98 | |||
99 | /** |
||
100 | * Sets SortDirection. |
||
101 | * |
||
102 | * @param mixed $sortDirection |
||
103 | * |
||
104 | * @return UseSortFixer |
||
105 | */ |
||
106 | public function setSortDirection($sortDirection) |
||
112 | |||
113 | /** |
||
114 | * Sets SortType. |
||
115 | * |
||
116 | * @param mixed $sortType |
||
117 | * |
||
118 | * @return UseSortFixer |
||
119 | */ |
||
120 | 5 | public function setSortType($sortType) |
|
126 | |||
127 | /** |
||
128 | * Sets GroupType. |
||
129 | * |
||
130 | * @param int $groupType |
||
131 | * |
||
132 | * @return UseSortFixer |
||
133 | */ |
||
134 | public function setGroupType($groupType) |
||
140 | |||
141 | /** |
||
142 | * Sets GroupSkipEmpty. |
||
143 | * |
||
144 | * @param bool $groupSkipEmpty |
||
145 | * |
||
146 | * @return UseSortFixer |
||
147 | */ |
||
148 | public function setGroupSkipEmpty($groupSkipEmpty) |
||
154 | |||
155 | /** |
||
156 | * Do the fix. Return the fixed code or false if the code has not changed. |
||
157 | * |
||
158 | * @param string $data |
||
159 | * |
||
160 | * @return string|false |
||
161 | */ |
||
162 | 5 | public function fix($data) |
|
232 | |||
233 | /** |
||
234 | * Create blocks. |
||
235 | * |
||
236 | * @param array $namespaces |
||
237 | * |
||
238 | * @return array Groups |
||
239 | */ |
||
240 | 4 | private function createGroups(array $namespaces) |
|
275 | |||
276 | /** |
||
277 | * Sort a group. |
||
278 | * |
||
279 | * @param array $group |
||
280 | * |
||
281 | * @return array $group Sorted |
||
282 | */ |
||
283 | 4 | private function sortGroup(array $group) |
|
317 | |||
318 | /** |
||
319 | * Render a group. |
||
320 | * |
||
321 | * @param array $group |
||
322 | * |
||
323 | * @return string Group rendered |
||
324 | */ |
||
325 | 4 | private function renderGroup(array $group) |
|
340 | } |
||
341 |