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); |
||
| 15 | class ClassGenerator extends AbstractGenerator |
||
| 16 | { |
||
| 17 | use AbstractFinalTrait; |
||
| 18 | |||
| 19 | /** OOP public visibility */ |
||
| 20 | const VISIBILITY_PUBLIC = 'public'; |
||
| 21 | |||
| 22 | /** OOP protected visibility */ |
||
| 23 | const VISIBILITY_PROTECTED = 'protected'; |
||
| 24 | |||
| 25 | /** OOP private visibility */ |
||
| 26 | const VISIBILITY_PRIVATE = 'private'; |
||
| 27 | |||
| 28 | /** @var string Class name */ |
||
| 29 | protected $className; |
||
| 30 | |||
| 31 | /** @var string Parent class name */ |
||
| 32 | protected $parentClassName; |
||
| 33 | |||
| 34 | /** @var string Class namespace */ |
||
| 35 | protected $namespace; |
||
| 36 | |||
| 37 | /** @var array Collection of class uses */ |
||
| 38 | protected $uses = []; |
||
| 39 | |||
| 40 | /** @var array Collection of class interfaces */ |
||
| 41 | protected $interfaces = []; |
||
| 42 | |||
| 43 | /** @var array Collection of class used traits */ |
||
| 44 | protected $traits = []; |
||
| 45 | |||
| 46 | /** @var string Multi-line file description */ |
||
| 47 | protected $fileDescription; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * ClassGenerator constructor. |
||
| 51 | * |
||
| 52 | * @param string $className Class name |
||
| 53 | * @param AbstractGenerator $parent Parent generator |
||
| 54 | */ |
||
| 55 | public function __construct(string $className = null, AbstractGenerator $parent = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set class file description. |
||
| 64 | * |
||
| 65 | * @param array $description Collection of class file description lines |
||
| 66 | * |
||
| 67 | * @return ClassGenerator |
||
| 68 | */ |
||
| 69 | public function defDescription(array $description) : ClassGenerator |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set class namespace. |
||
| 83 | * |
||
| 84 | * @param string $namespace |
||
| 85 | * |
||
| 86 | * @return ClassGenerator |
||
| 87 | */ |
||
| 88 | public function defNamespace(string $namespace) : ClassGenerator |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set class name. |
||
| 97 | * |
||
| 98 | * @param string $className |
||
| 99 | * |
||
| 100 | * @return ClassGenerator |
||
| 101 | */ |
||
| 102 | public function defName(string $className) : ClassGenerator |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set class use. |
||
| 111 | * |
||
| 112 | * @param string $use Use class name |
||
| 113 | * @param string $alias Use class name |
||
| 114 | * |
||
| 115 | * @return ClassGenerator |
||
| 116 | */ |
||
| 117 | public function defUse(string $use, string $alias = null) : ClassGenerator |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set parent class. |
||
| 131 | * |
||
| 132 | * @param string $className Parent class name |
||
| 133 | * |
||
| 134 | * @return ClassGenerator |
||
| 135 | */ |
||
| 136 | public function defExtends(string $className) : ClassGenerator |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set implements interfaces. |
||
| 145 | * |
||
| 146 | * @param string $interfaceName Interface name |
||
| 147 | * |
||
| 148 | * @return ClassGenerator |
||
| 149 | */ |
||
| 150 | public function defImplements(string $interfaceName) : ClassGenerator |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set class trait use. |
||
| 159 | * |
||
| 160 | * @param string $trait Trait class name |
||
| 161 | * |
||
| 162 | * @return ClassGenerator |
||
| 163 | */ |
||
| 164 | public function defTrait(string $trait) : ClassGenerator |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set protected class property. |
||
| 173 | * |
||
| 174 | * @param string $name Property name |
||
| 175 | * @param string $type Property type |
||
| 176 | * @param mixed $value Property value |
||
| 177 | * @param string $description Property description |
||
| 178 | * |
||
| 179 | * @return PropertyGenerator |
||
| 180 | */ |
||
| 181 | public function defProtectedProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set class property. |
||
| 188 | * |
||
| 189 | * @param string $name Property name |
||
| 190 | * @param string $type Property type |
||
| 191 | * @param mixed $value Property value |
||
| 192 | * @param string $description Property description |
||
| 193 | * |
||
| 194 | * @return PropertyGenerator |
||
| 195 | */ |
||
| 196 | public function defProperty(string $name, string $type, $value = null, string $description = null) : PropertyGenerator |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set protected static class property. |
||
| 208 | * |
||
| 209 | * @param string $name Property name |
||
| 210 | * @param string $type Property type |
||
| 211 | * @param mixed $value Property value |
||
| 212 | * @param string $description Property description |
||
| 213 | * |
||
| 214 | * @return PropertyGenerator |
||
| 215 | */ |
||
| 216 | public function defProtectedStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set static class property. |
||
| 223 | * |
||
| 224 | * @param string $name Property name |
||
| 225 | * @param string $type Property type |
||
| 226 | * @param mixed $value Property value |
||
| 227 | * @param string $description Property description |
||
| 228 | * |
||
| 229 | * @return PropertyGenerator |
||
| 230 | */ |
||
| 231 | public function defStaticProperty(string $name, string $type, $value, string $description = null) : PropertyGenerator |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set protected class method. |
||
| 238 | * |
||
| 239 | * @param string $name Method name |
||
| 240 | * |
||
| 241 | * @return MethodGenerator |
||
| 242 | */ |
||
| 243 | public function defProtectedMethod(string $name) : MethodGenerator |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Set public class method. |
||
| 250 | * |
||
| 251 | * @param string $name Method name |
||
| 252 | * |
||
| 253 | * @return MethodGenerator |
||
| 254 | */ |
||
| 255 | public function defMethod(string $name) : MethodGenerator |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set protected static class method. |
||
| 262 | * |
||
| 263 | * @param string $name Method name |
||
| 264 | * |
||
| 265 | * @return MethodGenerator |
||
| 266 | */ |
||
| 267 | public function defProtectedStaticMethod(string $name) : MethodGenerator |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set public static class method. |
||
| 274 | * |
||
| 275 | * @param string $name Method name |
||
| 276 | * |
||
| 277 | * @return MethodGenerator |
||
| 278 | */ |
||
| 279 | public function defStaticMethod(string $name) : MethodGenerator |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set class constant. |
||
| 286 | * |
||
| 287 | * @param string $name |
||
| 288 | * @param mixed $value |
||
| 289 | * |
||
| 290 | * @return $this|ClassConstantGenerator |
||
| 291 | */ |
||
| 292 | public function defConstant(string $name, $value, string $type, string $description) : ClassConstantGenerator |
||
| 301 | |||
| 302 | protected function buildUsesCode(array $formattedCode) : array |
||
| 316 | |||
| 317 | protected function buildCommentsCode(array $formattedCode) : array |
||
| 326 | |||
| 327 | protected function buildTraitsCode(array $formattedCode, string $innerIndentation) : array |
||
| 341 | |||
| 342 | protected function buildFileDescriptionCode(array $formattedCode) : array |
||
| 351 | |||
| 352 | protected function buildConstantsCode(array $formattedCode) : array |
||
| 361 | |||
| 362 | protected function buildPropertiesCode(array $formattedCode) : array |
||
| 370 | |||
| 371 | protected function buildMethodsCode(array $formattedCode) : array |
||
| 379 | |||
| 380 | protected function buildNamespaceCode(array $formattedCode = []) : array |
||
| 393 | |||
| 394 | /** |
||
| 395 | * {@inheritdoc} |
||
| 396 | * @throws \InvalidArgumentException |
||
| 397 | * @throws ClassNameNotFoundException |
||
| 398 | */ |
||
| 399 | public function code(int $indentation = 0) : string |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Build class definition. |
||
| 429 | * |
||
| 430 | * @return string Function definition |
||
| 431 | */ |
||
| 432 | protected function buildDefinition() |
||
| 441 | } |
||
| 442 |
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: