| Total Complexity | 54 |
| Total Lines | 457 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like DataClassHandler 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.
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 DataClassHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class DataClassHandler |
||
| 20 | { |
||
| 21 | use Injectable; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null|\ast\Node - Abstract syntax tree of the data object class |
||
| 25 | */ |
||
| 26 | public ?\ast\Node $ast; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string[] - Array to collect all types which are not defined as use |
||
| 30 | * statement |
||
| 31 | */ |
||
| 32 | public array $unusedTypes = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string - Full qualified name of the class |
||
| 36 | */ |
||
| 37 | private string $fqn; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var DataClassFileHandler - Handle the file of the class |
||
| 41 | */ |
||
| 42 | private DataClassFileHandler $file; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array<int,array<string,string>> - All collected model properties. Collected from db, has_one and belongs_to |
||
| 46 | * config |
||
| 47 | */ |
||
| 48 | private array $modelProperties = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array<int,array<string,string>> - All collected model methods. Collected from has_many, many_many, |
||
| 52 | * belongs_many_many config. |
||
| 53 | */ |
||
| 54 | private array $modelMethods = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string[] - Injected dependencies |
||
| 58 | */ |
||
| 59 | private static array $dependencies = [ |
||
| 60 | 'renderer' => '%$' . DataClassTaskView::class, |
||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var DataClassTaskView - Injected data class renderer |
||
| 65 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 66 | */ |
||
| 67 | private DataClassTaskView $renderer; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Util - Injected util class |
||
| 71 | */ |
||
| 72 | private Util $util; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string[] - All classes in same namespace |
||
| 76 | */ |
||
| 77 | private array $classesInSameNamespace = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor. |
||
| 81 | * |
||
| 82 | * @param string $fqn Full qualified name of the class |
||
| 83 | */ |
||
| 84 | public function __construct(string $fqn) |
||
| 85 | { |
||
| 86 | $this->fqn = $fqn; |
||
| 87 | |||
| 88 | /** @var Util $util */ |
||
| 89 | $util = Injector::inst()->create(Util::class); |
||
| 90 | $this->util = $util; |
||
| 91 | |||
| 92 | /** @var DataClassFileHandler $fileHandler */ |
||
| 93 | $fileHandler = Injector::inst()->createWithArgs(DataClassFileHandler::class, [$this->util->fileByFqn($fqn)]); |
||
| 94 | $this->file = $fileHandler; |
||
| 95 | |||
| 96 | $this->ast = $this->file->getClassAst($fqn); |
||
| 97 | |||
| 98 | $this->classesInSameNamespace = $this->util->getClassesFromNamespace($this->util->getNamespaceFromFqn($fqn)); |
||
| 99 | |||
| 100 | $this->fetchModelProperties(); |
||
| 101 | $this->fetchModelMethods(); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get all missing use statements. |
||
| 106 | * |
||
| 107 | * @return string[] |
||
| 108 | */ |
||
| 109 | public function getMissingUseStatements(): array |
||
| 110 | { |
||
| 111 | $this->unusedTypes = array_unique($this->unusedTypes); |
||
| 112 | |||
| 113 | if (count($this->unusedTypes) === 0) { |
||
| 114 | return []; |
||
| 115 | } |
||
| 116 | |||
| 117 | return array_map(function ($unusedType) { |
||
| 118 | return 'use ' . $unusedType . ';'; |
||
| 119 | }, $this->unusedTypes); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get the file handler. |
||
| 124 | */ |
||
| 125 | public function getFile(): DataClassFileHandler |
||
| 126 | { |
||
| 127 | return $this->file; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the abstract syntax tree of the class. |
||
| 132 | */ |
||
| 133 | public function getAst(): ?\ast\Node |
||
| 134 | { |
||
| 135 | return $this->ast; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get class phpdoc from abstract syntax tree. |
||
| 140 | */ |
||
| 141 | public function getClassPhpDoc(): string |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get all db fields, collected from "db", "has_one" and "belongs_to" config, |
||
| 155 | * to fetch all possible model phpdoc properties. |
||
| 156 | * |
||
| 157 | * @return array<int,array<string,string>> |
||
| 158 | */ |
||
| 159 | public function getModelProperties() |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get all db many relations, collected from "has_many", "many_many" and |
||
| 166 | * "belongs_many_many" config, to fetch all possible model phpdoc methods. |
||
| 167 | * |
||
| 168 | * @return array<int,array<string,string>> |
||
| 169 | */ |
||
| 170 | public function getModelMethods() |
||
| 171 | { |
||
| 172 | return $this->modelMethods; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Generate a class php doc for collected model properties and methods. |
||
| 177 | */ |
||
| 178 | public function generateClassPhpDoc(): string |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set data class renderer. |
||
| 237 | */ |
||
| 238 | final public function setRenderer(DataClassTaskView $renderer): self |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get data class renderer. |
||
| 247 | */ |
||
| 248 | public function getRenderer(): DataClassTaskView |
||
| 251 | } |
||
| 252 | |||
| 253 | private function fetchModelProperties(bool $filterExistingAnnotations = true): void |
||
| 290 | }); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Gert model property array data. |
||
| 295 | * |
||
| 296 | * @return array<string,string> |
||
| 297 | */ |
||
| 298 | private function getModelPropertyData( |
||
| 299 | string $fieldName, |
||
| 300 | string $fieldType, |
||
| 301 | bool $filterExistingAnnotations, |
||
| 302 | string $description |
||
| 303 | ) { |
||
| 304 | if ($this->checkField($fieldName) === true) { |
||
| 305 | return []; |
||
| 306 | } |
||
| 307 | |||
| 308 | $matches = []; |
||
| 309 | if ($filterExistingAnnotations === true) { |
||
| 310 | $regex = '/@property[\s]*' . preg_quote($fieldType, '/') . '[\s]*\\$' . |
||
| 311 | preg_quote($fieldName, '/') . '[\s]*/m'; |
||
| 312 | |||
| 313 | preg_match_all($regex, $this->getClassPhpDoc(), $matches, PREG_SET_ORDER, 0); |
||
| 314 | } |
||
| 315 | |||
| 316 | if (count($matches) > 0) { |
||
| 317 | return []; |
||
| 318 | } |
||
| 319 | |||
| 320 | return [ |
||
| 321 | 'dataType' => $fieldType, |
||
| 322 | 'variableName' => $fieldName, |
||
| 323 | 'description' => $description, |
||
| 324 | ]; |
||
| 325 | } |
||
| 326 | |||
| 327 | private function fetchModelMethods(bool $filterExistingAnnotations = true): void |
||
| 328 | { |
||
| 329 | $hasManyList = 'SilverStripe\ORM\HasManyList'; |
||
| 330 | $manyManyList = 'SilverStripe\ORM\ManyManyList'; |
||
| 331 | |||
| 332 | // List relations |
||
| 333 | $relations = [ |
||
| 334 | 'has_many' => ['list' => $hasManyList], |
||
| 335 | 'many_many' => ['list' => $manyManyList], |
||
| 336 | 'belongs_many_many' => ['list' => $manyManyList], |
||
| 337 | ]; |
||
| 338 | |||
| 339 | foreach ($relations as $key => $relation) { |
||
| 340 | /** @var array<string,array<string,string>|string> $fieldConfigs */ |
||
| 341 | $fieldConfigs = Config::forClass($this->fqn)->get($key); |
||
| 342 | |||
| 343 | foreach ($fieldConfigs as $fieldName => $fieldType) { |
||
| 344 | $this->modelMethods[] = $this->getModelMethodData( |
||
| 345 | $fieldName, |
||
| 346 | $fieldType, |
||
| 347 | $relation, |
||
| 348 | $manyManyList, |
||
| 349 | $key, |
||
| 350 | $filterExistingAnnotations |
||
| 351 | ); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | $this->modelMethods = array_filter($this->modelMethods, function ($modelMethod) { |
||
| 356 | return count($modelMethod) > 0; |
||
| 357 | }); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Get model method data array. |
||
| 362 | * |
||
| 363 | * @param array<string,string>|string $fieldType |
||
| 364 | * @param array<string,string> $relation |
||
| 365 | * |
||
| 366 | * @return array<string,string> |
||
| 367 | */ |
||
| 368 | private function getModelMethodData( |
||
| 406 | ]; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Check if a db field should not be included. |
||
| 411 | */ |
||
| 412 | private function checkField(string $fieldName): bool |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Format data type. If we collect data types which are not declared as use statement and or not inside same |
||
| 431 | * namespace we can shorten the data type. |
||
| 432 | */ |
||
| 433 | private function shortenDataType(string $dataType): string |
||
| 476 | } |
||
| 477 | } |
||
| 478 |