Complex classes like MethodPattern 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 MethodPattern, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class MethodPattern implements PatternInterface { |
||
16 | |||
17 | const OUTPUT_BODY = 0; |
||
18 | |||
19 | const OUTPUT_FULL = 1; |
||
20 | |||
21 | const OUTPUT_DOC_COMMENT = 2; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * @var QueryStrategy |
||
26 | */ |
||
27 | private $nameQuery; |
||
28 | |||
29 | /** |
||
30 | * @var callable[] |
||
31 | */ |
||
32 | private $modifierChecker = []; |
||
33 | |||
34 | /** |
||
35 | * @var callable |
||
36 | */ |
||
37 | private $bodyChecker; |
||
38 | |||
39 | /** |
||
40 | * @var callable |
||
41 | */ |
||
42 | private $docCommentChecker; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | private $outputType = self::OUTPUT_BODY; |
||
48 | |||
49 | /** |
||
50 | * @var ParametersPattern |
||
51 | */ |
||
52 | private $parametersPattern; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * |
||
57 | */ |
||
58 | 21 | public function __construct() { |
|
73 | |||
74 | |||
75 | /** |
||
76 | * @return $this |
||
77 | */ |
||
78 | 3 | public function outputFull() { |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @return $this |
||
86 | */ |
||
87 | 3 | public function outputBody() { |
|
91 | |||
92 | |||
93 | /** |
||
94 | * @return $this |
||
95 | */ |
||
96 | 3 | public function outputDocComment() { |
|
100 | |||
101 | |||
102 | /** |
||
103 | * @param string|QueryStrategy $name |
||
104 | * @return $this |
||
105 | */ |
||
106 | 21 | public function withName($name) { |
|
117 | |||
118 | |||
119 | /** |
||
120 | * @param callable $check |
||
121 | * @return $this |
||
122 | */ |
||
123 | 21 | public function withBody(callable $check) { |
|
127 | |||
128 | |||
129 | /** |
||
130 | * @param Collection $body |
||
131 | * @return bool |
||
132 | * @throws \Exception |
||
133 | */ |
||
134 | 17 | private function isValidBody(Collection $body) { |
|
143 | |||
144 | |||
145 | /** |
||
146 | * @param callable $check |
||
147 | * @return $this |
||
148 | */ |
||
149 | 21 | public function withDocComment(callable $check = null) { |
|
160 | |||
161 | |||
162 | /** |
||
163 | * Find functions without doc comments |
||
164 | */ |
||
165 | 3 | public function withoutDocComment() { |
|
172 | |||
173 | |||
174 | /** |
||
175 | * @param ParametersPattern $pattern |
||
176 | * @return $this |
||
177 | */ |
||
178 | 2 | public function withParameters(ParametersPattern $pattern) { |
|
182 | |||
183 | |||
184 | /** |
||
185 | * @return $this |
||
186 | */ |
||
187 | 21 | public function withAnyModifier() { |
|
194 | |||
195 | |||
196 | /** |
||
197 | * @param string $modifier |
||
198 | * @return $this |
||
199 | */ |
||
200 | 2 | public function withModifier($modifier) { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $modifier |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function withoutModifier($modifier) { |
||
221 | |||
222 | |||
223 | /** |
||
224 | * @param array $modifiers Array<String> |
||
225 | * @return bool |
||
226 | * @throws \Exception |
||
227 | */ |
||
228 | 17 | private function isValidModifiers(array $modifiers) { |
|
242 | |||
243 | |||
244 | /** |
||
245 | * @param QuerySequence $querySequence |
||
246 | * @return Collection|null |
||
247 | */ |
||
248 | 18 | public function __invoke(QuerySequence $querySequence) { |
|
345 | |||
346 | |||
347 | /** |
||
348 | * @param Token $token |
||
349 | * @return mixed |
||
350 | * @throws \Exception |
||
351 | */ |
||
352 | 17 | private function isValidDocComment(Token $token) { |
|
361 | |||
362 | |||
363 | /** |
||
364 | * @param Collection $parameters |
||
365 | * @return bool |
||
366 | */ |
||
367 | 17 | private function isValidParameters(Collection $parameters) { |
|
378 | |||
379 | |||
380 | } |