| Total Complexity | 175 |
| Total Lines | 1647 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 51 | class BeanDescriptor implements BeanDescriptorInterface |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * @var Table |
||
| 55 | */ |
||
| 56 | private $table; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var SchemaAnalyzer |
||
| 60 | */ |
||
| 61 | private $schemaAnalyzer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Schema |
||
| 65 | */ |
||
| 66 | private $schema; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var AbstractBeanPropertyDescriptor[] |
||
| 70 | */ |
||
| 71 | private $beanPropertyDescriptors = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var TDBMSchemaAnalyzer |
||
| 75 | */ |
||
| 76 | private $tdbmSchemaAnalyzer; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var NamingStrategyInterface |
||
| 80 | */ |
||
| 81 | private $namingStrategy; |
||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $beanNamespace; |
||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | private $generatedBeanNamespace; |
||
| 90 | /** |
||
| 91 | * @var AnnotationParser |
||
| 92 | */ |
||
| 93 | private $annotationParser; |
||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $daoNamespace; |
||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $generatedDaoNamespace; |
||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $resultIteratorNamespace; |
||
| 106 | /** |
||
| 107 | * @var string |
||
| 108 | */ |
||
| 109 | private $generatedResultIteratorNamespace; |
||
| 110 | /** |
||
| 111 | * @var CodeGeneratorListenerInterface |
||
| 112 | */ |
||
| 113 | private $codeGeneratorListener; |
||
| 114 | /** |
||
| 115 | * @var ConfigurationInterface |
||
| 116 | */ |
||
| 117 | private $configuration; |
||
| 118 | /** |
||
| 119 | * @var BeanRegistry |
||
| 120 | */ |
||
| 121 | private $registry; |
||
| 122 | /** |
||
| 123 | * @var MethodDescriptorInterface[][] |
||
| 124 | */ |
||
| 125 | private $descriptorsByMethodName = []; |
||
| 126 | /** |
||
| 127 | * @var DirectForeignKeyMethodDescriptor[]|null |
||
| 128 | */ |
||
| 129 | private $directForeignKeysDescriptors = null; |
||
| 130 | /** |
||
| 131 | * @var PivotTableMethodsDescriptor[]|null |
||
| 132 | */ |
||
| 133 | private $pivotTableDescriptors = null; |
||
| 134 | |||
| 135 | public function __construct( |
||
| 136 | Table $table, |
||
| 137 | string $beanNamespace, |
||
| 138 | string $generatedBeanNamespace, |
||
| 139 | string $daoNamespace, |
||
| 140 | string $generatedDaoNamespace, |
||
| 141 | string $resultIteratorNamespace, |
||
| 142 | string $generatedResultIteratorNamespace, |
||
| 143 | SchemaAnalyzer $schemaAnalyzer, |
||
| 144 | Schema $schema, |
||
| 145 | TDBMSchemaAnalyzer $tdbmSchemaAnalyzer, |
||
| 146 | NamingStrategyInterface $namingStrategy, |
||
| 147 | AnnotationParser $annotationParser, |
||
| 148 | CodeGeneratorListenerInterface $codeGeneratorListener, |
||
| 149 | ConfigurationInterface $configuration, |
||
| 150 | BeanRegistry $registry |
||
| 151 | ) { |
||
| 152 | $this->table = $table; |
||
| 153 | $this->beanNamespace = $beanNamespace; |
||
| 154 | $this->generatedBeanNamespace = $generatedBeanNamespace; |
||
| 155 | $this->daoNamespace = $daoNamespace; |
||
| 156 | $this->generatedDaoNamespace = $generatedDaoNamespace; |
||
| 157 | $this->resultIteratorNamespace = $resultIteratorNamespace; |
||
| 158 | $this->generatedResultIteratorNamespace = $generatedResultIteratorNamespace; |
||
| 159 | $this->schemaAnalyzer = $schemaAnalyzer; |
||
| 160 | $this->schema = $schema; |
||
| 161 | $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
||
| 162 | $this->namingStrategy = $namingStrategy; |
||
| 163 | $this->annotationParser = $annotationParser; |
||
| 164 | $this->codeGeneratorListener = $codeGeneratorListener; |
||
| 165 | $this->configuration = $configuration; |
||
| 166 | $this->registry = $registry; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function initBeanPropertyDescriptors(): void |
||
| 170 | { |
||
| 171 | $this->beanPropertyDescriptors = $this->getProperties($this->table); |
||
| 172 | |||
| 173 | //init the list of method names with regular properties names |
||
| 174 | foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
||
| 175 | $this->checkForDuplicate($beanPropertyDescriptor); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the foreign-key the column is part of, if any. null otherwise. |
||
| 181 | * |
||
| 182 | * @param Table $table |
||
| 183 | * @param Column $column |
||
| 184 | * |
||
| 185 | * @return ForeignKeyConstraint|null |
||
| 186 | */ |
||
| 187 | private function isPartOfForeignKey(Table $table, Column $column) : ?ForeignKeyConstraint |
||
| 188 | { |
||
| 189 | $localColumnName = $column->getName(); |
||
| 190 | foreach ($table->getForeignKeys() as $foreignKey) { |
||
| 191 | foreach ($foreignKey->getUnquotedLocalColumns() as $columnName) { |
||
| 192 | if ($columnName === $localColumnName) { |
||
| 193 | return $foreignKey; |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | return null; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return AbstractBeanPropertyDescriptor[] |
||
| 203 | */ |
||
| 204 | public function getBeanPropertyDescriptors(): array |
||
| 205 | { |
||
| 206 | return $this->beanPropertyDescriptors; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
||
| 211 | * |
||
| 212 | * @return AbstractBeanPropertyDescriptor[] |
||
| 213 | */ |
||
| 214 | public function getConstructorProperties(): array |
||
| 215 | { |
||
| 216 | $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
||
| 217 | return $property->isCompulsory(); |
||
| 218 | }); |
||
| 219 | |||
| 220 | return $constructorProperties; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns the list of columns that have default values for a given table. |
||
| 225 | * |
||
| 226 | * @return AbstractBeanPropertyDescriptor[] |
||
| 227 | */ |
||
| 228 | public function getPropertiesWithDefault(): array |
||
| 229 | { |
||
| 230 | $properties = $this->getPropertiesForTable($this->table); |
||
| 231 | $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
||
| 232 | return $property->hasDefault(); |
||
| 233 | }); |
||
| 234 | |||
| 235 | return $defaultProperties; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns the list of properties exposed as getters and setters in this class. |
||
| 240 | * |
||
| 241 | * @return AbstractBeanPropertyDescriptor[] |
||
| 242 | */ |
||
| 243 | public function getExposedProperties(): array |
||
| 244 | { |
||
| 245 | $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
||
| 246 | return $property->getTable()->getName() == $this->table->getName(); |
||
| 247 | }); |
||
| 248 | |||
| 249 | return $exposedProperties; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns the list of properties for this table (including parent tables). |
||
| 254 | * |
||
| 255 | * @param Table $table |
||
| 256 | * |
||
| 257 | * @return AbstractBeanPropertyDescriptor[] |
||
| 258 | */ |
||
| 259 | private function getProperties(Table $table): array |
||
| 260 | { |
||
| 261 | // Security check: a table MUST have a primary key |
||
| 262 | TDBMDaoGenerator::getPrimaryKeyColumnsOrFail($table); |
||
| 263 | |||
| 264 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 265 | if ($parentRelationship) { |
||
| 266 | $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
||
| 267 | $properties = $this->getProperties($parentTable); |
||
| 268 | // we merge properties by overriding property names. |
||
| 269 | $localProperties = $this->getPropertiesForTable($table); |
||
| 270 | foreach ($localProperties as $name => $property) { |
||
| 271 | // We do not override properties if this is a primary key! |
||
| 272 | if ($property->isPrimaryKey()) { |
||
| 273 | continue; |
||
| 274 | } |
||
| 275 | $properties[$name] = $property; |
||
| 276 | } |
||
| 277 | } else { |
||
| 278 | $properties = $this->getPropertiesForTable($table); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $properties; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the list of properties for this table (ignoring parent tables). |
||
| 286 | * |
||
| 287 | * @param Table $table |
||
| 288 | * |
||
| 289 | * @return AbstractBeanPropertyDescriptor[] |
||
| 290 | */ |
||
| 291 | private function getPropertiesForTable(Table $table): array |
||
| 292 | { |
||
| 293 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 294 | if ($parentRelationship) { |
||
| 295 | $ignoreColumns = $parentRelationship->getUnquotedLocalColumns(); |
||
| 296 | } else { |
||
| 297 | $ignoreColumns = []; |
||
| 298 | } |
||
| 299 | |||
| 300 | $beanPropertyDescriptors = []; |
||
| 301 | foreach ($table->getColumns() as $column) { |
||
| 302 | if (array_search($column->getName(), $ignoreColumns) !== false) { |
||
| 303 | continue; |
||
| 304 | } |
||
| 305 | |||
| 306 | $fk = $this->isPartOfForeignKey($table, $column); |
||
| 307 | if ($fk !== null) { |
||
| 308 | // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
||
| 309 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 310 | if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
||
| 311 | continue 2; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | // Check that this property is not an inheritance relationship |
||
| 315 | $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
||
| 316 | if ($parentRelationship === $fk) { |
||
| 317 | continue; |
||
| 318 | } |
||
| 319 | |||
| 320 | $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser, $this->registry->getBeanForTableName($fk->getForeignTableName())); |
||
| 321 | } else { |
||
| 322 | $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column, $this->namingStrategy, $this->annotationParser); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | // Now, let's get the name of all properties and let's check there is no duplicate. |
||
| 327 | /* @var $names AbstractBeanPropertyDescriptor[] */ |
||
| 328 | $names = []; |
||
| 329 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 330 | $name = $beanDescriptor->getGetterName(); |
||
| 331 | if (isset($names[$name])) { |
||
| 332 | $names[$name]->useAlternativeName(); |
||
| 333 | $beanDescriptor->useAlternativeName(); |
||
| 334 | } else { |
||
| 335 | $names[$name] = $beanDescriptor; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | // Final check (throw exceptions if problem arises) |
||
| 340 | $names = []; |
||
| 341 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 342 | $name = $beanDescriptor->getGetterName(); |
||
| 343 | if (isset($names[$name])) { |
||
| 344 | throw new TDBMException('Unsolvable name conflict while generating method name'); |
||
| 345 | } else { |
||
| 346 | $names[$name] = $beanDescriptor; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | // Last step, let's rebuild the list with a map: |
||
| 351 | $beanPropertyDescriptorsMap = []; |
||
| 352 | foreach ($beanPropertyDescriptors as $beanDescriptor) { |
||
| 353 | $beanPropertyDescriptorsMap[$beanDescriptor->getVariableName()] = $beanDescriptor; |
||
| 354 | } |
||
| 355 | |||
| 356 | return $beanPropertyDescriptorsMap; |
||
| 357 | } |
||
| 358 | |||
| 359 | private function generateBeanConstructor() : MethodGenerator |
||
| 360 | { |
||
| 361 | $constructorProperties = $this->getConstructorProperties(); |
||
| 362 | |||
| 363 | $constructor = new MethodGenerator('__construct', [], MethodGenerator::FLAG_PUBLIC); |
||
| 364 | $constructor->setDocBlock('The constructor takes all compulsory arguments.'); |
||
| 365 | $constructor->getDocBlock()->setWordWrap(false); |
||
| 366 | |||
| 367 | $assigns = []; |
||
| 368 | $parentConstructorArguments = []; |
||
| 369 | |||
| 370 | foreach ($constructorProperties as $property) { |
||
| 371 | $parameter = new ParameterGenerator(ltrim($property->getVariableName(), '$')); |
||
| 372 | if ($property->isTypeHintable()) { |
||
| 373 | $parameter->setType($property->getPhpType()); |
||
| 374 | } |
||
| 375 | $constructor->setParameter($parameter); |
||
| 376 | |||
| 377 | $constructor->getDocBlock()->setTag($property->getParamAnnotation()); |
||
| 378 | |||
| 379 | if ($property->getTable()->getName() === $this->table->getName()) { |
||
| 380 | $assigns[] = $property->getConstructorAssignCode()."\n"; |
||
| 381 | } else { |
||
| 382 | $parentConstructorArguments[] = $property->getVariableName(); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | $parentConstructorCode = sprintf("parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
||
| 387 | |||
| 388 | foreach ($this->getPropertiesWithDefault() as $property) { |
||
| 389 | $assigns[] = $property->assignToDefaultCode()."\n"; |
||
| 390 | } |
||
| 391 | |||
| 392 | $body = $parentConstructorCode . implode('', $assigns); |
||
| 393 | |||
| 394 | $constructor->setBody($body); |
||
| 395 | |||
| 396 | return $constructor; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the descriptors of one-to-many relationships (the foreign keys pointing on this beans) |
||
| 401 | * |
||
| 402 | * @return DirectForeignKeyMethodDescriptor[] |
||
| 403 | */ |
||
| 404 | private function getDirectForeignKeysDescriptors(): array |
||
| 405 | { |
||
| 406 | if ($this->directForeignKeysDescriptors !== null) { |
||
| 407 | return $this->directForeignKeysDescriptors; |
||
| 408 | } |
||
| 409 | $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
||
| 410 | |||
| 411 | $descriptors = []; |
||
| 412 | |||
| 413 | foreach ($fks as $fk) { |
||
| 414 | $desc = new DirectForeignKeyMethodDescriptor($fk, $this->table, $this->namingStrategy, $this->annotationParser, $this->beanNamespace); |
||
| 415 | $this->checkForDuplicate($desc); |
||
| 416 | $descriptors[] = $desc; |
||
| 417 | } |
||
| 418 | |||
| 419 | $this->directForeignKeysDescriptors = $descriptors; |
||
| 420 | return $this->directForeignKeysDescriptors; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return PivotTableMethodsDescriptor[] |
||
| 425 | */ |
||
| 426 | private function getPivotTableDescriptors(): array |
||
| 427 | { |
||
| 428 | if ($this->pivotTableDescriptors !== null) { |
||
| 429 | return $this->pivotTableDescriptors; |
||
| 430 | } |
||
| 431 | $descs = []; |
||
| 432 | foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
||
| 433 | // There are exactly 2 FKs since this is a pivot table. |
||
| 434 | $fks = array_values($table->getForeignKeys()); |
||
| 435 | |||
| 436 | if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
||
| 437 | list($localFk, $remoteFk) = $fks; |
||
| 438 | $desc = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser); |
||
| 439 | $this->checkForDuplicate($desc); |
||
| 440 | $descs[] = $desc; |
||
| 441 | } |
||
| 442 | if ($fks[1]->getForeignTableName() === $this->table->getName()) { |
||
| 443 | list($remoteFk, $localFk) = $fks; |
||
| 444 | $desc = new PivotTableMethodsDescriptor($table, $localFk, $remoteFk, $this->namingStrategy, $this->beanNamespace, $this->annotationParser); |
||
| 445 | $this->checkForDuplicate($desc); |
||
| 446 | $descs[] = $desc; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | $this->pivotTableDescriptors = $descs; |
||
| 451 | return $this->pivotTableDescriptors; |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Check the method name isn't already used and flag the associated descriptors to use their alternative names if it is the case |
||
| 456 | */ |
||
| 457 | private function checkForDuplicate(MethodDescriptorInterface $descriptor): void |
||
| 458 | { |
||
| 459 | $name = $descriptor->getName(); |
||
| 460 | if (!isset($this->descriptorsByMethodName[$name])) { |
||
| 461 | $this->descriptorsByMethodName[$name] = []; |
||
| 462 | } |
||
| 463 | $this->descriptorsByMethodName[$name][] = $descriptor; |
||
| 464 | if (count($this->descriptorsByMethodName[$name]) > 1) { |
||
| 465 | foreach ($this->descriptorsByMethodName[$name] as $duplicateDescriptor) { |
||
| 466 | $duplicateDescriptor->useAlternativeName(); |
||
| 467 | } |
||
| 468 | } |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Returns the list of method descriptors (and applies the alternative name if needed). |
||
| 473 | * |
||
| 474 | * @return RelationshipMethodDescriptorInterface[] |
||
| 475 | */ |
||
| 476 | public function getMethodDescriptors(): array |
||
| 482 | } |
||
| 483 | |||
| 484 | public function generateJsonSerialize(): MethodGenerator |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Returns as an array the class we need to extend from and the list of use statements. |
||
| 525 | * |
||
| 526 | * @param ForeignKeyConstraint|null $parentFk |
||
| 527 | * @return string[] |
||
| 528 | */ |
||
| 529 | private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null): array |
||
| 530 | { |
||
| 531 | $classes = []; |
||
| 532 | if ($parentFk !== null) { |
||
| 533 | $extends = $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
||
| 534 | $classes[] = $extends; |
||
| 535 | } |
||
| 536 | |||
| 537 | foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
||
| 538 | $className = $beanPropertyDescriptor->getClassName(); |
||
| 539 | if (null !== $className) { |
||
| 540 | $classes[] = $className; |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | foreach ($this->getMethodDescriptors() as $descriptor) { |
||
| 545 | $classes = array_merge($classes, $descriptor->getUsedClasses()); |
||
| 546 | } |
||
| 547 | |||
| 548 | $classes = array_unique($classes); |
||
| 549 | |||
| 550 | return $classes; |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Returns the representation of the PHP bean file with all getters and setters. |
||
| 555 | * |
||
| 556 | * @return ?FileGenerator |
||
| 557 | */ |
||
| 558 | public function generatePhpCode(): ?FileGenerator |
||
| 701 | } |
||
| 702 | |||
| 703 | private function registerTraits(ClassGenerator $class, string $annotationClass): void |
||
| 718 | } |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Writes the representation of the PHP DAO file. |
||
| 724 | * |
||
| 725 | * @return ?FileGenerator |
||
| 726 | */ |
||
| 727 | public function generateDaoPhpCode(): ?FileGenerator |
||
| 1129 | } |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Writes the representation of the PHP ResultIterator file. |
||
| 1133 | */ |
||
| 1134 | public function generateResultIteratorPhpCode(): ?FileGenerator |
||
| 1135 | { |
||
| 1136 | $file = new FileGenerator(); |
||
| 1137 | $class = new ClassGenerator(); |
||
| 1138 | $class->setAbstract(true); |
||
| 1139 | $file->setClass($class); |
||
| 1140 | $file->setNamespace($this->generatedResultIteratorNamespace); |
||
| 1141 | |||
| 1142 | $tableName = $this->table->getName(); |
||
| 1143 | |||
| 1144 | $className = $this->namingStrategy->getResultIteratorClassName($tableName); |
||
| 1145 | $baseClassName = $this->namingStrategy->getBaseResultIteratorClassName($tableName); |
||
| 1146 | $beanClassWithoutNameSpace = $this->namingStrategy->getBeanClassName($tableName); |
||
| 1147 | $beanClassName = $this->beanNamespace.'\\'.$beanClassWithoutNameSpace; |
||
| 1148 | |||
| 1149 | $file->setDocBlock(new DocBlockGenerator( |
||
| 1150 | <<<EOF |
||
| 1151 | This file has been automatically generated by TDBM. |
||
| 1152 | DO NOT edit this file, as it might be overwritten. |
||
| 1153 | If you need to perform changes, edit the $className class instead! |
||
| 1154 | EOF |
||
| 1155 | )); |
||
| 1156 | $class->addUse(ResultIterator::class); |
||
| 1157 | $class->setName($baseClassName); |
||
| 1158 | $class->setExtendedClass(ResultIterator::class); |
||
| 1159 | |||
| 1160 | $class->setDocBlock((new DocBlockGenerator( |
||
| 1161 | "The $baseClassName class will iterate over results of $beanClassWithoutNameSpace class.", |
||
| 1162 | null, |
||
| 1163 | [new Tag\MethodTag('getIterator', ['\\' . $beanClassName . '[]'])] |
||
| 1164 | ))->setWordWrap(false)); |
||
| 1165 | |||
| 1166 | $file = $this->codeGeneratorListener->onBaseResultIteratorGenerated($file, $this, $this->configuration); |
||
| 1167 | |||
| 1168 | return $file; |
||
| 1169 | } |
||
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Tries to find a @defaultSort annotation in one of the columns. |
||
| 1173 | * |
||
| 1174 | * @param Table $table |
||
| 1175 | * |
||
| 1176 | * @return mixed[] First item: column name, Second item: column order (asc/desc) |
||
| 1177 | */ |
||
| 1178 | private function getDefaultSortColumnFromAnnotation(Table $table): array |
||
| 1179 | { |
||
| 1180 | $defaultSort = null; |
||
| 1181 | $defaultSortDirection = null; |
||
| 1182 | foreach ($table->getColumns() as $column) { |
||
| 1183 | $comments = $column->getComment(); |
||
| 1184 | $matches = []; |
||
| 1185 | if ($comments !== null && preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
||
| 1186 | $defaultSort = $column->getName(); |
||
| 1187 | if (count($matches) === 3) { |
||
| 1188 | $defaultSortDirection = $matches[2]; |
||
| 1189 | } else { |
||
| 1190 | $defaultSortDirection = 'ASC'; |
||
| 1191 | } |
||
| 1192 | } |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | return [$defaultSort, $defaultSortDirection]; |
||
| 1196 | } |
||
| 1197 | |||
| 1198 | /** |
||
| 1199 | * @param string $beanNamespace |
||
| 1200 | * @param string $beanClassName |
||
| 1201 | * |
||
| 1202 | * @return MethodGenerator[] |
||
| 1203 | */ |
||
| 1204 | private function generateFindByDaoCode(string $beanNamespace, string $beanClassName, ClassGenerator $class): array |
||
| 1205 | { |
||
| 1206 | $methods = []; |
||
| 1207 | foreach ($this->removeDuplicateIndexes($this->table->getIndexes()) as $index) { |
||
| 1208 | if (!$index->isPrimary()) { |
||
| 1209 | $method = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
||
| 1210 | |||
| 1211 | if ($method !== null) { |
||
| 1212 | $method = $this->codeGeneratorListener->onBaseDaoFindByIndexGenerated($method, $index, $this, $this->configuration, $class); |
||
| 1213 | if ($method !== null) { |
||
| 1214 | $methods[] = $method; |
||
| 1215 | } |
||
| 1216 | } |
||
| 1217 | } |
||
| 1218 | } |
||
| 1219 | usort($methods, static function (MethodGenerator $methodA, MethodGenerator $methodB) { |
||
| 1220 | return $methodA->getName() <=> $methodB->getName(); |
||
| 1221 | }); |
||
| 1222 | |||
| 1223 | return $methods; |
||
| 1224 | } |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Remove identical indexes (indexes on same columns) |
||
| 1228 | * |
||
| 1229 | * @param Index[] $indexes |
||
| 1230 | * @return Index[] |
||
| 1231 | */ |
||
| 1232 | private function removeDuplicateIndexes(array $indexes): array |
||
| 1233 | { |
||
| 1234 | $indexesByKey = []; |
||
| 1235 | foreach ($indexes as $index) { |
||
| 1236 | $key = implode('__`__', $index->getUnquotedColumns()); |
||
| 1237 | // Unique Index have precedence over non unique one |
||
| 1238 | if (!isset($indexesByKey[$key]) || $index->isUnique()) { |
||
| 1239 | $indexesByKey[$key] = $index; |
||
| 1240 | } |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | return array_values($indexesByKey); |
||
| 1244 | } |
||
| 1245 | |||
| 1246 | /** |
||
| 1247 | * @param Index $index |
||
| 1248 | * @param string $beanNamespace |
||
| 1249 | * @param string $beanClassName |
||
| 1250 | * |
||
| 1251 | * @return MethodGenerator|null |
||
| 1252 | */ |
||
| 1253 | private function generateFindByDaoCodeForIndex(Index $index, string $beanNamespace, string $beanClassName): ?MethodGenerator |
||
| 1392 | } |
||
| 1393 | |||
| 1394 | /** |
||
| 1395 | * Generates the code for the getUsedTable protected method. |
||
| 1396 | * |
||
| 1397 | * @return MethodGenerator |
||
| 1398 | */ |
||
| 1399 | private function generateGetUsedTablesCode(): MethodGenerator |
||
| 1400 | { |
||
| 1401 | $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
||
| 1402 | if ($hasParentRelationship) { |
||
| 1403 | $code = sprintf('$tables = parent::getUsedTables(); |
||
| 1404 | $tables[] = %s; |
||
| 1405 | |||
| 1406 | return $tables;', var_export($this->table->getName(), true)); |
||
| 1407 | } else { |
||
| 1408 | $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
||
| 1409 | } |
||
| 1410 | |||
| 1411 | $method = new MethodGenerator('getUsedTables'); |
||
| 1412 | $method->setDocBlock('Returns an array of used tables by this bean (from parent to child relationship).'); |
||
| 1413 | $method->getDocBlock()->setTag(new ReturnTag(['string[]'])); |
||
| 1414 | $method->setReturnType('array'); |
||
| 1415 | $method->setBody($code); |
||
| 1416 | |||
| 1417 | return $method; |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | private function generateOnDeleteCode(): ?MethodGenerator |
||
| 1421 | { |
||
| 1422 | $code = ''; |
||
| 1423 | $relationships = $this->getPropertiesForTable($this->table); |
||
| 1424 | foreach ($relationships as $relationship) { |
||
| 1425 | if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
||
| 1426 | $tdbmFk = ForeignKey::createFromFk($relationship->getForeignKey()); |
||
| 1427 | $code .= '$this->setRef('.var_export($tdbmFk->getCacheKey(), true).', null, '.var_export($this->table->getName(), true).");\n"; |
||
| 1428 | } |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | if (!$code) { |
||
| 1432 | return null; |
||
| 1433 | } |
||
| 1434 | |||
| 1435 | $method = new MethodGenerator('onDelete'); |
||
| 1436 | $method->setDocBlock('Method called when the bean is removed from database.'); |
||
| 1437 | $method->setReturnType('void'); |
||
| 1438 | $method->setBody('parent::onDelete(); |
||
| 1439 | '.$code); |
||
| 1440 | |||
| 1441 | return $method; |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * @param PivotTableMethodsDescriptor[] $pivotTableMethodsDescriptors |
||
| 1446 | * @return MethodGenerator |
||
| 1447 | */ |
||
| 1448 | private function generateGetManyToManyRelationshipDescriptorCode(array $pivotTableMethodsDescriptors): ?MethodGenerator |
||
| 1449 | { |
||
| 1450 | if (empty($pivotTableMethodsDescriptors)) { |
||
| 1451 | return null; |
||
| 1452 | } |
||
| 1453 | |||
| 1454 | $method = new MethodGenerator('_getManyToManyRelationshipDescriptor'); |
||
| 1455 | $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); |
||
| 1456 | $method->setDocBlock('Get the paths used for many to many relationships methods.'); |
||
| 1457 | $method->getDocBlock()->setTag(new GenericTag('internal')); |
||
| 1458 | $method->setReturnType(ManyToManyRelationshipPathDescriptor::class); |
||
| 1459 | |||
| 1460 | $parameter = new ParameterGenerator('pathKey'); |
||
| 1461 | $parameter->setType('string'); |
||
| 1462 | $method->setParameter($parameter); |
||
| 1463 | |||
| 1464 | $code = 'switch ($pathKey) {'."\n"; |
||
| 1465 | foreach ($pivotTableMethodsDescriptors as $pivotTableMethodsDescriptor) { |
||
| 1466 | $code .= ' case '.var_export($pivotTableMethodsDescriptor->getManyToManyRelationshipKey(), true).":\n"; |
||
| 1467 | $code .= ' return '.$pivotTableMethodsDescriptor->getManyToManyRelationshipInstantiationCode().";\n"; |
||
| 1468 | } |
||
| 1469 | $code .= " default:\n"; |
||
| 1470 | $code .= " return parent::_getManyToManyRelationshipDescriptor(\$pathKey);\n"; |
||
| 1471 | $code .= "}\n"; |
||
| 1472 | |||
| 1473 | $method->setBody($code); |
||
| 1474 | |||
| 1475 | return $method; |
||
| 1476 | } |
||
| 1477 | |||
| 1478 | /** |
||
| 1479 | * @param PivotTableMethodsDescriptor[] $pivotTableMethodsDescriptors |
||
| 1480 | * @return MethodGenerator |
||
| 1481 | */ |
||
| 1482 | private function generateGetManyToManyRelationshipDescriptorKeysCode(array $pivotTableMethodsDescriptors): ?MethodGenerator |
||
| 1483 | { |
||
| 1484 | if (empty($pivotTableMethodsDescriptors)) { |
||
| 1485 | return null; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | $method = new MethodGenerator('_getManyToManyRelationshipDescriptorKeys'); |
||
| 1489 | $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC); |
||
| 1490 | $method->setReturnType('array'); |
||
| 1491 | $method->setDocBlock('Returns the list of keys supported for many to many relationships'); |
||
| 1492 | $method->getDocBlock()->setTag(new GenericTag('internal')); |
||
| 1493 | $method->getDocBlock()->setTag(new ReturnTag('string[]')); |
||
| 1494 | |||
| 1495 | $keys = []; |
||
| 1496 | foreach ($pivotTableMethodsDescriptors as $pivotTableMethodsDescriptor) { |
||
| 1497 | $keys[] = var_export($pivotTableMethodsDescriptor->getManyToManyRelationshipKey(), true); |
||
| 1498 | } |
||
| 1499 | |||
| 1500 | $code = 'return array_merge(parent::_getManyToManyRelationshipDescriptorKeys(), ['.implode(', ', $keys).']);'; |
||
| 1501 | |||
| 1502 | $method->setBody($code); |
||
| 1503 | |||
| 1504 | return $method; |
||
| 1505 | } |
||
| 1506 | |||
| 1507 | /** |
||
| 1508 | * @param PivotTableMethodsDescriptor[] $pivotTableMethodsDescriptors |
||
| 1509 | * @return MethodGenerator |
||
| 1510 | */ |
||
| 1511 | private function generateCloneCode(array $pivotTableMethodsDescriptors): MethodGenerator |
||
| 1512 | { |
||
| 1513 | $precode = ''; |
||
| 1514 | $postcode = ''; |
||
| 1515 | |||
| 1516 | foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
||
| 1517 | $postcode .= $beanPropertyDescriptor->getCloneRule(); |
||
| 1518 | } |
||
| 1519 | |||
| 1520 | //cloning many to many relationships |
||
| 1521 | foreach ($pivotTableMethodsDescriptors as $beanMethodDescriptor) { |
||
| 1522 | $precode .= $beanMethodDescriptor->getCloneRule()."\n"; |
||
| 1523 | } |
||
| 1524 | |||
| 1525 | $method = new MethodGenerator('__clone'); |
||
| 1526 | $method->setBody($precode."parent::__clone();\n".$postcode); |
||
| 1527 | |||
| 1528 | return $method; |
||
| 1529 | } |
||
| 1530 | |||
| 1531 | /** |
||
| 1532 | * Returns the bean class name (without the namespace). |
||
| 1533 | * |
||
| 1534 | * @return string |
||
| 1535 | */ |
||
| 1536 | public function getBeanClassName() : string |
||
| 1537 | { |
||
| 1538 | return $this->namingStrategy->getBeanClassName($this->table->getName()); |
||
| 1539 | } |
||
| 1540 | |||
| 1541 | /** |
||
| 1542 | * Returns the base bean class name (without the namespace). |
||
| 1543 | * |
||
| 1544 | * @return string |
||
| 1545 | */ |
||
| 1546 | public function getBaseBeanClassName() : string |
||
| 1547 | { |
||
| 1548 | return $this->namingStrategy->getBaseBeanClassName($this->table->getName()); |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Returns the DAO class name (without the namespace). |
||
| 1553 | * |
||
| 1554 | * @return string |
||
| 1555 | */ |
||
| 1556 | public function getDaoClassName() : string |
||
| 1557 | { |
||
| 1558 | return $this->namingStrategy->getDaoClassName($this->table->getName()); |
||
| 1559 | } |
||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Returns the base DAO class name (without the namespace). |
||
| 1563 | * |
||
| 1564 | * @return string |
||
| 1565 | */ |
||
| 1566 | public function getBaseDaoClassName() : string |
||
| 1567 | { |
||
| 1568 | return $this->namingStrategy->getBaseDaoClassName($this->table->getName()); |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Returns the ResultIterator class name (without the namespace). |
||
| 1573 | * |
||
| 1574 | * @return string |
||
| 1575 | */ |
||
| 1576 | public function getResultIteratorClassName() : string |
||
| 1577 | { |
||
| 1578 | return $this->namingStrategy->getResultIteratorClassName($this->table->getName()); |
||
| 1579 | } |
||
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Returns the base ResultIterator class name (without the namespace). |
||
| 1583 | * |
||
| 1584 | * @return string |
||
| 1585 | */ |
||
| 1586 | public function getBaseResultIteratorClassName() : string |
||
| 1589 | } |
||
| 1590 | |||
| 1591 | /** |
||
| 1592 | * Returns the table used to build this bean. |
||
| 1593 | * |
||
| 1594 | * @return Table |
||
| 1595 | */ |
||
| 1596 | public function getTable(): Table |
||
| 1597 | { |
||
| 1598 | return $this->table; |
||
| 1599 | } |
||
| 1600 | |||
| 1601 | /** |
||
| 1602 | * Returns the extended bean class name (without the namespace), or null if the bean is not extended. |
||
| 1603 | * |
||
| 1604 | * @return string |
||
| 1605 | */ |
||
| 1606 | public function getExtendedBeanClassName(): ?string |
||
| 1607 | { |
||
| 1608 | $parentFk = $this->schemaAnalyzer->getParentRelationship($this->table->getName()); |
||
| 1609 | if ($parentFk !== null) { |
||
| 1610 | return $this->namingStrategy->getBeanClassName($parentFk->getForeignTableName()); |
||
| 1611 | } else { |
||
| 1612 | return null; |
||
| 1613 | } |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | /** |
||
| 1617 | * @return string |
||
| 1618 | */ |
||
| 1619 | public function getBeanNamespace(): string |
||
| 1620 | { |
||
| 1621 | return $this->beanNamespace; |
||
| 1622 | } |
||
| 1623 | |||
| 1624 | /** |
||
| 1625 | * @return string |
||
| 1626 | */ |
||
| 1627 | public function getGeneratedBeanNamespace(): string |
||
| 1630 | } |
||
| 1631 | |||
| 1632 | /** |
||
| 1633 | * @param ForeignKeyConstraint[] $fks |
||
| 1634 | */ |
||
| 1635 | private function generateGetForeignKeys(array $fks): MethodGenerator |
||
| 1636 | { |
||
| 1637 | $fkArray = []; |
||
| 1638 | |||
| 1639 | foreach ($fks as $fk) { |
||
| 1640 | $tdbmFk = ForeignKey::createFromFk($fk); |
||
| 1641 | $fkArray[$tdbmFk->getCacheKey()] = [ |
||
| 1642 | ForeignKey::FOREIGN_TABLE => $fk->getForeignTableName(), |
||
| 1643 | ForeignKey::LOCAL_COLUMNS => $fk->getUnquotedLocalColumns(), |
||
| 1644 | ForeignKey::FOREIGN_COLUMNS => $fk->getUnquotedForeignColumns(), |
||
| 1645 | ]; |
||
| 1646 | } |
||
| 1647 | |||
| 1648 | ksort($fkArray); |
||
| 1649 | foreach ($fkArray as $tableFks) { |
||
| 1650 | ksort($tableFks); |
||
| 1651 | } |
||
| 1652 | |||
| 1653 | $code = <<<EOF |
||
| 1654 | if (\$tableName === %s) { |
||
| 1655 | if (self::\$foreignKeys === null) { |
||
| 1656 | self::\$foreignKeys = new ForeignKeys(%s); |
||
| 1657 | } |
||
| 1658 | return self::\$foreignKeys; |
||
| 1659 | } |
||
| 1660 | return parent::getForeignKeys(\$tableName); |
||
| 1661 | EOF; |
||
| 1662 | $code = sprintf($code, var_export($this->getTable()->getName(), true), $this->psr2VarExport($fkArray, ' ')); |
||
| 1663 | |||
| 1664 | $method = new MethodGenerator('getForeignKeys'); |
||
| 1665 | $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PROTECTED); |
||
| 1666 | $method->setStatic(true); |
||
| 1667 | $method->setDocBlock('Internal method used to retrieve the list of foreign keys attached to this bean.'); |
||
| 1668 | $method->setReturnType(ForeignKeys::class); |
||
| 1669 | |||
| 1670 | $parameter = new ParameterGenerator('tableName'); |
||
| 1671 | $parameter->setType('string'); |
||
| 1672 | $method->setParameter($parameter); |
||
| 1673 | |||
| 1674 | |||
| 1675 | $method->setBody($code); |
||
| 1676 | |||
| 1677 | return $method; |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | /** |
||
| 1681 | * @param mixed $var |
||
| 1682 | * @param string $indent |
||
| 1683 | * @return string |
||
| 1684 | */ |
||
| 1685 | private function psr2VarExport($var, string $indent=''): string |
||
| 1698 | } |
||
| 1699 | } |
||
| 1700 |