| Total Complexity | 41 |
| Total Lines | 545 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 3 | Features | 2 |
Complex classes like TDBMDaoGenerator 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 TDBMDaoGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class TDBMDaoGenerator |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var TDBMSchemaAnalyzer |
||
| 40 | */ |
||
| 41 | private $tdbmSchemaAnalyzer; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var GeneratorListenerInterface |
||
| 45 | */ |
||
| 46 | private $eventDispatcher; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var NamingStrategyInterface |
||
| 50 | */ |
||
| 51 | private $namingStrategy; |
||
| 52 | /** |
||
| 53 | * @var ConfigurationInterface |
||
| 54 | */ |
||
| 55 | private $configuration; |
||
| 56 | /** |
||
| 57 | * @var Inflector |
||
| 58 | */ |
||
| 59 | private static $inflector; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor. |
||
| 63 | * |
||
| 64 | * @param ConfigurationInterface $configuration |
||
| 65 | * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
||
| 66 | */ |
||
| 67 | public function __construct(ConfigurationInterface $configuration, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
||
| 68 | { |
||
| 69 | $this->configuration = $configuration; |
||
| 70 | $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
||
| 71 | $this->namingStrategy = $configuration->getNamingStrategy(); |
||
| 72 | $this->eventDispatcher = $configuration->getGeneratorEventDispatcher(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Generates all the daos and beans. |
||
| 77 | * |
||
| 78 | * @throws TDBMException |
||
| 79 | */ |
||
| 80 | public function generateAllDaosAndBeans(bool $fromLock = false): void |
||
| 81 | { |
||
| 82 | if (!$fromLock) { |
||
| 83 | $this->tdbmSchemaAnalyzer->generateLockFile(); |
||
|
|
|||
| 84 | } |
||
| 85 | $schema = $this->tdbmSchemaAnalyzer->getSchema(); |
||
| 86 | |||
| 87 | // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
||
| 88 | $tableList = $schema->getTables(); |
||
| 89 | |||
| 90 | // Remove all beans and daos from junction tables |
||
| 91 | $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
||
| 92 | $junctionTableNames = array_map(function (Table $table) { |
||
| 93 | return $table->getName(); |
||
| 94 | }, $junctionTables); |
||
| 95 | |||
| 96 | $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
||
| 97 | return !in_array($table->getName(), $junctionTableNames, true); |
||
| 98 | }); |
||
| 99 | |||
| 100 | $this->cleanUpGenerated(); |
||
| 101 | |||
| 102 | $beanDescriptors = []; |
||
| 103 | |||
| 104 | $beanRegistry = new BeanRegistry($this->configuration, $schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy); |
||
| 105 | foreach ($tableList as $table) { |
||
| 106 | $beanDescriptors[] = $beanRegistry->addBeanForTable($table); |
||
| 107 | } |
||
| 108 | foreach ($beanDescriptors as $beanDescriptor) { |
||
| 109 | $beanDescriptor->initBeanPropertyDescriptors(); |
||
| 110 | } |
||
| 111 | foreach ($beanDescriptors as $beanDescriptor) { |
||
| 112 | $this->generateBean($beanDescriptor); |
||
| 113 | $this->generateDao($beanDescriptor); |
||
| 114 | $this->generateResultIterator($beanDescriptor); |
||
| 115 | } |
||
| 116 | |||
| 117 | $this->generateFactory($beanDescriptors); |
||
| 118 | |||
| 119 | // Let's call the list of listeners |
||
| 120 | $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Removes all files from the Generated folders. |
||
| 125 | * This is a way to ensure that when a table is deleted, the matching bean/dao are deleted. |
||
| 126 | * Note: only abstract generated classes are deleted. We do not delete the code that might have been customized |
||
| 127 | * by the user. The user will need to delete this code him/herself |
||
| 128 | */ |
||
| 129 | private function cleanUpGenerated(): void |
||
| 130 | { |
||
| 131 | $generatedBeanDir = $this->configuration->getPathFinder()->getPath($this->configuration->getBeanNamespace().'\\Generated\\Xxx')->getPath(); |
||
| 132 | $this->deleteAllPhpFiles($generatedBeanDir); |
||
| 133 | |||
| 134 | $generatedDaoDir = $this->configuration->getPathFinder()->getPath($this->configuration->getDaoNamespace().'\\Generated\\Xxx')->getPath(); |
||
| 135 | $this->deleteAllPhpFiles($generatedDaoDir); |
||
| 136 | |||
| 137 | $generatedResultIteratorDir = $this->configuration->getPathFinder()->getPath($this->configuration->getResultIteratorNamespace().'\\Generated\\Xxx')->getPath(); |
||
| 138 | $this->deleteAllPhpFiles($generatedResultIteratorDir); |
||
| 139 | } |
||
| 140 | |||
| 141 | private function deleteAllPhpFiles(string $directory): void |
||
| 142 | { |
||
| 143 | $files = glob($directory.'/*.php', GLOB_NOSORT); |
||
| 144 | if ($files === false) { |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | $fileSystem = new Filesystem(); |
||
| 148 | $fileSystem->remove($files); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
||
| 153 | * |
||
| 154 | * @param BeanDescriptor $beanDescriptor |
||
| 155 | * |
||
| 156 | * @throws TDBMException |
||
| 157 | */ |
||
| 158 | public function generateBean(BeanDescriptor $beanDescriptor): void |
||
| 159 | { |
||
| 160 | $className = $beanDescriptor->getBeanClassName(); |
||
| 161 | $baseClassName = $beanDescriptor->getBaseBeanClassName(); |
||
| 162 | $table = $beanDescriptor->getTable(); |
||
| 163 | $beannamespace = $this->configuration->getBeanNamespace(); |
||
| 164 | $file = $beanDescriptor->generatePhpCode(); |
||
| 165 | if ($file === null) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
||
| 170 | |||
| 171 | $fileContent = $file->generate(); |
||
| 172 | |||
| 173 | // Hard code PSR-2 fix |
||
| 174 | $fileContent = $this->psr2Fix($fileContent); |
||
| 175 | // Add the declare strict-types directive |
||
| 176 | $commentEnd = strpos($fileContent, ' */') + 3; |
||
| 177 | $fileContent = substr($fileContent, 0, $commentEnd) . "\n\ndeclare(strict_types=1);" . substr($fileContent, $commentEnd + 1); |
||
| 178 | |||
| 179 | $this->dumpFile($possibleBaseFileName, $fileContent); |
||
| 180 | |||
| 181 | $possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname(); |
||
| 182 | |||
| 183 | if (!file_exists($possibleFileName)) { |
||
| 184 | $tableName = $table->getName(); |
||
| 185 | $str = "<?php |
||
| 186 | /* |
||
| 187 | * This file has been automatically generated by TDBM. |
||
| 188 | * You can edit this file as it will not be overwritten. |
||
| 189 | */ |
||
| 190 | |||
| 191 | declare(strict_types=1); |
||
| 192 | |||
| 193 | namespace {$beannamespace}; |
||
| 194 | |||
| 195 | use {$beannamespace}\\Generated\\{$baseClassName}; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * The $className class maps the '$tableName' table in database. |
||
| 199 | */ |
||
| 200 | class $className extends $baseClassName |
||
| 201 | { |
||
| 202 | } |
||
| 203 | "; |
||
| 204 | |||
| 205 | $this->dumpFile($possibleFileName, $str); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Writes the PHP bean DAO with simple functions to create/get/save objects. |
||
| 211 | * |
||
| 212 | * @param BeanDescriptor $beanDescriptor |
||
| 213 | * |
||
| 214 | * @throws TDBMException |
||
| 215 | */ |
||
| 216 | private function generateDao(BeanDescriptor $beanDescriptor): void |
||
| 217 | { |
||
| 218 | $className = $beanDescriptor->getDaoClassName(); |
||
| 219 | $baseClassName = $beanDescriptor->getBaseDaoClassName(); |
||
| 220 | $beanClassName = $beanDescriptor->getBeanClassName(); |
||
| 221 | $table = $beanDescriptor->getTable(); |
||
| 222 | $file = $beanDescriptor->generateDaoPhpCode(); |
||
| 223 | if ($file === null) { |
||
| 224 | return; |
||
| 225 | } |
||
| 226 | $daonamespace = $this->configuration->getDaoNamespace(); |
||
| 227 | $tableName = $table->getName(); |
||
| 228 | |||
| 229 | $beanClassWithoutNameSpace = $beanClassName; |
||
| 230 | |||
| 231 | $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
||
| 232 | |||
| 233 | $fileContent = $file->generate(); |
||
| 234 | |||
| 235 | // Hard code PSR-2 fix |
||
| 236 | $fileContent = $this->psr2Fix($fileContent); |
||
| 237 | // Add the declare strict-types directive |
||
| 238 | $commentEnd = strpos($fileContent, ' */') + 3; |
||
| 239 | $fileContent = substr($fileContent, 0, $commentEnd) . "\n\ndeclare(strict_types=1);" . substr($fileContent, $commentEnd + 1); |
||
| 240 | |||
| 241 | $this->dumpFile($possibleBaseFileName, $fileContent); |
||
| 242 | |||
| 243 | |||
| 244 | $possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname(); |
||
| 245 | |||
| 246 | // Now, let's generate the "editable" class |
||
| 247 | if (!file_exists($possibleFileName)) { |
||
| 248 | $str = "<?php |
||
| 249 | /* |
||
| 250 | * This file has been automatically generated by TDBM. |
||
| 251 | * You can edit this file as it will not be overwritten. |
||
| 252 | */ |
||
| 253 | |||
| 254 | declare(strict_types=1); |
||
| 255 | |||
| 256 | namespace {$daonamespace}; |
||
| 257 | |||
| 258 | use {$daonamespace}\\Generated\\{$baseClassName}; |
||
| 259 | |||
| 260 | /** |
||
| 261 | * The $className class will maintain the persistence of $beanClassWithoutNameSpace class into the $tableName table. |
||
| 262 | */ |
||
| 263 | class $className extends $baseClassName |
||
| 264 | { |
||
| 265 | } |
||
| 266 | "; |
||
| 267 | $this->dumpFile($possibleFileName, $str); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Fixes PSR-2 for files generated by Zend-Code |
||
| 273 | */ |
||
| 274 | private function psr2Fix(string $content): string |
||
| 275 | { |
||
| 276 | // Removes the extra blank line at the end (before: `\n\n`, after: `\n`) |
||
| 277 | return rtrim($content, PHP_EOL) . PHP_EOL; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Writes the PHP ResultIterator file with typed accessors. |
||
| 282 | */ |
||
| 283 | private function generateResultIterator(BeanDescriptor $beanDescriptor) : void |
||
| 284 | { |
||
| 285 | $resultIteratorClassName = $beanDescriptor->getResultIteratorClassName(); |
||
| 286 | $resultIteratorBaseClassName = $beanDescriptor->getBaseResultIteratorClassName(); |
||
| 287 | $resultIteratorNamespace = $this->configuration->getResultIteratorNamespace(); |
||
| 288 | $resultIteratorBaseNamespace = $resultIteratorNamespace . '\\Generated'; |
||
| 289 | $beanClassWithoutNameSpace = $beanDescriptor->getBeanClassName(); |
||
| 290 | $file = $beanDescriptor->generateResultIteratorPhpCode(); |
||
| 291 | if ($file === null) { |
||
| 292 | return; |
||
| 293 | } |
||
| 294 | |||
| 295 | $fileContent = $this->psr2Fix($file->generate()); |
||
| 296 | $commentEnd = strpos($fileContent, ' */') + 3; |
||
| 297 | $fileContent = substr($fileContent, 0, $commentEnd) . "\n\ndeclare(strict_types=1);" . substr($fileContent, $commentEnd + 1); |
||
| 298 | |||
| 299 | $baseResultIteratorFilePath = $this->configuration->getPathFinder()->getPath($resultIteratorBaseNamespace.'\\'.$resultIteratorBaseClassName)->getPathname(); |
||
| 300 | $this->dumpFile($baseResultIteratorFilePath, $fileContent); |
||
| 301 | |||
| 302 | $resultIteratorFilePath = $this->configuration->getPathFinder()->getPath($resultIteratorNamespace.'\\'.$resultIteratorClassName)->getPathname(); |
||
| 303 | // Now, let's generate the "editable" class |
||
| 304 | if (!file_exists($resultIteratorFilePath)) { |
||
| 305 | $str = "<?php |
||
| 306 | /* |
||
| 307 | * This file has been automatically generated by TDBM. |
||
| 308 | * You can edit this file as it will not be overwritten. |
||
| 309 | */ |
||
| 310 | |||
| 311 | declare(strict_types=1); |
||
| 312 | |||
| 313 | namespace {$resultIteratorNamespace}; |
||
| 314 | |||
| 315 | use {$resultIteratorBaseNamespace}\\{$resultIteratorBaseClassName}; |
||
| 316 | |||
| 317 | /** |
||
| 318 | * The $resultIteratorClassName class will iterate over results of $beanClassWithoutNameSpace class. |
||
| 319 | */ |
||
| 320 | class $resultIteratorClassName extends $resultIteratorBaseClassName |
||
| 321 | { |
||
| 322 | } |
||
| 323 | "; |
||
| 324 | $this->dumpFile($resultIteratorFilePath, $str); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Generates the factory bean. |
||
| 330 | * |
||
| 331 | * @param BeanDescriptor[] $beanDescriptors |
||
| 332 | * @throws TDBMException |
||
| 333 | */ |
||
| 334 | private function generateFactory(array $beanDescriptors) : void |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Transforms a string to camelCase (except the first letter will be uppercase too). |
||
| 434 | * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
||
| 435 | * Quoting is removed if present. |
||
| 436 | * |
||
| 437 | * @param string $str |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public static function toCamelCase(string $str) : string |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Tries to put string to the singular form (if it is plural). |
||
| 465 | * We assume the table names are in english. |
||
| 466 | * |
||
| 467 | * @param string $str |
||
| 468 | * |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public static function toSingular(string $str): string |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Tries to put string to the plural form (if it is singular). |
||
| 482 | * We assume the string is in english. |
||
| 483 | */ |
||
| 484 | public static function toPlural(string $str): string |
||
| 485 | { |
||
| 486 | if (self::$inflector === null) { |
||
| 487 | self::$inflector = InflectorFactory::create()->build(); |
||
| 488 | } |
||
| 489 | |||
| 490 | return self::$inflector->pluralize($str); |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Put the first letter of the string in lower case. |
||
| 495 | * Very useful to transform a class name into a variable name. |
||
| 496 | * |
||
| 497 | * @param string $str |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public static function toVariableName(string $str): string |
||
| 502 | { |
||
| 503 | return strtolower(substr($str, 0, 1)).substr($str, 1); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Ensures the file passed in parameter can be written in its directory. |
||
| 508 | * |
||
| 509 | * @param string $fileName |
||
| 510 | * |
||
| 511 | * @throws TDBMException |
||
| 512 | */ |
||
| 513 | private function ensureDirectoryExist(string $fileName): void |
||
| 514 | { |
||
| 515 | $dirName = dirname($fileName); |
||
| 516 | if (!file_exists($dirName)) { |
||
| 517 | $old = umask(0); |
||
| 518 | $result = mkdir($dirName, 0775, true); |
||
| 519 | umask($old); |
||
| 520 | if ($result === false) { |
||
| 521 | throw new TDBMException("Unable to create directory: '".$dirName."'."); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | private function dumpFile(string $fileName, string $content) : void |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
||
| 536 | * |
||
| 537 | * @param Type $type The DBAL type |
||
| 538 | * |
||
| 539 | * @return string The PHP type |
||
| 540 | */ |
||
| 541 | public static function dbalTypeToPhpType(Type $type) : string |
||
| 542 | { |
||
| 543 | $map = [ |
||
| 544 | Type::TARRAY => 'array', |
||
| 545 | Type::SIMPLE_ARRAY => 'array', |
||
| 546 | 'json' => 'array', // 'json' is supported from Doctrine DBAL 2.6 only. |
||
| 547 | Type::JSON_ARRAY => 'array', |
||
| 548 | Type::BIGINT => 'string', |
||
| 549 | Type::BOOLEAN => 'bool', |
||
| 550 | Type::DATETIME_IMMUTABLE => '\DateTimeImmutable', |
||
| 551 | Type::DATETIMETZ_IMMUTABLE => '\DateTimeImmutable', |
||
| 552 | Type::DATE_IMMUTABLE => '\DateTimeImmutable', |
||
| 553 | Type::TIME_IMMUTABLE => '\DateTimeImmutable', |
||
| 554 | Type::DECIMAL => 'string', |
||
| 555 | Type::INTEGER => 'int', |
||
| 556 | Type::OBJECT => 'string', |
||
| 557 | Type::SMALLINT => 'int', |
||
| 558 | Type::STRING => 'string', |
||
| 559 | Type::TEXT => 'string', |
||
| 560 | Type::BINARY => 'resource', |
||
| 561 | Type::BLOB => 'resource', |
||
| 562 | Type::FLOAT => 'float', |
||
| 563 | Type::GUID => 'string', |
||
| 564 | ]; |
||
| 565 | |||
| 566 | return $map[$type->getName()] ?? $type->getName(); |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param Table $table |
||
| 571 | * @return string[] |
||
| 572 | * @throws TDBMException |
||
| 573 | */ |
||
| 574 | public static function getPrimaryKeyColumnsOrFail(Table $table): array |
||
| 581 | } |
||
| 582 | } |
||
| 583 |