Complex classes like MethodDescriptor 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 MethodDescriptor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class MethodDescriptor extends DescriptorAbstract implements Interfaces\MethodInterface, Interfaces\VisibilityInterface |
||
20 | { |
||
21 | /** @var ClassDescriptor|InterfaceDescriptor|TraitDescriptor $parent */ |
||
22 | protected $parent; |
||
23 | |||
24 | /** @var bool $abstract */ |
||
25 | protected $abstract = false; |
||
26 | |||
27 | /** @var bool $final */ |
||
28 | protected $final = false; |
||
29 | |||
30 | /** @var bool $static */ |
||
31 | protected $static = false; |
||
32 | |||
33 | /** @var string $visibility */ |
||
34 | protected $visibility = 'public'; |
||
35 | |||
36 | /** @var Collection */ |
||
37 | protected $arguments; |
||
38 | |||
39 | /** @var Type */ |
||
40 | 1 | private $returnType; |
|
41 | |||
42 | 1 | /** |
|
43 | * Initializes the all properties representing a collection with a new Collection object. |
||
44 | 1 | */ |
|
45 | 1 | public function __construct() |
|
51 | |||
52 | /** |
||
53 | * @param ClassDescriptor|InterfaceDescriptor|TraitDescriptor $parent |
||
54 | */ |
||
55 | public function setParent($parent) |
||
66 | |||
67 | /** |
||
68 | * @return ClassDescriptor|InterfaceDescriptor|TraitDescriptor |
||
69 | */ |
||
70 | public function getParent() |
||
74 | |||
75 | 1 | /** |
|
76 | 1 | * {@inheritDoc} |
|
77 | */ |
||
78 | public function setAbstract($abstract) |
||
82 | |||
83 | 1 | /** |
|
84 | * {@inheritDoc} |
||
85 | */ |
||
86 | public function isAbstract() |
||
90 | |||
91 | 1 | /** |
|
92 | 1 | * {@inheritDoc} |
|
93 | */ |
||
94 | public function setFinal($final) |
||
98 | |||
99 | 1 | /** |
|
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | public function isFinal() |
||
106 | |||
107 | 1 | /** |
|
108 | 1 | * {@inheritDoc} |
|
109 | */ |
||
110 | public function setStatic($static) |
||
114 | |||
115 | 1 | /** |
|
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | public function isStatic() |
||
122 | |||
123 | 1 | /** |
|
124 | 1 | * {@inheritDoc} |
|
125 | */ |
||
126 | public function setVisibility($visibility) |
||
130 | |||
131 | 1 | /** |
|
132 | * {@inheritDoc} |
||
133 | */ |
||
134 | public function getVisibility() |
||
138 | |||
139 | 1 | /** |
|
140 | * {@inheritDoc} |
||
141 | */ |
||
142 | 1 | public function setArguments(Collection $arguments) |
|
149 | |||
150 | /** |
||
151 | * @param string $name |
||
152 | * @param ArgumentDescriptor $argument |
||
153 | */ |
||
154 | public function addArgument($name, ArgumentDescriptor $argument) |
||
159 | |||
160 | 1 | /** |
|
161 | * {@inheritDoc} |
||
162 | */ |
||
163 | public function getArguments() |
||
167 | |||
168 | /** |
||
169 | 1 | * @return ReturnDescriptor |
|
170 | */ |
||
171 | 1 | public function getResponse(): ReturnDescriptor |
|
187 | |||
188 | /** |
||
189 | 1 | * Returns the file associated with the parent class, interface or trait. |
|
190 | * |
||
191 | * @return FileDescriptor |
||
192 | 1 | */ |
|
193 | 1 | public function getFile() |
|
197 | 1 | ||
198 | 1 | /** |
|
199 | 1 | * @return Collection |
|
200 | */ |
||
201 | public function getReturn() |
||
216 | 1 | ||
217 | 1 | /** |
|
218 | 1 | * @return Collection |
|
219 | */ |
||
220 | public function getParam() |
||
235 | |||
236 | /** |
||
237 | * Returns the Method from which this method should inherit its information, if any. |
||
238 | * |
||
239 | * The inheritance scheme for a method is more complicated than for most elements; the following business rules |
||
240 | * apply: |
||
241 | * |
||
242 | * 1. if the parent class/interface extends another class or other interfaces (interfaces have multiple |
||
243 | * inheritance!) then: |
||
244 | * 1. Check each parent class/interface's parent if they have a method with the exact same name |
||
245 | * 2. if a method is found with the same name; return the first one encountered. |
||
246 | * 2. if the parent is a class and implements interfaces, check each interface for a method with the exact same |
||
247 | * name. If such a method is found, return the first hit. |
||
248 | * |
||
249 | * @return MethodDescriptor|null |
||
250 | */ |
||
251 | public function getInheritedElement() |
||
299 | |||
300 | /** |
||
301 | * Sets return type of this method. |
||
302 | * |
||
303 | * @param Type $returnType |
||
304 | */ |
||
305 | public function setReturnType(Type $returnType) |
||
309 | } |
||
310 |