Complex classes like BeanDescriptor 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 BeanDescriptor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class BeanDescriptor implements BeanDescriptorInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Table |
||
| 21 | */ |
||
| 22 | private $table; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var SchemaAnalyzer |
||
| 26 | */ |
||
| 27 | private $schemaAnalyzer; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Schema |
||
| 31 | */ |
||
| 32 | private $schema; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var AbstractBeanPropertyDescriptor[] |
||
| 36 | */ |
||
| 37 | private $beanPropertyDescriptors = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var TDBMSchemaAnalyzer |
||
| 41 | */ |
||
| 42 | private $tdbmSchemaAnalyzer; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var NamingStrategyInterface |
||
| 46 | */ |
||
| 47 | private $namingStrategy; |
||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $beanNamespace; |
||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $generatedBeanNamespace; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Table $table |
||
| 59 | * @param string $beanNamespace |
||
| 60 | * @param string $generatedBeanNamespace |
||
| 61 | * @param SchemaAnalyzer $schemaAnalyzer |
||
| 62 | * @param Schema $schema |
||
| 63 | * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
||
| 64 | * @param NamingStrategyInterface $namingStrategy |
||
| 65 | */ |
||
| 66 | public function __construct(Table $table, string $beanNamespace, string $generatedBeanNamespace, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, NamingStrategyInterface $namingStrategy) |
||
| 77 | |||
| 78 | private function initBeanPropertyDescriptors() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the foreign-key the column is part of, if any. null otherwise. |
||
| 85 | * |
||
| 86 | * @param Table $table |
||
| 87 | * @param Column $column |
||
| 88 | * |
||
| 89 | * @return ForeignKeyConstraint|null |
||
| 90 | */ |
||
| 91 | private function isPartOfForeignKey(Table $table, Column $column) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return AbstractBeanPropertyDescriptor[] |
||
| 107 | */ |
||
| 108 | public function getBeanPropertyDescriptors() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
||
| 115 | * |
||
| 116 | * @return AbstractBeanPropertyDescriptor[] |
||
| 117 | */ |
||
| 118 | public function getConstructorProperties() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Returns the list of columns that have default values for a given table. |
||
| 129 | * |
||
| 130 | * @return AbstractBeanPropertyDescriptor[] |
||
| 131 | */ |
||
| 132 | public function getPropertiesWithDefault() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns the list of properties exposed as getters and setters in this class. |
||
| 144 | * |
||
| 145 | * @return AbstractBeanPropertyDescriptor[] |
||
| 146 | */ |
||
| 147 | public function getExposedProperties(): array |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the list of properties for this table (including parent tables). |
||
| 158 | * |
||
| 159 | * @param Table $table |
||
| 160 | * |
||
| 161 | * @return AbstractBeanPropertyDescriptor[] |
||
| 162 | */ |
||
| 163 | private function getProperties(Table $table) |
||
| 164 | { |
||
| 165 | if ($table->getPrimaryKey() === null) { |
||
| 166 | // Security check: a table MUST have a primary key |
||
| 167 | throw new TDBMException(sprintf('Table "%s" does not have any primary key', $table->getName())); |
||
| 168 | } |
||
| 169 | |||
| 170 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 171 | if ($parentRelationship) { |
||
| 172 | $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
||
| 173 | $properties = $this->getProperties($parentTable); |
||
| 174 | // we merge properties by overriding property names. |
||
| 175 | $localProperties = $this->getPropertiesForTable($table); |
||
| 176 | foreach ($localProperties as $name => $property) { |
||
| 177 | // We do not override properties if this is a primary key! |
||
| 178 | if ($property->isPrimaryKey()) { |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | $properties[$name] = $property; |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $properties = $this->getPropertiesForTable($table); |
||
| 185 | } |
||
| 186 | |||
| 187 | return $properties; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Returns the list of properties for this table (ignoring parent tables). |
||
| 192 | * |
||
| 193 | * @param Table $table |
||
| 194 | * |
||
| 195 | * @return AbstractBeanPropertyDescriptor[] |
||
| 196 | */ |
||
| 197 | private function getPropertiesForTable(Table $table) |
||
| 198 | { |
||
| 199 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 200 | if ($parentRelationship) { |
||
| 201 | $ignoreColumns = $parentRelationship->getUnquotedLocalColumns(); |
||
| 202 | } else { |
||
| 203 | $ignoreColumns = []; |
||
| 204 | } |
||
| 205 | |||
| 206 | $beanPropertyDescriptors = []; |
||
| 207 | foreach ($table->getColumns() as $column) { |
||
| 208 | if (array_search($column->getName(), $ignoreColumns) !== false) { |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | |||
| 212 | $fk = $this->isPartOfForeignKey($table, $column); |
||
| 213 | if ($fk !== null) { |
||
| 214 | // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
||
| 215 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 216 | if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
||
| 217 | continue 2; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | // Check that this property is not an inheritance relationship |
||
| 221 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 222 | if ($parentRelationship === $fk) { |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer, $this->namingStrategy); |
||
| 227 | } else { |
||
| 228 | $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | // Now, let's get the name of all properties and let's check there is no duplicate. |
||
| 233 | /** @var $names AbstractBeanPropertyDescriptor[] */ |
||
| 234 | $names = []; |
||
| 235 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 236 | $name = $beanDescriptor->getGetterName(); |
||
| 237 | if (isset($names[$name])) { |
||
| 238 | $names[$name]->useAlternativeName(); |
||
| 239 | $beanDescriptor->useAlternativeName(); |
||
| 240 | } else { |
||
| 241 | $names[$name] = $beanDescriptor; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | // Final check (throw exceptions if problem arises) |
||
| 246 | $names = []; |
||
| 247 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 248 | $name = $beanDescriptor->getGetterName(); |
||
| 249 | if (isset($names[$name])) { |
||
| 250 | throw new TDBMException('Unsolvable name conflict while generating method name'); |
||
| 251 | } else { |
||
| 252 | $names[$name] = $beanDescriptor; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 256 | // Last step, let's rebuild the list with a map: |
||
| 257 | $beanPropertyDescriptorsMap = []; |
||
| 258 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 259 | $beanPropertyDescriptorsMap[$beanDescriptor->getVariableName()] = $beanDescriptor; |
||
| 260 | } |
||
| 261 | |||
| 262 | return $beanPropertyDescriptorsMap; |
||
| 263 | } |
||
| 264 | |||
| 265 | private function generateBeanConstructor() : string |
||
| 266 | { |
||
| 267 | $constructorProperties = $this->getConstructorProperties(); |
||
| 268 | |||
| 269 | $constructorCode = ' /** |
||
| 270 | * The constructor takes all compulsory arguments. |
||
| 271 | * |
||
| 272 | %s |
||
| 273 | */ |
||
| 274 | public function __construct(%s) |
||
| 275 | { |
||
| 276 | %s%s } |
||
| 277 | |||
| 278 | '; |
||
| 279 | |||
| 280 | $paramAnnotations = []; |
||
| 281 | $arguments = []; |
||
| 282 | $assigns = []; |
||
| 283 | $parentConstructorArguments = []; |
||
| 284 | |||
| 285 | foreach ($constructorProperties as $property) { |
||
| 286 | $arguments[] = $property->getPhpType().' '.$property->getVariableName(); |
||
| 287 | $paramAnnotations[] = $property->getParamAnnotation(); |
||
| 288 | if ($property->getTable()->getName() === $this->table->getName()) { |
||
| 289 | $assigns[] = $property->getConstructorAssignCode()."\n"; |
||
| 290 | } else { |
||
| 291 | $parentConstructorArguments[] = $property->getVariableName(); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
||
| 296 | |||
| 297 | foreach ($this->getPropertiesWithDefault() as $property) { |
||
| 298 | $assigns[] = $property->assignToDefaultCode()."\n"; |
||
| 299 | } |
||
| 300 | |||
| 301 | return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode('', $assigns)); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the descriptors of one-to-many relationships (the foreign keys pointing on this beans) |
||
| 306 | * |
||
| 307 | * @return DirectForeignKeyMethodDescriptor[] |
||
| 308 | */ |
||
| 309 | private function getDirectForeignKeysDescriptors(): array |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return PivotTableMethodsDescriptor[] |
||
| 324 | */ |
||
| 325 | private function getPivotTableDescriptors(): array |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns the list of method descriptors (and applies the alternative name if needed). |
||
| 348 | * |
||
| 349 | * @return MethodDescriptorInterface[] |
||
| 350 | */ |
||
| 351 | public function getMethodDescriptors(): array |
||
| 375 | |||
| 376 | public function generateJsonSerialize(): string |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Returns as an array the class we need to extend from and the list of use statements. |
||
| 418 | * |
||
| 419 | * @param ForeignKeyConstraint|null $parentFk |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function generatePhpCode(): string |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $beanNamespace |
||
| 514 | * @param string $beanClassName |
||
| 515 | * |
||
| 516 | * @return array first element: list of used beans, second item: PHP code as a string |
||
| 517 | */ |
||
| 518 | public function generateFindByDaoCode($beanNamespace, $beanClassName) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Remove identical indexes (indexes on same columns) |
||
| 535 | * |
||
| 536 | * @param Index[] $indexes |
||
| 537 | * @return Index[] |
||
| 538 | */ |
||
| 539 | private function removeDuplicateIndexes(array $indexes): array |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param Index $index |
||
| 551 | * @param string $beanNamespace |
||
| 552 | * @param string $beanClassName |
||
| 553 | * |
||
| 554 | * @return array first element: list of used beans, second item: PHP code as a string |
||
| 555 | */ |
||
| 556 | private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Generates the code for the getUsedTable protected method. |
||
| 666 | * |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | private function generateGetUsedTablesCode() |
||
| 693 | |||
| 694 | private function generateOnDeleteCode() |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Returns the bean class name (without the namespace). |
||
| 722 | * |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | public function getBeanClassName() : string |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Returns the base bean class name (without the namespace). |
||
| 732 | * |
||
| 733 | * @return string |
||
| 734 | */ |
||
| 735 | public function getBaseBeanClassName() : string |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Returns the DAO class name (without the namespace). |
||
| 742 | * |
||
| 743 | * @return string |
||
| 744 | */ |
||
| 745 | public function getDaoClassName() : string |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Returns the base DAO class name (without the namespace). |
||
| 752 | * |
||
| 753 | * @return string |
||
| 754 | */ |
||
| 755 | public function getBaseDaoClassName() : string |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Returns the table used to build this bean. |
||
| 762 | * |
||
| 763 | * @return Table |
||
| 764 | */ |
||
| 765 | public function getTable(): Table |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
||
| 772 | * |
||
| 773 | * @return string |
||
| 774 | */ |
||
| 775 | public function getExtendedBeanClassName(): ?string |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @return string |
||
| 787 | */ |
||
| 788 | public function getBeanNamespace(): string |
||
| 792 | |||
| 793 | /** |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | public function getGeneratedBeanNamespace(): string |
||
| 800 | } |
||
| 801 |