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 DocumentorService 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 DocumentorService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class DocumentorService extends Service |
||
13 | { |
||
14 | const DTO_INTERFACE = '\\PSFS\\base\\dto\\Dto'; |
||
15 | const MODEL_INTERFACE = '\\Propel\\Runtime\\ActiveRecord\\ActiveRecordInterface'; |
||
16 | /** |
||
17 | * @Inyectable |
||
18 | * @var \PSFS\base\Router route |
||
19 | */ |
||
20 | protected $route; |
||
21 | |||
22 | /** |
||
23 | * Method that extract all modules |
||
24 | * @return array |
||
25 | */ |
||
26 | public function getModules() |
||
44 | |||
45 | /** |
||
46 | * Method that extract all endpoints for each module |
||
47 | * |
||
48 | * @param string $module |
||
49 | * |
||
50 | * @return array |
||
51 | */ |
||
52 | public function extractApiEndpoints($module) |
||
70 | |||
71 | /** |
||
72 | * Method that extract all the endpoit information by reflection |
||
73 | * |
||
74 | * @param string $namespace |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | public function extractApiInfo($namespace) |
||
108 | |||
109 | /** |
||
110 | * Extract route from doc comments |
||
111 | * |
||
112 | * @param string $comments |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | protected function extractRoute($comments = '') |
||
123 | |||
124 | /** |
||
125 | * Extract method from doc comments |
||
126 | * |
||
127 | * @param string $comments |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | protected function extractMethod($comments = '') |
||
138 | |||
139 | /** |
||
140 | * Extract visibility from doc comments |
||
141 | * |
||
142 | * @param string $comments |
||
143 | * |
||
144 | * @return boolean |
||
145 | */ |
||
146 | protected function extractVisibility($comments = '') |
||
156 | |||
157 | /** |
||
158 | * Method that extract the description for the endpoint |
||
159 | * |
||
160 | * @param string $comments |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function extractDescription($comments = '') |
||
179 | |||
180 | /** |
||
181 | * Method that extract the type of a variable |
||
182 | * @param string $comments |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | protected function extractVarType($comments = '') |
||
196 | |||
197 | /** |
||
198 | * Method that extract the payload for the endpoint |
||
199 | * |
||
200 | * @param string $model |
||
201 | * @param string $comments |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | protected function extractPayload($model, $comments = '') |
||
215 | |||
216 | /** |
||
217 | * Extract return class for api endpoint |
||
218 | * @param string $model |
||
219 | * @param string $comments |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function extractReturn($model, $comments = '') |
||
258 | |||
259 | /** |
||
260 | * Extract all fields from a ActiveResource model |
||
261 | * @param string $namespace |
||
262 | * |
||
263 | * @return mixed |
||
264 | */ |
||
265 | protected function extractModelFields($namespace) |
||
288 | } |
||
289 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.