Complex classes like ClassGenerator 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 ClassGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 13 | class ClassGenerator extends AbstractGenerator |
||
| 14 | { |
||
| 15 | use AbstractFinalTrait; |
||
| 16 | |||
| 17 | /** OOP public visibility */ |
||
| 18 | const VISIBILITY_PUBLIC = 'public'; |
||
| 19 | |||
| 20 | /** OOP protected visibility */ |
||
| 21 | const VISIBILITY_PROTECTED = 'protected'; |
||
| 22 | |||
| 23 | /** OOP private visibility */ |
||
| 24 | const VISIBILITY_PRIVATE = 'private'; |
||
| 25 | |||
| 26 | /** @var string Class name */ |
||
| 27 | protected $className; |
||
| 28 | |||
| 29 | /** @var string Parent class name */ |
||
| 30 | protected $parentClassName; |
||
| 31 | |||
| 32 | /** @var string Class namespace */ |
||
| 33 | protected $namespace; |
||
| 34 | |||
| 35 | /** @var array Collection of class uses */ |
||
| 36 | protected $uses = []; |
||
| 37 | |||
| 38 | /** @var array Collection of class interfaces */ |
||
| 39 | protected $interfaces = []; |
||
| 40 | |||
| 41 | /** @var array Collection of class used traits */ |
||
| 42 | protected $traits = []; |
||
| 43 | |||
| 44 | /** @var string Multi-line file description */ |
||
| 45 | protected $fileDescription; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * ClassGenerator constructor. |
||
| 49 | * |
||
| 50 | * @param string $className Class name |
||
| 51 | * @param AbstractGenerator $parent Parent generator |
||
| 52 | */ |
||
| 53 | public function __construct(string $className, AbstractGenerator $parent = null) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set class file description. |
||
| 62 | * |
||
| 63 | * @param array $description Collection of class file description lines |
||
| 64 | * |
||
| 65 | * @return ClassGenerator |
||
| 66 | */ |
||
| 67 | public function defDescription(array $description) : ClassGenerator |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Set class namespace. |
||
| 81 | * |
||
| 82 | * @param string $namespace |
||
| 83 | * |
||
| 84 | * @return ClassGenerator |
||
| 85 | */ |
||
| 86 | public function defNamespace(string $namespace) : ClassGenerator |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set class use. |
||
| 95 | * |
||
| 96 | * @param string $use Use class name |
||
| 97 | * @param string $alias Use class name |
||
| 98 | * |
||
| 99 | * @return ClassGenerator |
||
| 100 | */ |
||
| 101 | public function defUse(string $use, string $alias = null) : ClassGenerator |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set parent class. |
||
| 115 | * |
||
| 116 | * @param string $className Parent class name |
||
| 117 | * |
||
| 118 | * @return ClassGenerator |
||
| 119 | */ |
||
| 120 | public function defExtends(string $className) : ClassGenerator |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set implements interfaces. |
||
| 129 | * |
||
| 130 | * @param string $interfaceName Interface name |
||
| 131 | * |
||
| 132 | * @return ClassGenerator |
||
| 133 | */ |
||
| 134 | public function defImplements(string $interfaceName) : ClassGenerator |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set class trait use. |
||
| 143 | * |
||
| 144 | * @param string $trait Trait class name |
||
| 145 | * |
||
| 146 | * @return ClassGenerator |
||
| 147 | */ |
||
| 148 | public function defTrait(string $trait) : ClassGenerator |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set protected class property. |
||
| 157 | * |
||
| 158 | * @param string $name Property name |
||
| 159 | * @param string $type Property type |
||
| 160 | * @param mixed $value Property value |
||
| 161 | * @param string $description Property description |
||
| 162 | * |
||
| 163 | * @return PropertyGenerator |
||
| 164 | */ |
||
| 165 | public function defProtectedProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set class property. |
||
| 172 | * |
||
| 173 | * @param string $name Property name |
||
| 174 | * @param string $type Property type |
||
| 175 | * @param mixed $value Property value |
||
| 176 | * @param string $description Property description |
||
| 177 | * |
||
| 178 | * @return PropertyGenerator |
||
| 179 | */ |
||
| 180 | public function defProperty(string $name, string $type, $value = null, string $description = null) : PropertyGenerator |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set protected static class property. |
||
| 192 | * |
||
| 193 | * @param string $name Property name |
||
| 194 | * @param string $type Property type |
||
| 195 | * @param mixed $value Property value |
||
| 196 | * @param string $description Property description |
||
| 197 | * |
||
| 198 | * @return PropertyGenerator |
||
| 199 | */ |
||
| 200 | public function defProtectedStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set static class property. |
||
| 207 | * |
||
| 208 | * @param string $name Property name |
||
| 209 | * @param string $type Property type |
||
| 210 | * @param mixed $value Property value |
||
| 211 | * @param string $description Property description |
||
| 212 | * |
||
| 213 | * @return PropertyGenerator |
||
| 214 | */ |
||
| 215 | public function defStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Set protected class method. |
||
| 222 | * |
||
| 223 | * @param string $name Method name |
||
| 224 | * |
||
| 225 | * @return MethodGenerator |
||
| 226 | */ |
||
| 227 | public function defProtectedMethod(string $name) : MethodGenerator |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set public class method. |
||
| 234 | * |
||
| 235 | * @param string $name Method name |
||
| 236 | * |
||
| 237 | * @return MethodGenerator |
||
| 238 | */ |
||
| 239 | public function defMethod(string $name) : MethodGenerator |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set protected static class method. |
||
| 246 | * |
||
| 247 | * @param string $name Method name |
||
| 248 | * |
||
| 249 | * @return MethodGenerator |
||
| 250 | */ |
||
| 251 | public function defProtectedStaticMethod(string $name) : MethodGenerator |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set public static class method. |
||
| 258 | * |
||
| 259 | * @param string $name Method name |
||
| 260 | * |
||
| 261 | * @return MethodGenerator |
||
| 262 | */ |
||
| 263 | public function defStaticMethod(string $name) : MethodGenerator |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set class constant. |
||
| 270 | * |
||
| 271 | * @param string $name |
||
| 272 | * @param mixed $value |
||
| 273 | * |
||
| 274 | * @return $this|ClassConstantGenerator |
||
| 275 | */ |
||
| 276 | public function defConstant(string $name, $value, string $type, string $description) : ClassConstantGenerator |
||
| 285 | |||
| 286 | protected function buildUsesCode(array $formattedCode) : array |
||
| 300 | |||
| 301 | protected function buildCommentsCode(array $formattedCode) : array |
||
| 310 | |||
| 311 | protected function buildTraitsCode(array $formattedCode, string $innerIndentation) : array |
||
| 325 | |||
| 326 | protected function buildFileDescriptionCode(array $formattedCode) : array |
||
| 335 | |||
| 336 | protected function buildConstantsCode(array $formattedCode) : array |
||
| 345 | |||
| 346 | protected function buildPropertiesCode(array $formattedCode) : array |
||
| 354 | |||
| 355 | protected function buildMethodsCode(array $formattedCode) : array |
||
| 363 | |||
| 364 | protected function buildNamespaceCode(array $formattedCode = []) : array |
||
| 377 | |||
| 378 | /** |
||
| 379 | * {@inheritdoc} |
||
| 380 | * @throws \InvalidArgumentException |
||
| 381 | */ |
||
| 382 | public function code(int $indentation = 0) : string |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Build class definition. |
||
| 408 | * |
||
| 409 | * @return string Function definition |
||
| 410 | */ |
||
| 411 | protected function buildDefinition() |
||
| 420 | } |
||
| 421 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: