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) |
||
236 | |||
237 | /** |
||
238 | * @param string $classFqsen |
||
239 | * @param string $methodName |
||
240 | * @Then class ":classFqsen" has a method :method |
||
241 | */ |
||
242 | public function classHasMethod($classFqsen, $methodName) |
||
258 | |||
259 | /** |
||
260 | * @param string $classFqsen |
||
261 | * @param string $propertyName |
||
262 | * @Then class ":classFqsen" has a property :property |
||
263 | */ |
||
264 | public function classHasProperty($classFqsen, $propertyName) |
||
272 | |||
273 | /** |
||
274 | * @param string $classFqsen |
||
275 | * @param string $methodName |
||
276 | * @param string $argument |
||
277 | * @param string $type |
||
278 | * @Then class ":classFqsen" has a method :method with argument :argument of type ":type" |
||
279 | */ |
||
280 | public function classHasMethodWithArgumentOfType($classFqsen, $methodName, $argument, $type) |
||
291 | |||
292 | /** |
||
293 | * @param string $classFqsen |
||
294 | * @param string $methodName |
||
295 | * @param string $param |
||
296 | * @param string $type |
||
297 | * @Then class ":classFqsen" has a method :method with param :param of type ":type" |
||
298 | */ |
||
299 | public function classHasMethodWithParamOfType($classFqsen, $methodName, $param, $type) |
||
311 | |||
312 | /** |
||
313 | * @param string $classFqsen |
||
314 | * @param string $constantName |
||
315 | * @Then class ":classFqsen" has a constant :constantName |
||
316 | */ |
||
317 | public function classHasConstant($classFqsen, $constantName) |
||
324 | |||
325 | /** |
||
326 | * @param string $className |
||
327 | * @return ClassDescriptor |
||
328 | * @throws \Exception |
||
329 | */ |
||
330 | private function findClassByName($className) |
||
343 | |||
344 | /** |
||
345 | * @param string $tagName |
||
346 | * @param int $expectedNumber |
||
347 | * @param DescriptorAbstract $element |
||
348 | */ |
||
349 | private static function AssertTagCount($element, $tagName, $expectedNumber) |
||
359 | |||
360 | /** |
||
361 | * @Then /^the ast has a file named "([^"]*)" with a summary:$/ |
||
362 | * @throws \Exception |
||
363 | */ |
||
364 | public function theAstHasAFileNamedWithASummary(string $fileName, PyStringNode $string) |
||
372 | |||
373 | /** |
||
374 | * @param string $classFqsen |
||
375 | * @param string $methodName |
||
376 | * @throws Exception |
||
377 | * @Then class ":classFqsen" has a method :method with returntype :returnType |
||
378 | * @Then class ":classFqsen" has a method :method with returntype :returnType without description |
||
379 | */ |
||
380 | public function classHasMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
387 | |||
388 | /** |
||
389 | * @param string $classFqsen |
||
390 | * @param string $methodName |
||
391 | * @throws Exception |
||
392 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType |
||
393 | * @Then class ":classFqsen" has a magic method :method with returntype :returnType without description |
||
394 | */ |
||
395 | public function classHasMagicMethodWithReturnType($classFqsen, $methodName, $returnType) |
||
402 | |||
403 | /** |
||
404 | * @param string $classFqsen |
||
405 | * @param string $methodName |
||
406 | * @throws Exception |
||
407 | * @Then class ":classFqsen" has a method :method with returntype :returnType with description: |
||
408 | */ |
||
409 | public function classHasMethodWithReturnTypeAndDescription($classFqsen, $methodName, $returnType, PyStringNode $description) |
||
416 | |||
417 | /** |
||
418 | * @Then class ":classFqsen" has a method ":method" without returntype |
||
419 | * @throws \Exception |
||
420 | */ |
||
421 | public function classReturnTaggetReturnWithoutAnyWithoutReturntype($classFqsen, $methodName) |
||
427 | |||
428 | /** |
||
429 | * @throws Exception |
||
430 | * @Then has function :fqsen with returntype :returnType |
||
431 | * @Then has function :fqsen with returntype :returnType without description |
||
432 | */ |
||
433 | public function functionWithReturnType($fqsen, $returnType) |
||
440 | |||
441 | /** |
||
442 | * @throws Exception |
||
443 | * @Then has function :fqsen with returntype :returnType with description: |
||
444 | */ |
||
445 | public function functionWithReturnTypeAndDescription($fqsen, $returnType, PyStringNode $description) |
||
452 | |||
453 | /** |
||
454 | * @Then has function :fqsen without returntype |
||
455 | * @throws \Exception |
||
456 | */ |
||
457 | public function functionWithoutReturntype($fqsen) |
||
463 | |||
464 | /** |
||
465 | * @throws Exception |
||
466 | */ |
||
467 | private function findMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
477 | |||
478 | /** |
||
479 | * @throws Exception |
||
480 | */ |
||
481 | private function findMagicMethodResponse($classFqsen, $methodName): ReturnDescriptor |
||
498 | |||
499 | /** |
||
500 | * @throws Exception |
||
501 | */ |
||
502 | private function findFunctionResponse(string $fqsen): ReturnDescriptor |
||
507 | |||
508 | /** |
||
509 | * @Then class ":classFqsen" has a magic method :method with argument ":argument" of type :type |
||
510 | */ |
||
511 | public function classHasMagicMethodWithArgument($classFqsen, $methodName, $argument, $type) |
||
526 | |||
527 | /** |
||
528 | * @Then /^(\d+) files should be parsed$/ |
||
529 | */ |
||
530 | public function filesShouldBeParsed($count) |
||
534 | |||
535 | /** |
||
536 | * @Then /^the ast has a function named "([^"]*)"$/ |
||
537 | */ |
||
538 | public function theAstHasAFunctionNamed($functionName) |
||
545 | |||
546 | /** |
||
547 | * @Then argument :argument of function ":functionName" has no defined type and description is: |
||
548 | */ |
||
549 | public function argumentOfFunctionHasNoTypeAndHasDescripion($argument, $functionName, PyStringNode $description) |
||
565 | |||
566 | /** |
||
567 | * @Given the namespace ':namespace' has a function named ':functionName' |
||
568 | */ |
||
569 | public function theNamespaceFoo(string $namespace, string $functionName) |
||
577 | |||
578 | private function findFunctionInNamespace(NamespaceDescriptor $namespace, string $functionName) |
||
588 | |||
589 | /** |
||
590 | * @Then /^file "([^"]*)" must contain a marker$/ |
||
591 | */ |
||
592 | public function fileMustContainAMarker($filename) |
||
601 | |||
602 | /** |
||
603 | * @Then class ":className" must have magic property ":propertyName" of type :type |
||
604 | */ |
||
605 | public function classMustHaveMagicPropertyOfType($className, $propertyName, $type) |
||
620 | } |
||
621 |
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: