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 |
||
35 | class ApiContext extends BaseContext implements Context |
||
36 | { |
||
37 | /** |
||
38 | * @Then /^the AST has a class named "([^"]*)" in file "([^"]*)"$/ |
||
39 | * @throws \Exception |
||
40 | */ |
||
41 | public function theASTHasAclassNamedInFile($class, $file) |
||
58 | |||
59 | /** |
||
60 | * @Then /^the AST doesn't have a class "([^"]*)"$/ |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | public function theASTDoesnTHaveAClass($className) |
||
74 | |||
75 | /** |
||
76 | * @Then /^the class named "([^"]*)" is in the default package$/ |
||
77 | * @throws \Exception |
||
78 | */ |
||
79 | public function theASTHasAClassInDefaultPackage($class) |
||
85 | |||
86 | /** |
||
87 | * @Then /^the AST has a trait named "([^"]*)" in file "([^"]*)"$/ |
||
88 | * @throws \Exception |
||
89 | */ |
||
90 | public function theASTHasATraitNamedInFile($trait, $file) |
||
107 | |||
108 | /** |
||
109 | * @Then the class named ":class" has docblock with content: |
||
110 | */ |
||
111 | public function classHasDocblockWithContent($class, PyStringNode $expectedContent) |
||
117 | |||
118 | /** |
||
119 | * @Then class ":classFqsen" has :docElement: |
||
120 | * @throws Exception |
||
121 | */ |
||
122 | public function classHasDocblockContent($classFqsen, $docElement, PyStringNode $value) |
||
130 | |||
131 | /** |
||
132 | * @Then class ":classFqsen" has :elementType :elementName with :docElement: |
||
133 | */ |
||
134 | public function classHasElementWithDocblockContent($classFqsen, $elementType, $elementName, $docElement, PyStringNode $value) |
||
157 | |||
158 | /** |
||
159 | * @Then class ":classFqsen" has version :value |
||
160 | */ |
||
161 | public function classHasVersion($classFqsen, $value) |
||
174 | |||
175 | /** |
||
176 | * @Then class ":classFqsen" without tag :tagName |
||
177 | */ |
||
178 | public function classWithoutTag($classFqsen, $tagName) |
||
182 | |||
183 | /** |
||
184 | * @param string $classFqsen |
||
185 | * @param string $tagName |
||
186 | * @param int $expectedNumber |
||
187 | * @Then class ":classFqsen" has exactly :expectedNumber tag :tagName |
||
188 | */ |
||
189 | public function classHasTag($classFqsen, $tagName, $expectedNumber) |
||
194 | |||
195 | /** |
||
196 | * @param string $classFqsen |
||
197 | * @param string $tagName |
||
198 | * @param string $method |
||
199 | * @Then class ":classFqsen" has a method named :method without tag :tagName |
||
200 | */ |
||
201 | public function classHasMethodWithoutTag($classFqsen, $tagName, $method) |
||
205 | |||
206 | /** |
||
207 | * @param string $classFqsen |
||
208 | * @param string $tagName |
||
209 | * @param string $methodName |
||
210 | * @Then class ":classFqsen" has a method named :method with exactly :expected tag :tagName |
||
211 | */ |
||
212 | public function classHasMethodWithExpectedCountTag($classFqsen, $tagName, $methodName, $expectedCount) |
||
219 | |||
220 | /** |
||
221 | * @param string $classFqsen |
||
222 | * @param string $methodName |
||
223 | * @Then class ":classFqsen" has a method :method with argument ":argument is variadic |
||
224 | */ |
||
225 | public function classHasMethodWithArgumentVariadic($classFqsen, $methodName, $argument) |
||
237 | |||
238 | /** |
||
239 | * @param string $classFqsen |
||
240 | * @param string $methodName |
||
241 | * @Then class ":classFqsen" has a method :method |
||
242 | */ |
||
243 | public function classHasMethod($classFqsen, $methodName) |
||
251 | |||
252 | /** |
||
253 | * @param string $classFqsen |
||
254 | * @param string $propertyName |
||
255 | * @Then class ":classFqsen" has a property :property |
||
256 | */ |
||
257 | public function classHasProperty($classFqsen, $propertyName) |
||
265 | |||
266 | /** |
||
267 | * @param string $classFqsen |
||
268 | * @param string $methodName |
||
269 | * @param string $argument |
||
270 | * @param string $type |
||
271 | * @Then class ":classFqsen" has a method :method with argument :argument of type ":type" |
||
272 | */ |
||
273 | public function classHasMethodWithArgumentOfType($classFqsen, $methodName, $argument, $type) |
||
284 | |||
285 | /** |
||
286 | * @param string $classFqsen |
||
287 | * @param string $methodName |
||
288 | * @param string $param |
||
289 | * @param string $type |
||
290 | * @Then class ":classFqsen" has a method :method with param :param of type ":type" |
||
291 | */ |
||
292 | public function classHasMethodWithParamOfType($classFqsen, $methodName, $param, $type) |
||
304 | |||
305 | /** |
||
306 | * @param string $classFqsen |
||
307 | * @param string $constantName |
||
308 | * @Then class ":classFqsen" has a constant :constantName |
||
309 | */ |
||
310 | public function classHasConstant($classFqsen, $constantName) |
||
317 | |||
318 | /** |
||
319 | * @param string $className |
||
320 | * @return ClassDescriptor |
||
321 | * @throws \Exception |
||
322 | */ |
||
323 | private function findClassByName($className) |
||
336 | |||
337 | /** |
||
338 | * @param string $tagName |
||
339 | * @param int $expectedNumber |
||
340 | * @param DescriptorAbstract $element |
||
341 | */ |
||
342 | private static function AssertTagCount($element, $tagName, $expectedNumber) |
||
352 | |||
353 | /** |
||
354 | * @Then /^the ast has a file named "([^"]*)" with a summary:$/ |
||
355 | * @throws \Exception |
||
356 | */ |
||
357 | public function theAstHasAFileNamedWithASummary(string $fileName, PyStringNode $string) |
||
365 | |||
366 | /** |
||
367 | * @param string $classFqsen |
||
368 | * @param string $methodName |
||
369 | * @throws Exception |
||
370 | * @Then class ":classFqsen" has a method :method with returntype :returnType |
||
371 | * @Then class ":classFqsen" has a method :method with returntype :returnType without description |
||
372 | */ |
||
373 | public function classHasMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
380 | |||
381 | /** |
||
382 | * @param string $classFqsen |
||
383 | * @param string $methodName |
||
384 | * @throws Exception |
||
385 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType |
||
386 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType without description |
||
387 | */ |
||
388 | public function classHasMagicMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
395 | |||
396 | /** |
||
397 | * @param string $classFqsen |
||
398 | * @param string $methodName |
||
399 | * @throws Exception |
||
400 | * @Then class ":classFqsen" has a method :method with returntype :returnType with description: |
||
401 | */ |
||
402 | public function classHasMethodWithReturnTypeAndDescription($classFqsen, $methodName, $returnType, PyStringNode $description) |
||
409 | |||
410 | /** |
||
411 | * @Then class ":classFqsen" has a method ":method" without returntype |
||
412 | * @throws \Exception |
||
413 | */ |
||
414 | public function classReturnTaggetReturnWithoutAnyWithoutReturntype($classFqsen, $methodName) |
||
420 | |||
421 | /** |
||
422 | * @throws Exception |
||
423 | * @Then has function :fqsen with returntype :returnType |
||
424 | * @Then has function :fqsen with returntype :returnType without description |
||
425 | */ |
||
426 | public function functionWithReturnType($fqsen, $returnType) |
||
433 | |||
434 | /** |
||
435 | * @throws Exception |
||
436 | * @Then has function :fqsen with returntype :returnType with description: |
||
437 | */ |
||
438 | public function functionWithReturnTypeAndDescription($fqsen, $returnType, PyStringNode $description) |
||
445 | |||
446 | /** |
||
447 | * @Then has function :fqsen without returntype |
||
448 | * @throws \Exception |
||
449 | */ |
||
450 | public function functionWithoutReturntype($fqsen) |
||
456 | |||
457 | /** |
||
458 | * @throws Exception |
||
459 | */ |
||
460 | private function findMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
470 | |||
471 | /** |
||
472 | * @throws Exception |
||
473 | */ |
||
474 | private function findMagicMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
491 | |||
492 | /** |
||
493 | * @throws Exception |
||
494 | */ |
||
495 | private function findFunctionResponse(string $fqsen): ReturnDescriptor |
||
500 | |||
501 | /** |
||
502 | * @Then class ":classFqsen" has a magic method :method with argument ":argument" of type :type |
||
503 | */ |
||
504 | public function classHasMagicMethodWithArgument($classFqsen, $methodName, $argument, $type) |
||
519 | |||
520 | /** |
||
521 | * @Then /^(\d+) files should be parsed$/ |
||
522 | */ |
||
523 | public function filesShouldBeParsed($count) |
||
527 | |||
528 | /** |
||
529 | * @Then /^the ast has a function named "([^"]*)"$/ |
||
530 | */ |
||
531 | public function theAstHasAFunctionNamed($functionName) |
||
538 | |||
539 | /** |
||
540 | * @Then argument :argument of function ":functionName" has no defined type and description is: |
||
541 | */ |
||
542 | public function argumentOfFunctionHasNoTypeAndHasDescripion($argument, $functionName, PyStringNode $description) |
||
558 | |||
559 | /** |
||
560 | * @Given the namespace ':namespace' has a function named ':functionName' |
||
561 | */ |
||
562 | public function theNamespaceFoo(string $namespace, string $functionName) |
||
570 | |||
571 | private function findFunctionInNamespace(NamespaceDescriptor $namespace, string $functionName) |
||
581 | |||
582 | /** |
||
583 | * @Then /^file "([^"]*)" must contain a marker$/ |
||
584 | */ |
||
585 | public function fileMustContainAMarker($filename) |
||
594 | |||
595 | /** |
||
596 | * @Then class ":className" must have magic property ":propertyName" of type :type |
||
597 | */ |
||
598 | public function classMustHaveMagicPropertyOfType($className, $propertyName, $type) |
||
613 | } |
||
614 |
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: