Complex classes like AbstractReflectionElement 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 AbstractReflectionElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 12 | abstract class AbstractReflectionElement extends AbstractReflection implements ElementReflectionInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | protected $isDocumented; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var mixed[] |
||
| 21 | */ |
||
| 22 | protected $annotations; |
||
| 23 | |||
| 24 | public function getStartPosition(): int |
||
| 28 | |||
| 29 | public function getEndPosition(): int |
||
| 33 | |||
| 34 | public function isDocumented(): bool |
||
| 50 | |||
| 51 | public function isDeprecated(): bool |
||
| 64 | |||
| 65 | public function inNamespace(): bool |
||
| 69 | |||
| 70 | public function getNamespaceName(): string |
||
| 87 | |||
| 88 | public function getPseudoNamespaceName(): string |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string[] |
||
| 95 | */ |
||
| 96 | public function getNamespaceAliases(): array |
||
| 100 | |||
| 101 | public function getDescription(): string |
||
| 112 | |||
| 113 | public function getDocComment(): string |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return mixed[] |
||
| 120 | */ |
||
| 121 | public function getAnnotations(): array |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return mixed[] |
||
| 139 | */ |
||
| 140 | public function getAnnotation(string $name): array |
||
| 144 | |||
| 145 | public function hasAnnotation(string $name): bool |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param mixed $reflection |
||
| 152 | * @return mixed[] |
||
| 153 | */ |
||
| 154 | private function getAnnotationsFromReflection($reflection): array |
||
| 177 | |||
| 178 | private function getShortDescription(): string |
||
| 192 | } |
||
| 193 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: