Complex classes like ApiContext 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 ApiContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class ApiContext extends BaseContext implements Context |
||
34 | { |
||
35 | /** |
||
36 | * @Then /^the AST has a class named "([^"]*)" in file "([^"]*)"$/ |
||
37 | * @throws \Exception |
||
38 | */ |
||
39 | public function theASTHasAclassNamedInFile($class, $file) |
||
40 | { |
||
41 | $ast = $this->getAst(); |
||
42 | |||
43 | $file = $this->processFilePath($file); |
||
44 | /** @var FileDescriptor $fileDescriptor */ |
||
45 | $fileDescriptor = $ast->getFiles()->get($file); |
||
46 | |||
47 | /** @var ClassDescriptor $classDescriptor */ |
||
48 | foreach ($fileDescriptor->getClasses() as $classDescriptor) { |
||
49 | if ($classDescriptor->getName() === $class) { |
||
50 | return; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | throw new \Exception(sprintf('Didn\'t find expected class "%s" in "%s"', $class, $file)); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @Then /^the AST doesn't have a class "([^"]*)"$/ |
||
59 | * @throws \Exception |
||
60 | */ |
||
61 | public function theASTDoesnTHaveAClass($className) |
||
62 | { |
||
63 | $ast = $this->getAst(); |
||
64 | foreach ($ast->getFiles() as $file) { |
||
65 | foreach ($file->getClasses() as $classDescriptor) { |
||
66 | if ($classDescriptor->getName() === $className) { |
||
67 | throw new \Exception('Found unexpected class'); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @Then /^the class named "([^"]*)" is in the default package$/ |
||
75 | * @throws \Exception |
||
76 | */ |
||
77 | public function theASTHasAClassInDefaultPackage($class) |
||
78 | { |
||
79 | $class = $this->findClassByName($class); |
||
80 | |||
81 | Assert::assertEquals('Default', $class->getPackage()->getName()); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @Then /^the AST has a trait named "([^"]*)" in file "([^"]*)"$/ |
||
86 | * @throws \Exception |
||
87 | */ |
||
88 | public function theASTHasATraitNamedInFile($trait, $file) |
||
89 | { |
||
90 | $ast = $this->getAst(); |
||
91 | |||
92 | $file = $this->processFilePath($file); |
||
93 | /** @var FileDescriptor $fileDescriptor */ |
||
94 | $fileDescriptor = $ast->getFiles()->get($file); |
||
95 | |||
96 | /** @var TraitDescriptor $classDescriptor */ |
||
97 | foreach ($fileDescriptor->getTraits() as $classDescriptor) { |
||
98 | if ($classDescriptor->getName() === $trait) { |
||
99 | return; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | throw new \Exception(sprintf('Didn\'t find expected trait "%s" in "%s"', $trait, $file)); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @Then the class named ":class" has docblock with content: |
||
108 | */ |
||
109 | public function classHasDocblockWithContent($class, PyStringNode $expectedContent) |
||
110 | { |
||
111 | $class = $this->findClassByName($class); |
||
112 | |||
113 | Assert::assertEquals($expectedContent->getRaw(), $class->getDescription()); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @Then class ":classFqsen" has :docElement: |
||
118 | * @throws Exception |
||
119 | */ |
||
120 | public function classHasDocblockContent($classFqsen, $docElement, PyStringNode $value) |
||
121 | { |
||
122 | $class = $this->findClassByFqsen($classFqsen); |
||
123 | |||
124 | $method = 'get' . $docElement; |
||
125 | |||
126 | Assert::assertEquals($value->getRaw(), $class->{$method}()); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @Then class ":classFqsen" has :elementType :elementName with :docElement: |
||
131 | */ |
||
132 | public function classHasElementWithDocblockContent($classFqsen, $elementType, $elementName, $docElement, PyStringNode $value) |
||
133 | { |
||
134 | $class = $this->findClassByFqsen($classFqsen); |
||
135 | |||
136 | switch ($elementType) { |
||
137 | case 'method': |
||
138 | case 'constant': |
||
139 | $method = $method = 'get' . $elementType . 's'; |
||
140 | break; |
||
141 | case 'property': |
||
142 | $method = 'getProperties'; |
||
143 | break; |
||
144 | default: |
||
145 | $method = 'get' . $elementType; |
||
146 | break; |
||
147 | } |
||
148 | |||
149 | $element = $class-> {$method}()->get($elementName); |
||
150 | $method = 'get' . $docElement; |
||
151 | $actual = $element->{$method}(); |
||
152 | |||
153 | Assert::assertEquals($value->getRaw(), $actual, sprintf('"%s" does not match "%s"', $actual, $value->getRaw())); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @Then class ":classFqsen" has version :value |
||
158 | */ |
||
159 | public function classHasVersion($classFqsen, $value) |
||
160 | { |
||
161 | $class = $this->findClassByFqsen($classFqsen); |
||
162 | |||
163 | /** @var VersionDescriptor $tag */ |
||
164 | foreach ($class->getVersion() as $tag) { |
||
165 | if ($tag->getVersion() === $value) { |
||
166 | return; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | Assert::fail(sprintf('Didn\'t find expected version "%s"', $value)); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @Then class ":classFqsen" without tag :tagName |
||
175 | */ |
||
176 | public function classWithoutTag($classFqsen, $tagName) |
||
177 | { |
||
178 | $this->classHasTag($classFqsen, $tagName, 0); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param string $classFqsen |
||
183 | * @param string $tagName |
||
184 | * @param int $expectedNumber |
||
185 | * @Then class ":classFqsen" has exactly :expectedNumber tag :tagName |
||
186 | */ |
||
187 | public function classHasTag($classFqsen, $tagName, $expectedNumber) |
||
188 | { |
||
189 | $class = $this->findClassByFqsen($classFqsen); |
||
190 | static::AssertTagCount($class, $tagName, $expectedNumber); |
||
|
|||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $classFqsen |
||
195 | * @param string $tagName |
||
196 | * @param string $method |
||
197 | * @Then class ":classFqsen" has a method named :method without tag :tagName |
||
198 | */ |
||
199 | public function classHasMethodWithoutTag($classFqsen, $tagName, $method) |
||
200 | { |
||
201 | $this->classHasMethodWithExpectedCountTag($classFqsen, $tagName, $method, 0); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param string $classFqsen |
||
206 | * @param string $tagName |
||
207 | * @param string $methodName |
||
208 | * @Then class ":classFqsen" has a method named :method with exactly :expected tag :tagName |
||
209 | */ |
||
210 | public function classHasMethodWithExpectedCountTag($classFqsen, $tagName, $methodName, $expectedCount) |
||
211 | { |
||
212 | $class = $this->findClassByFqsen($classFqsen); |
||
213 | $method = $class->getMethods()->get($methodName); |
||
214 | |||
215 | static::AssertTagCount($method, $tagName, $expectedCount); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $classFqsen |
||
220 | * @param string $methodName |
||
221 | * @Then class ":classFqsen" has a method :method with argument ":argument is variadic |
||
222 | */ |
||
223 | public function classHasMethodWithAgumentVariadic($classFqsen, $methodName, $argument) |
||
235 | |||
236 | /** |
||
237 | * @param string $classFqsen |
||
238 | * @param string $methodName |
||
239 | * @Then class ":classFqsen" has a method :method |
||
240 | */ |
||
241 | public function classHasMethod($classFqsen, $methodName) |
||
242 | { |
||
243 | $class = $this->findClassByFqsen($classFqsen); |
||
244 | /** @var MethodDescriptor $method */ |
||
245 | $method = $class->getMethods()->get($methodName, null); |
||
246 | Assert::assertInstanceOf(MethodDescriptor::class, $method); |
||
247 | Assert::assertEquals($methodName, $method->getName()); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $classFqsen |
||
252 | * @param string $methodName |
||
253 | * @param string $argument |
||
254 | * @param string $type |
||
255 | * @Then class ":classFqsen" has a method :method with argument :argument of type ":type" |
||
256 | */ |
||
257 | public function classHasMethodWithAgumentOfType($classFqsen, $methodName, $argument, $type) |
||
258 | { |
||
259 | $class = $this->findClassByFqsen($classFqsen); |
||
260 | /** @var MethodDescriptor $method */ |
||
261 | $method = $class->getMethods()->get($methodName); |
||
262 | Assert::assertArrayHasKey($argument, $method->getArguments()); |
||
263 | /** @var ArgumentDescriptor $argumentDescriptor */ |
||
264 | $argumentDescriptor = $method->getArguments()[$argument]; |
||
265 | |||
266 | Assert::assertEquals($type, (string) $argumentDescriptor->getTypes()); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $classFqsen |
||
271 | * @param string $methodName |
||
272 | * @param string $param |
||
273 | * @param string $type |
||
274 | * @Then class ":classFqsen" has a method :method with param :param of type ":type" |
||
275 | */ |
||
276 | public function classHasMethodWithParamOfType($classFqsen, $methodName, $param, $type) |
||
277 | { |
||
278 | $class = $this->findClassByFqsen($classFqsen); |
||
279 | /** @var MethodDescriptor $method */ |
||
280 | $method = $class->getMethods()->get($methodName); |
||
281 | /** @var ParamDescriptor $paramDescriptor */ |
||
282 | foreach ($method->getParam() as $paramDescriptor) { |
||
283 | if ($paramDescriptor->getName() === $param) { |
||
284 | Assert::assertEquals($type, (string) $paramDescriptor->getTypes()); |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * @param string $classFqsen |
||
291 | * @param string $constantName |
||
292 | * @Then class ":classFqsen" has a constant :constantName |
||
293 | */ |
||
294 | public function classHasConstant($classFqsen, $constantName) |
||
295 | { |
||
296 | /** @var ClassDescriptor $class */ |
||
297 | $class = $this->findClassByFqsen($classFqsen); |
||
298 | $constant = $class->getConstants()->get($constantName); |
||
299 | Assert::assertInstanceOf(ConstantDescriptor::class, $constant); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $className |
||
304 | * @return ClassDescriptor |
||
305 | * @throws \Exception |
||
306 | */ |
||
307 | private function findClassByName($className) |
||
308 | { |
||
309 | $ast = $this->getAst(); |
||
310 | foreach ($ast->getFiles() as $file) { |
||
311 | foreach ($file->getClasses() as $classDescriptor) { |
||
312 | if ($classDescriptor->getName() === $className) { |
||
313 | return $classDescriptor; |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | throw new \Exception(sprintf('Didn\'t find expected class "%s"', $className)); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param string $tagName |
||
323 | * @param int $expectedNumber |
||
324 | * @param DescriptorAbstract $element |
||
325 | */ |
||
326 | private static function AssertTagCount($element, $tagName, $expectedNumber) |
||
327 | { |
||
328 | /** @var Collection $tagCollection */ |
||
329 | $tagCollection = $element->getTags()->get($tagName, new Collection()); |
||
330 | |||
331 | Assert::assertEquals((int) $expectedNumber, $tagCollection->count()); |
||
332 | if ($expectedNumber > 0) { |
||
333 | Assert::assertEquals($tagName, $tagCollection[0]->getName()); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @Then /^the ast has a file named "([^"]*)" with a summary:$/ |
||
339 | * @throws \Exception |
||
340 | */ |
||
341 | public function theAstHasAFileNamedWithASummary(string $fileName, PyStringNode $string) |
||
342 | { |
||
343 | $ast = $this->getAst(); |
||
344 | /** @var FileDescriptor $file */ |
||
345 | $file = $ast->getFiles()->get($fileName); |
||
346 | |||
347 | Assert::assertEquals($string->getRaw(), $file->getSummary()); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param string $classFqsen |
||
352 | * @param string $methodName |
||
353 | * @throws Exception |
||
354 | * @Then class ":classFqsen" has a method :method with returntype :returnType |
||
355 | * @Then class ":classFqsen" has a method :method with returntype :returnType without description |
||
356 | */ |
||
357 | public function classHasMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
364 | |||
365 | /** |
||
366 | * @param string $classFqsen |
||
367 | * @param string $methodName |
||
368 | * @throws Exception |
||
369 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType |
||
370 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType without description |
||
371 | */ |
||
372 | public function classHasMagicMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
373 | { |
||
374 | $response = $this->findMagicMethodResponse($classFqsen, $methodName); |
||
375 | |||
376 | Assert::assertEquals($returnType, (string) $response->getTypes()); |
||
377 | Assert::assertEquals('', (string) $response->getDescription()); |
||
379 | |||
380 | /** |
||
381 | * @param string $classFqsen |
||
382 | * @param string $methodName |
||
383 | * @throws Exception |
||
384 | * @Then class ":classFqsen" has a method :method with returntype :returnType with description: |
||
385 | */ |
||
386 | public function classHasMethodWithReturnTypeAndDescription($classFqsen, $methodName, $returnType, PyStringNode $description) |
||
393 | |||
394 | /** |
||
395 | * @Then class ":classFqsen" has a method ":method" without returntype |
||
396 | * @throws \Exception |
||
397 | */ |
||
398 | public function classReturnTaggetReturnWithoutAnyWithoutReturntype($classFqsen, $methodName) |
||
404 | |||
405 | /** |
||
406 | * @throws Exception |
||
407 | * @Then has function :fqsen with returntype :returnType |
||
408 | * @Then has function :fqsen with returntype :returnType without description |
||
409 | */ |
||
410 | public function functionWithReturnType($fqsen, $returnType) |
||
417 | |||
418 | /** |
||
419 | * @throws Exception |
||
420 | * @Then has function :fqsen with returntype :returnType with description: |
||
421 | */ |
||
422 | public function functionWithReturnTypeAndDescription($fqsen, $returnType, PyStringNode $description) |
||
429 | |||
430 | /** |
||
431 | * @Then has function :fqsen without returntype |
||
432 | * @throws \Exception |
||
433 | */ |
||
434 | public function functionWithoutReturntype($fqsen) |
||
440 | |||
441 | /** |
||
442 | * @throws Exception |
||
443 | */ |
||
444 | private function findMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
445 | { |
||
446 | $class = $this->findClassByFqsen($classFqsen); |
||
454 | |||
455 | /** |
||
456 | * @throws Exception |
||
457 | */ |
||
458 | private function findMagicMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
475 | |||
476 | /** |
||
477 | * @throws Exception |
||
478 | */ |
||
479 | private function findFunctionResponse(string $fqsen): ReturnDescriptor |
||
484 | |||
485 | /** |
||
486 | * @Then class ":classFqsen" has a magic method :method with argument ":argument" of type :type |
||
487 | */ |
||
488 | public function classHasMagicMethodWithArgument($classFqsen, $methodName, $argument, $type) |
||
503 | |||
504 | /** |
||
505 | * @Then /^(\d+) files should be parsed$/ |
||
506 | */ |
||
507 | public function filesShouldBeParsed($count) |
||
511 | |||
512 | /** |
||
513 | * @Given /^the ast has a function named "([^"]*)"$/ |
||
514 | */ |
||
515 | public function theAstHasAFunctionNamed($functionName) |
||
519 | |||
520 | /** |
||
521 | * @Given the namespace ':namespace' has a function named ':functionName' |
||
522 | */ |
||
523 | public function theNamespaceFoo(string $namespace, string $functionName) |
||
531 | |||
532 | private function findFunctionInNamespace(NamespaceDescriptor $namespace, string $functionName) |
||
542 | } |
||
543 |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: