Complex classes like GenerateDynamicBundleCommand 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 GenerateDynamicBundleCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class GenerateDynamicBundleCommand extends Command |
||
| 33 | { |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | const BUNDLE_NAMESPACE = 'GravitonDyn'; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | const BUNDLE_NAME_MASK = self::BUNDLE_NAMESPACE.'/%sBundle'; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | const GENERATION_HASHFILE_FILENAME = 'genhash'; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $bundleBundleNamespace; |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | private $bundleBundleDir; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $bundleBundleClassname; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $bundleBundleClassfile; |
||
| 55 | |||
| 56 | /** @var array */ |
||
| 57 | private $bundleBundleList = []; |
||
| 58 | |||
| 59 | /** @var array|null */ |
||
| 60 | private $bundleAdditions = null; |
||
| 61 | |||
| 62 | /** @var array|null */ |
||
| 63 | private $serviceWhitelist = null; |
||
| 64 | /** |
||
| 65 | * @var CommandRunner |
||
| 66 | */ |
||
| 67 | private $runner; |
||
| 68 | /** |
||
| 69 | * @var LoaderInterface |
||
| 70 | */ |
||
| 71 | private $definitionLoader; |
||
| 72 | /** |
||
| 73 | * @var SerializerInterface |
||
| 74 | */ |
||
| 75 | private $serializer; |
||
| 76 | /** |
||
| 77 | * @var Filesystem |
||
| 78 | */ |
||
| 79 | private $fs; |
||
| 80 | /** |
||
| 81 | * @var BundleGenerator |
||
| 82 | */ |
||
| 83 | private $bundleGenerator; |
||
| 84 | /** |
||
| 85 | * @var ResourceGenerator |
||
| 86 | */ |
||
| 87 | private $resourceGenerator; |
||
| 88 | /** |
||
| 89 | * @var DynamicBundleBundleGenerator |
||
| 90 | */ |
||
| 91 | private $bundleBundleGenerator; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param LoaderInterface $definitionLoader JSON definition loader |
||
| 95 | * @param BundleGenerator $bundleGenerator bundle generator |
||
| 96 | * @param ResourceGenerator $resourceGenerator resource generator |
||
| 97 | * @param DynamicBundleBundleGenerator $bundleBundleGenerator bundlebundle generator |
||
| 98 | * @param SerializerInterface $serializer Serializer |
||
| 99 | * @param string|null $bundleAdditions Additional bundles list in JSON format |
||
| 100 | * @param string|null $serviceWhitelist Service whitelist in JSON format |
||
| 101 | * @param string|null $name name |
||
| 102 | */ |
||
| 103 | public function __construct( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritDoc} |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | protected function configure() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritDoc} |
||
| 168 | * |
||
| 169 | * @param InputInterface $input input |
||
| 170 | * @param OutputInterface $output output |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * scans through all existing dynamic bundles, checks if there is a generation hash and collect that |
||
| 284 | * all in an array that can be used for fast checking. |
||
| 285 | * |
||
| 286 | * @param string $baseDir base directory of dynamic bundles |
||
| 287 | * |
||
| 288 | * @return array key is bundlepath, value is the current hash |
||
| 289 | */ |
||
| 290 | private function getExistingBundleHashes($baseDir) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * from a name of a folder of a bundle, this function returns the corresponding class name |
||
| 327 | * |
||
| 328 | * @param string $folderName folder name |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | private function getBundleClassnameFromFolder($folderName) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * returns a finder that iterates all bundle directories |
||
| 343 | * |
||
| 344 | * @param string $baseDir the base dir to search |
||
| 345 | * |
||
| 346 | * @return Finder|null finder or null if basedir does not exist |
||
| 347 | */ |
||
| 348 | private function getBundleFinder($baseDir) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Calculates a hash of all templates that generator uses to output it's file. |
||
| 364 | * That way a regeneration will be triggered when one of them changes.. |
||
| 365 | * |
||
| 366 | * @return string hash |
||
| 367 | */ |
||
| 368 | private function getTemplateHash() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Generate Bundle entities |
||
| 382 | * |
||
| 383 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 384 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 385 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 386 | * @param string $bundleClassName class name |
||
| 387 | * |
||
| 388 | * @return void |
||
| 389 | * @throws \Exception |
||
| 390 | */ |
||
| 391 | protected function generateSubResources( |
||
| 409 | |||
| 410 | /** |
||
| 411 | * generates the resources of a bundle |
||
| 412 | * |
||
| 413 | * @param JsonDefinition $jsonDef definition |
||
| 414 | * @param string $bundleName name |
||
| 415 | * @param string $bundleDir dir |
||
| 416 | * @param string $bundleNamespace namespace |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | protected function generateResources( |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get all sub hashes |
||
| 462 | * |
||
| 463 | * @param JsonDefinition $definition Main JSON definition |
||
| 464 | * @return JsonDefinition[] |
||
| 465 | */ |
||
| 466 | protected function getSubResources(JsonDefinition $definition) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Gathers data for the command to run. |
||
| 488 | * |
||
| 489 | * @param array $arguments Set of cli arguments passed to the command |
||
| 490 | * @param OutputInterface $output Output channel to send messages to. |
||
| 491 | * @param JsonDefinition $jsonDef Configuration of the service |
||
| 492 | * |
||
| 493 | * @return void |
||
| 494 | * @throws \LogicException |
||
| 495 | */ |
||
| 496 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * generates the basic bundle structure |
||
| 509 | * |
||
| 510 | * @param string $namespace Namespace |
||
| 511 | * @param string $bundleName Name of bundle |
||
| 512 | * @param string $targetDir target directory |
||
| 513 | * |
||
| 514 | * @return void |
||
| 515 | * |
||
| 516 | * @throws \LogicException |
||
| 517 | */ |
||
| 518 | private function generateBundle( |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Generates our BundleBundle for dynamic bundles. |
||
| 533 | * It basically replaces the Bundle main class that got generated |
||
| 534 | * by the Sensio bundle task and it includes all of our bundles there. |
||
| 535 | * |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | private function generateBundleBundleClass() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
| 557 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
| 558 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
| 559 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
| 560 | * |
||
| 561 | * @param string $routerBase router base |
||
| 562 | * |
||
| 563 | * @return bool true if yes, false if not |
||
| 564 | */ |
||
| 565 | private function isNotWhitelistedController($routerBase) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
| 576 | * |
||
| 577 | * @param string $bundleDir directory of the bundle |
||
| 578 | * @param string $hash the hash to save |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | private function generateGenerationHashFile($bundleDir, $hash) |
||
| 589 | } |
||
| 590 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.