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 |
||
17 | class MethodPattern implements PatternInterface { |
||
18 | |||
19 | const OUTPUT_BODY = 0; |
||
20 | |||
21 | const OUTPUT_FULL = 1; |
||
22 | |||
23 | const OUTPUT_DOC_COMMENT = 2; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * @var QueryStrategy |
||
28 | */ |
||
29 | private $nameQuery; |
||
30 | |||
31 | /** |
||
32 | * @var callable[] |
||
33 | */ |
||
34 | private $modifierChecker = []; |
||
35 | |||
36 | /** |
||
37 | * @var callable |
||
38 | */ |
||
39 | private $bodyChecker; |
||
40 | |||
41 | /** |
||
42 | * @var callable |
||
43 | */ |
||
44 | private $docCommentChecker; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $outputType = self::OUTPUT_BODY; |
||
50 | |||
51 | /** |
||
52 | * @var ParametersPattern |
||
53 | */ |
||
54 | private $argumentsPattern; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * |
||
59 | */ |
||
60 | 27 | public function __construct() { |
|
61 | 27 | $this->withName(Strict::create()->valueLike('!.+!')); |
|
62 | 27 | $this->withAnyModifier(); |
|
63 | |||
64 | /** @noinspection PhpUnusedParameterInspection */ |
||
65 | $this->withBody(function (Collection $body) { |
||
66 | 24 | return true; |
|
67 | 27 | }); |
|
68 | |||
69 | /** @noinspection PhpUnusedParameterInspection */ |
||
70 | $this->withDocComment(function (Token $token) { |
||
71 | 24 | return true; |
|
72 | 27 | }); |
|
73 | |||
74 | 27 | } |
|
75 | |||
76 | |||
77 | /** |
||
78 | * @return $this |
||
79 | */ |
||
80 | 3 | public function outputFull() : self { |
|
81 | 3 | $this->outputType = self::OUTPUT_FULL; |
|
82 | 3 | return $this; |
|
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @return $this |
||
88 | */ |
||
89 | 3 | public function outputBody() : self { |
|
90 | 3 | $this->outputType = self::OUTPUT_BODY; |
|
91 | 3 | return $this; |
|
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * @return $this |
||
97 | */ |
||
98 | 3 | public function outputDocComment() : self { |
|
99 | 3 | $this->outputType = self::OUTPUT_DOC_COMMENT; |
|
100 | 3 | return $this; |
|
101 | } |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @param string|QueryStrategy $name |
||
106 | * @return $this |
||
107 | */ |
||
108 | 27 | public function withName($name) : self { |
|
109 | 27 | if (is_string($name)) { |
|
110 | 3 | $this->nameQuery = Strict::create()->valueIs($name); |
|
111 | 27 | } elseif ($name instanceof QueryStrategy) { |
|
112 | 27 | $this->nameQuery = $name; |
|
113 | } else { |
||
114 | 3 | throw new \InvalidArgumentException('Invalid name format. Expect string or Query'); |
|
115 | } |
||
116 | |||
117 | 27 | return $this; |
|
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * @param callable $check |
||
123 | * @return $this |
||
124 | */ |
||
125 | 27 | public function withBody(callable $check) : self { |
|
126 | 27 | $this->bodyChecker = $check; |
|
127 | 27 | return $this; |
|
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * @param Collection $body |
||
133 | * @return bool |
||
134 | * @throws \Exception |
||
135 | */ |
||
136 | 24 | private function isValidBody(Collection $body) : bool { |
|
137 | 24 | $checker = $this->bodyChecker; |
|
138 | 24 | return $checker($body); |
|
139 | } |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @param callable $check |
||
144 | * @return $this |
||
145 | */ |
||
146 | 27 | public function withDocComment(callable $check = null) : self { |
|
157 | |||
158 | |||
159 | /** |
||
160 | * Find functions without doc comments |
||
161 | */ |
||
162 | 3 | public function withoutDocComment() : self { |
|
163 | $this->docCommentChecker = function (Token $token) { |
||
164 | 3 | return $token->isValid() === false; |
|
165 | }; |
||
166 | |||
167 | 3 | return $this; |
|
168 | } |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @param ParametersPattern $pattern |
||
173 | * @return $this |
||
174 | */ |
||
175 | 3 | public function withParameters(ParametersPattern $pattern) : self { |
|
176 | 3 | $this->argumentsPattern = $pattern; |
|
177 | 3 | return $this; |
|
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @return $this |
||
183 | */ |
||
184 | 27 | public function withAnyModifier() : self { |
|
185 | 27 | $this->modifierChecker = []; |
|
186 | $this->modifierChecker[] = function () { |
||
187 | 24 | return true; |
|
188 | }; |
||
189 | 27 | return $this; |
|
190 | } |
||
191 | |||
192 | |||
193 | /** |
||
194 | * @param string $modifier |
||
195 | * @return $this |
||
196 | */ |
||
197 | 3 | public function withModifier(string $modifier) : self { |
|
198 | $this->modifierChecker[] = function ($allModifiers) use ($modifier) { |
||
199 | 3 | return in_array($modifier, $allModifiers); |
|
200 | }; |
||
201 | |||
202 | 3 | return $this; |
|
203 | } |
||
204 | |||
205 | |||
206 | /** |
||
207 | * @param string $modifier |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function withoutModifier(string $modifier) : self { |
||
211 | |||
212 | 3 | $this->modifierChecker[] = function ($allModifiers) use ($modifier) { |
|
213 | 3 | return !in_array($modifier, $allModifiers); |
|
214 | }; |
||
215 | |||
216 | 3 | return $this; |
|
217 | } |
||
218 | |||
219 | |||
220 | /** |
||
221 | * @param string[] $modifiers |
||
222 | * @return bool |
||
223 | * @throws \Exception |
||
224 | */ |
||
225 | 24 | private function isValidModifiers(array $modifiers) : bool { |
|
239 | |||
240 | |||
241 | /** |
||
242 | * @param QuerySequence $querySequence |
||
243 | * @return Collection|null |
||
244 | */ |
||
245 | 24 | public function __invoke(QuerySequence $querySequence) { |
|
246 | 24 | static $availableModifiers = [ |
|
247 | T_STATIC, |
||
248 | T_PRIVATE, |
||
249 | T_PUBLIC, |
||
250 | T_ABSTRACT, |
||
251 | T_FINAL, |
||
354 | |||
355 | |||
356 | /** |
||
357 | * @param Token $token |
||
358 | * @return boolean |
||
359 | * @throws \Exception |
||
360 | */ |
||
361 | 24 | private function isValidDocComment(Token $token) : bool { |
|
365 | |||
366 | |||
367 | /** |
||
368 | * @param Collection $parameters |
||
369 | * @return bool |
||
370 | */ |
||
371 | 24 | private function isValidArguments(Collection $parameters) : bool { |
|
380 | |||
381 | |||
382 | } |