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 |
||
| 34 | class GenerateDynamicBundleCommand extends Command |
||
| 35 | { |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | const BUNDLE_NAMESPACE = 'GravitonDyn'; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | const BUNDLE_NAME_MASK = self::BUNDLE_NAMESPACE.'/%sBundle'; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | const GENERATION_HASHFILE_FILENAME = 'genhash'; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | private $bundleBundleNamespace; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | private $bundleBundleDir; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $bundleBundleClassname; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $bundleBundleClassfile; |
||
| 57 | |||
| 58 | /** @var array */ |
||
| 59 | private $bundleBundleList = []; |
||
| 60 | |||
| 61 | /** @var array|null */ |
||
| 62 | private $bundleAdditions = null; |
||
| 63 | |||
| 64 | /** @var array|null */ |
||
| 65 | private $serviceWhitelist = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var CommandRunner |
||
| 69 | */ |
||
| 70 | private $runner; |
||
| 71 | /** |
||
| 72 | * @var LoaderInterface |
||
| 73 | */ |
||
| 74 | private $definitionLoader; |
||
| 75 | /** |
||
| 76 | * @var XmlManipulator |
||
| 77 | */ |
||
| 78 | private $xmlManipulator; |
||
| 79 | /** |
||
| 80 | * @var SerializerInterface |
||
| 81 | */ |
||
| 82 | private $serializer; |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * @param CommandRunner $runner Runs a console command. |
||
| 87 | * @param XmlManipulator $xmlManipulator Helper to change the content of a xml file. |
||
| 88 | * @param LoaderInterface $definitionLoader JSON definition loader |
||
| 89 | * @param SerializerInterface $serializer Serializer |
||
| 90 | * @param string|null $bundleAdditions Additional bundles list in JSON format |
||
| 91 | * @param string|null $serviceWhitelist Service whitelist in JSON format |
||
| 92 | * @param string|null $name The name of the command; passing null means it must be set in |
||
| 93 | * configure() |
||
| 94 | */ |
||
| 95 | 6 | public function __construct( |
|
| 118 | |||
| 119 | /** |
||
| 120 | * {@inheritDoc} |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 6 | protected function configure() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritDoc} |
||
| 164 | * |
||
| 165 | * @param InputInterface $input input |
||
| 166 | * @param OutputInterface $output output |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | 2 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * scans through all existing dynamic bundles, checks if there is a generation hash and collect that |
||
| 278 | * all in an array that can be used for fast checking. |
||
| 279 | * |
||
| 280 | * @param string $baseDir base directory of dynamic bundles |
||
| 281 | * |
||
| 282 | * @return array key is bundlepath, value is the current hash |
||
| 283 | */ |
||
| 284 | private function getExistingBundleHashes($baseDir) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Calculates a hash of all templates that generator uses to output it's file. |
||
| 322 | * That way a regeneration will be triggered when one of them changes.. |
||
| 323 | * |
||
| 324 | * @return string hash |
||
| 325 | */ |
||
| 326 | private function getTemplateHash() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Generate Bundle entities |
||
| 340 | * |
||
| 341 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 342 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 343 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
| 344 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 345 | * @param string $namespace Absolute path to the bundle root dir. |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | * @throws \Exception |
||
| 349 | */ |
||
| 350 | 4 | protected function generateSubResources( |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Generate the actual Bundle |
||
| 379 | * |
||
| 380 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 381 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 382 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | protected function generateMainResource(OutputInterface $output, JsonDefinition $jsonDef, $bundleName) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get all sub hashes |
||
| 404 | * |
||
| 405 | * @param JsonDefinition $definition Main JSON definition |
||
| 406 | * @return JsonDefinition[] |
||
| 407 | */ |
||
| 408 | 6 | protected function getSubResources(JsonDefinition $definition) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Gathers data for the command to run. |
||
| 430 | * |
||
| 431 | * @param array $arguments Set of cli arguments passed to the command |
||
| 432 | * @param OutputInterface $output Output channel to send messages to. |
||
| 433 | * @param JsonDefinition $jsonDef Configuration of the service |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | * @throws \LogicException |
||
| 437 | */ |
||
| 438 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Generates a Bundle via command line (wrapping graviton:generate:bundle) |
||
| 451 | * |
||
| 452 | * @param string $namespace Namespace |
||
| 453 | * @param string $bundleName Name of bundle |
||
| 454 | * @param InputInterface $input Input |
||
| 455 | * @param OutputInterface $output Output |
||
| 456 | * |
||
| 457 | * @return void |
||
| 458 | * |
||
| 459 | * @throws \LogicException |
||
| 460 | */ |
||
| 461 | private function generateBundle( |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Generates our BundleBundle for dynamic bundles. |
||
| 487 | * It basically replaces the Bundle main class that got generated |
||
| 488 | * by the Sensio bundle task and it includes all of our bundles there. |
||
| 489 | * |
||
| 490 | * @return void |
||
| 491 | */ |
||
| 492 | private function generateBundleBundleClass() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns the path to the generated validation.xml |
||
| 511 | * |
||
| 512 | * @param string $namespace Namespace |
||
| 513 | * |
||
| 514 | * @return string path |
||
| 515 | */ |
||
| 516 | private function getGeneratedValidationXmlPath($namespace) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns the field string as described in the json file |
||
| 523 | * |
||
| 524 | * @param JsonDefinition $jsonDef The json def |
||
| 525 | * |
||
| 526 | * @return string CommandLine string for the generator command |
||
| 527 | */ |
||
| 528 | private function getFieldString(JsonDefinition $jsonDef) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
| 547 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
| 548 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
| 549 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
| 550 | * |
||
| 551 | * @param string $routerBase router base |
||
| 552 | * |
||
| 553 | * @return bool true if yes, false if not |
||
| 554 | */ |
||
| 555 | private function isNotWhitelistedController($routerBase) |
||
| 563 | |||
| 564 | /** |
||
| 565 | * renders and stores the validation.xml file of a bundle. |
||
| 566 | * |
||
| 567 | * what are we doing here? |
||
| 568 | * well, when we started to generate our subclasses (hashes in our own service) as own |
||
| 569 | * Document classes, i had the problem that the validation.xml always got overwritten by the |
||
| 570 | * console task. sadly, validation.xml is one file for all classes in the bundle. |
||
| 571 | * so here we merge the generated validation.xml we saved in the loop before back into the |
||
| 572 | * final validation.xml again. the final result should be one validation.xml including all |
||
| 573 | * the validation rules for all the documents in this bundle. |
||
| 574 | * |
||
| 575 | * @todo we might just make this an option to the resource generator, i need to grok why this was an issue |
||
| 576 | * |
||
| 577 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
| 578 | * @param string $location Location where to store the file. |
||
| 579 | * |
||
| 580 | * @return void |
||
| 581 | */ |
||
| 582 | private function generateValidationXml(XmlManipulator $xmlManipulator, $location) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
| 593 | * |
||
| 594 | * @param string $bundleDir directory of the bundle |
||
| 595 | * @param string $hash the hash to save |
||
| 596 | * |
||
| 597 | * @return void |
||
| 598 | */ |
||
| 599 | private function generateGenerationHashFile($bundleDir, $hash) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Returns an XMLElement from a generated validation.xml that was generated during Resources generation. |
||
| 609 | * |
||
| 610 | * @param string $namespace Namespace, ie GravitonDyn\ShowcaseBundle |
||
| 611 | * |
||
| 612 | * @return \SimpleXMLElement The element |
||
| 613 | * |
||
| 614 | * @deprecated is this really used? |
||
| 615 | */ |
||
| 616 | public function getGeneratedValidationXml($namespace) |
||
| 628 | } |
||
| 629 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..