Complex classes like Method 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 Method, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class Method extends ElementAbstract implements MethodInterface |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * Method's name |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $name; |
||
32 | |||
33 | /** |
||
34 | * Method's arguments. |
||
35 | * @var ArgumentCollection |
||
36 | */ |
||
37 | protected $arguments; |
||
38 | |||
39 | /** |
||
40 | * Method's code. |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $code; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $visibility = Visibility::TYPE_PUBLIC; |
||
49 | |||
50 | /** |
||
51 | * Sets like a final. |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $isFinal; |
||
55 | |||
56 | /** |
||
57 | * Sets like an abstract. |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $isAbstract; |
||
61 | |||
62 | /** |
||
63 | * Sets like an interface. |
||
64 | * @var bool |
||
65 | */ |
||
66 | protected $isInterface; |
||
67 | |||
68 | /** |
||
69 | * Sets like a static. |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $isStatic; |
||
73 | |||
74 | /** |
||
75 | * Documentation Block. |
||
76 | * @var DocBlockInterface |
||
77 | */ |
||
78 | protected $docBlock; |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 32 | public function init() |
|
89 | |||
90 | /** |
||
91 | * @return PhpClass |
||
92 | */ |
||
93 | 1 | public function getParent() |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 13 | public function setParent(ElementInterface $parent) |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 24 | public function getName() |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | 26 | public function setName($name) |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 18 | public function getArgumentCollection() |
|
138 | |||
139 | /** |
||
140 | * @inheritdoc |
||
141 | */ |
||
142 | 8 | public function addArgument(ArgumentInterface $argument) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 32 | public function setArgumentCollection(ArgumentCollection $arguments) |
|
160 | |||
161 | /** |
||
162 | * @inheritdoc |
||
163 | * @return MethodInterface |
||
164 | */ |
||
165 | 7 | public function setDescription($description) |
|
171 | |||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | 1 | public function getDescription() |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 1 | public function setReturns(TagInterface $tag) |
|
190 | |||
191 | /** |
||
192 | * @inheritdoc |
||
193 | */ |
||
194 | 1 | public function getReturns() |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 12 | public function getCode() |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | 27 | public function setCode($code) |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 16 | public function getVisibility() |
|
224 | |||
225 | /** |
||
226 | * @inheritdoc |
||
227 | * @return Method |
||
228 | */ |
||
229 | 32 | public function setVisibility($visibility) |
|
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | 16 | public function isFinal() |
|
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | * @return Method |
||
248 | */ |
||
249 | 2 | public function setIsFinal($isFinal = true) |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 18 | public function isAbstract() |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | * @return Method |
||
267 | */ |
||
268 | 5 | public function setIsAbstract($isAbstract = true) |
|
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | 16 | public function isStatic() |
|
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | * @return Method |
||
290 | */ |
||
291 | 2 | public function setIsStatic($isStatic = true) |
|
297 | |||
298 | /** |
||
299 | * @inheritdoc |
||
300 | */ |
||
301 | 18 | public function isInterface() |
|
305 | |||
306 | /** |
||
307 | * @inheritdoc |
||
308 | * @return Method |
||
309 | */ |
||
310 | 4 | public function setIsInterface($isInterface = true) |
|
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | 17 | public function getDocBlock() |
|
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | * @return Method |
||
332 | */ |
||
333 | 32 | public function setDocBlock(DocBlockInterface $docBlock) |
|
339 | |||
340 | 11 | protected function codeToString() |
|
352 | |||
353 | /** |
||
354 | * @inheritdoc |
||
355 | */ |
||
356 | 16 | public function toString() |
|
374 | |||
375 | /** |
||
376 | * Get string with method type. |
||
377 | * @return string |
||
378 | */ |
||
379 | 16 | protected function toStringFunction() |
|
389 | |||
390 | /** |
||
391 | * Get string code. |
||
392 | * @return string |
||
393 | */ |
||
394 | 16 | public function toStringCode() |
|
409 | |||
410 | /** |
||
411 | * Create a Get Method from Property of Class. |
||
412 | * |
||
413 | * @param PropertyInterface $property |
||
414 | * |
||
415 | * @return Method |
||
416 | */ |
||
417 | 1 | public static function createGetterFromProperty(PropertyInterface $property) |
|
425 | |||
426 | /** |
||
427 | * Generate Set Method from Property. |
||
428 | * Add a set method in the class based on Object Property. |
||
429 | * |
||
430 | * @param PropertyInterface $property |
||
431 | * |
||
432 | * @return Method |
||
433 | */ |
||
434 | 1 | public static function createSetterFromProperty(PropertyInterface $property) |
|
450 | |||
451 | /** |
||
452 | * Creates a method from Reflection Method. |
||
453 | * |
||
454 | * @param \ReflectionMethod $reflected |
||
455 | * |
||
456 | * @return Method |
||
457 | */ |
||
458 | 4 | public static function createFromReflection(\ReflectionMethod $reflected) |
|
471 | } |
||
472 |