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 |
||
| 30 | class GenerateDynamicBundleCommand extends Command |
||
| 31 | { |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | const BUNDLE_NAMESPACE = 'GravitonDyn'; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | const BUNDLE_NAME_MASK = self::BUNDLE_NAMESPACE.'/%sBundle'; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | const GENERATION_HASHFILE_FILENAME = 'genhash'; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | private $bundleBundleNamespace; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $bundleBundleDir; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $bundleBundleClassname; |
||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $bundleBundleClassfile; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $bundleBundleList = []; |
||
| 56 | |||
| 57 | /** @var array|null */ |
||
| 58 | private $bundleAdditions = null; |
||
| 59 | |||
| 60 | /** @var array|null */ |
||
| 61 | private $serviceWhitelist = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var CommandRunner |
||
| 65 | */ |
||
| 66 | private $runner; |
||
| 67 | /** |
||
| 68 | * @var LoaderInterface |
||
| 69 | */ |
||
| 70 | private $definitionLoader; |
||
| 71 | /** |
||
| 72 | * @var SerializerInterface |
||
| 73 | */ |
||
| 74 | private $serializer; |
||
| 75 | |||
| 76 | |||
| 77 | /** |
||
| 78 | * @param CommandRunner $runner Runs a console command. |
||
| 79 | * @param LoaderInterface $definitionLoader JSON definition loader |
||
| 80 | * @param SerializerInterface $serializer Serializer |
||
| 81 | * @param string|null $bundleAdditions Additional bundles list in JSON format |
||
| 82 | * @param string|null $serviceWhitelist Service whitelist in JSON format |
||
| 83 | * @param string|null $name The name of the command; passing null means it must be set in |
||
| 84 | * configure() |
||
| 85 | */ |
||
| 86 | public function __construct( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritDoc} |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | protected function configure() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | * |
||
| 154 | * @param InputInterface $input input |
||
| 155 | * @param OutputInterface $output output |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * scans through all existing dynamic bundles, checks if there is a generation hash and collect that |
||
| 267 | * all in an array that can be used for fast checking. |
||
| 268 | * |
||
| 269 | * @param string $baseDir base directory of dynamic bundles |
||
| 270 | * |
||
| 271 | * @return array key is bundlepath, value is the current hash |
||
| 272 | */ |
||
| 273 | private function getExistingBundleHashes($baseDir) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * we cannot just delete the BundleBundle at the beginning, we need to prefill |
||
| 310 | * it with all existing dynamic bundles.. |
||
| 311 | * |
||
| 312 | * @param string $baseDir base dir |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | */ |
||
| 316 | private function createInitialBundleBundle($baseDir) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * from a name of a folder of a bundle, this function returns the corresponding class name |
||
| 333 | * |
||
| 334 | * @param string $folderName folder name |
||
| 335 | * |
||
| 336 | * @return string |
||
| 337 | */ |
||
| 338 | private function getBundleClassnameFromFolder($folderName) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * returns a finder that iterates all bundle directories |
||
| 349 | * |
||
| 350 | * @param string $baseDir the base dir to search |
||
| 351 | * |
||
| 352 | * @return Finder|null finder or null if basedir does not exist |
||
| 353 | */ |
||
| 354 | private function getBundleFinder($baseDir) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Calculates a hash of all templates that generator uses to output it's file. |
||
| 370 | * That way a regeneration will be triggered when one of them changes.. |
||
| 371 | * |
||
| 372 | * @return string hash |
||
| 373 | */ |
||
| 374 | private function getTemplateHash() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Generate Bundle entities |
||
| 388 | * |
||
| 389 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 390 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 391 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 392 | * |
||
| 393 | * @return void |
||
| 394 | * @throws \Exception |
||
| 395 | */ |
||
| 396 | protected function generateSubResources( |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Generate the actual Bundle |
||
| 417 | * |
||
| 418 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 419 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 420 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 421 | * |
||
| 422 | * @return void |
||
| 423 | */ |
||
| 424 | protected function generateMainResource(OutputInterface $output, JsonDefinition $jsonDef, $bundleName) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get all sub hashes |
||
| 443 | * |
||
| 444 | * @param JsonDefinition $definition Main JSON definition |
||
| 445 | * @return JsonDefinition[] |
||
| 446 | */ |
||
| 447 | protected function getSubResources(JsonDefinition $definition) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Gathers data for the command to run. |
||
| 469 | * |
||
| 470 | * @param array $arguments Set of cli arguments passed to the command |
||
| 471 | * @param OutputInterface $output Output channel to send messages to. |
||
| 472 | * @param JsonDefinition $jsonDef Configuration of the service |
||
| 473 | * |
||
| 474 | * @return void |
||
| 475 | * @throws \LogicException |
||
| 476 | */ |
||
| 477 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Generates a Bundle via command line (wrapping graviton:generate:bundle) |
||
| 490 | * |
||
| 491 | * @param string $namespace Namespace |
||
| 492 | * @param string $bundleName Name of bundle |
||
| 493 | * @param InputInterface $input Input |
||
| 494 | * @param OutputInterface $output Output |
||
| 495 | * @param string $deleteBefore Delete before directory |
||
| 496 | * |
||
| 497 | * @return void |
||
| 498 | * |
||
| 499 | * @throws \LogicException |
||
| 500 | */ |
||
| 501 | private function generateBundle( |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Generates our BundleBundle for dynamic bundles. |
||
| 532 | * It basically replaces the Bundle main class that got generated |
||
| 533 | * by the Sensio bundle task and it includes all of our bundles there. |
||
| 534 | * |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | private function generateBundleBundleClass() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Returns the field string as described in the json file |
||
| 556 | * |
||
| 557 | * @param JsonDefinition $jsonDef The json def |
||
| 558 | * |
||
| 559 | * @return string CommandLine string for the generator command |
||
| 560 | */ |
||
| 561 | private function getFieldString(JsonDefinition $jsonDef) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
| 580 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
| 581 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
| 582 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
| 583 | * |
||
| 584 | * @param string $routerBase router base |
||
| 585 | * |
||
| 586 | * @return bool true if yes, false if not |
||
| 587 | */ |
||
| 588 | private function isNotWhitelistedController($routerBase) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
| 599 | * |
||
| 600 | * @param string $bundleDir directory of the bundle |
||
| 601 | * @param string $hash the hash to save |
||
| 602 | * |
||
| 603 | * @return void |
||
| 604 | */ |
||
| 605 | private function generateGenerationHashFile($bundleDir, $hash) |
||
| 612 | } |
||
| 613 |
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.