Complex classes like RuntimeClass 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 RuntimeClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 8 | final class RuntimeClass implements CodeGenerator |
||
| 9 | { |
||
| 10 | use FinalTrait; |
||
| 11 | use AbstractTrait; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $class; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $className; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string[] |
||
| 25 | */ |
||
| 26 | private $namespace; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $extends; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string[] |
||
| 35 | */ |
||
| 36 | private $implements = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | private $uses = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | private $isLoaded = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var RuntimeMethod[] |
||
| 50 | */ |
||
| 51 | private $methods = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var RuntimeProperty[] |
||
| 55 | */ |
||
| 56 | private $properties = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var RuntimeConstant[] |
||
| 60 | */ |
||
| 61 | private $constant = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string[] |
||
| 65 | */ |
||
| 66 | private static $registeredClasses = []; |
||
| 67 | |||
| 68 | 16 | public function __construct(string $class, string ...$implements) |
|
| 81 | |||
| 82 | 2 | public function getNamespace(): string |
|
| 86 | |||
| 87 | 1 | public function getClass(): string |
|
| 91 | |||
| 92 | 1 | public function getClassName(): string |
|
| 96 | |||
| 97 | 15 | public function implements(string ...$interfaces): self |
|
| 109 | |||
| 110 | 1 | public function extends(string $class): self |
|
| 119 | |||
| 120 | public function use(string ...$traits): self |
||
| 132 | |||
| 133 | public function isUsing(string $name): bool |
||
| 137 | |||
| 138 | 1 | public function isExtending(string $name): bool |
|
| 142 | |||
| 143 | 1 | public function implementsInterface(string $interface): bool |
|
| 147 | |||
| 148 | 2 | public function addMethod(RuntimeMethod $method): self |
|
| 154 | |||
| 155 | 1 | public function hasMethod(string $name): bool |
|
| 159 | |||
| 160 | 1 | public function addProperty(RuntimeProperty $property): self |
|
| 166 | |||
| 167 | 1 | public function addConstant(RuntimeConstant $constant): self |
|
| 173 | |||
| 174 | 1 | public function hasConstant(string $name): bool |
|
| 178 | |||
| 179 | 1 | public function hasProperty(string $name): bool |
|
| 183 | |||
| 184 | 10 | public function generateCode(): string |
|
| 234 | |||
| 235 | 1 | public function createInstance() |
|
| 241 | |||
| 242 | 2 | public function register(): bool |
|
| 252 | |||
| 253 | 2 | public function load(): bool |
|
| 278 | |||
| 279 | 2 | public function __toString(): string |
|
| 283 | } |
||
| 284 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..