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 AccessFilter 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 AccessFilter, and based on these observations, apply Extract Interface, too.
1 | <?php /** AccessFilterMicro */ |
||
17 | class AccessFilter extends Filter |
||
18 | { |
||
19 | /** |
||
20 | * @inheritdoc |
||
21 | */ |
||
22 | public function pre(array $params) |
||
43 | |||
44 | /** |
||
45 | * Check one rule |
||
46 | * |
||
47 | * @access protected |
||
48 | * |
||
49 | * @param array $rule rule definition |
||
50 | * |
||
51 | * @return bool|null |
||
52 | */ |
||
53 | protected function checkRule(array $rule) |
||
67 | |||
68 | /** |
||
69 | * Match action |
||
70 | * |
||
71 | * @access protected |
||
72 | * |
||
73 | * @param array $rule rule definition |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | protected function matchAction($rule) |
||
89 | |||
90 | /** |
||
91 | * Match user |
||
92 | * |
||
93 | * @access protected |
||
94 | * @global Container |
||
95 | * |
||
96 | * @param array $rule rule definition |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | protected function matchUser($rule) |
||
136 | |||
137 | /** |
||
138 | * Match role |
||
139 | * |
||
140 | * @access protected |
||
141 | * @global Container |
||
142 | * |
||
143 | * @param array $rule rule definition |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | View Code Duplication | protected function matchRole($rule) |
|
165 | |||
166 | /** |
||
167 | * Match IP |
||
168 | * |
||
169 | * @access protected |
||
170 | * @global Container |
||
171 | * |
||
172 | * @param array $rule rule definition |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | protected function matchIP($rule) |
||
196 | |||
197 | /** |
||
198 | * Match verbose |
||
199 | * |
||
200 | * @access protected |
||
201 | * @global Container |
||
202 | * |
||
203 | * @param array $rule rule definition |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | View Code Duplication | protected function matchVerb($rule) |
|
227 | |||
228 | /** |
||
229 | * @inheritdoc |
||
230 | */ |
||
231 | public function post(array $params) |
||
235 | } |
||
236 |
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.