Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * scans through all existing dynamic bundles, checks if there is a generation hash and collect that |
||
| 275 | * all in an array that can be used for fast checking. |
||
| 276 | * |
||
| 277 | * @param string $baseDir base directory of dynamic bundles |
||
| 278 | * |
||
| 279 | * @return array key is bundlepath, value is the current hash |
||
| 280 | */ |
||
| 281 | private function getExistingBundleHashes($baseDir) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * we cannot just delete the BundleBundle at the beginning, we need to prefill |
||
| 318 | * it with all existing dynamic bundles.. |
||
| 319 | * |
||
| 320 | * @param string $baseDir base dir |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | private function createInitialBundleBundle($baseDir) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * returns a finder that iterates all bundle directories |
||
| 345 | * |
||
| 346 | * @param string $baseDir the base dir to search |
||
| 347 | * |
||
| 348 | * @return Finder|null finder or null if basedir does not exist |
||
| 349 | */ |
||
| 350 | private function getBundleFinder($baseDir) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Calculates a hash of all templates that generator uses to output it's file. |
||
| 366 | * That way a regeneration will be triggered when one of them changes.. |
||
| 367 | * |
||
| 368 | * @return string hash |
||
| 369 | */ |
||
| 370 | private function getTemplateHash() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Generate Bundle entities |
||
| 384 | * |
||
| 385 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 386 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 387 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
| 388 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 389 | * @param string $namespace Absolute path to the bundle root dir. |
||
| 390 | * |
||
| 391 | * @return void |
||
| 392 | * @throws \Exception |
||
| 393 | */ |
||
| 394 | 4 | protected function generateSubResources( |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Generate the actual Bundle |
||
| 424 | * |
||
| 425 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
| 426 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
| 427 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | protected function generateMainResource(OutputInterface $output, JsonDefinition $jsonDef, $bundleName) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Get all sub hashes |
||
| 450 | * |
||
| 451 | * @param JsonDefinition $definition Main JSON definition |
||
| 452 | * @return JsonDefinition[] |
||
| 453 | */ |
||
| 454 | 6 | protected function getSubResources(JsonDefinition $definition) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Gathers data for the command to run. |
||
| 476 | * |
||
| 477 | * @param array $arguments Set of cli arguments passed to the command |
||
| 478 | * @param OutputInterface $output Output channel to send messages to. |
||
| 479 | * @param JsonDefinition $jsonDef Configuration of the service |
||
| 480 | * |
||
| 481 | * @return void |
||
| 482 | * @throws \LogicException |
||
| 483 | */ |
||
| 484 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Generates a Bundle via command line (wrapping graviton:generate:bundle) |
||
| 497 | * |
||
| 498 | * @param string $namespace Namespace |
||
| 499 | * @param string $bundleName Name of bundle |
||
| 500 | * @param InputInterface $input Input |
||
| 501 | * @param OutputInterface $output Output |
||
| 502 | * @param string $deleteBefore Delete before directory |
||
| 503 | * |
||
| 504 | * @return void |
||
| 505 | * |
||
| 506 | * @throws \LogicException |
||
| 507 | */ |
||
| 508 | private function generateBundle( |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Generates our BundleBundle for dynamic bundles. |
||
| 540 | * It basically replaces the Bundle main class that got generated |
||
| 541 | * by the Sensio bundle task and it includes all of our bundles there. |
||
| 542 | * |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | private function generateBundleBundleClass() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Returns the path to the generated validation.xml |
||
| 564 | * |
||
| 565 | * @param string $namespace Namespace |
||
| 566 | * |
||
| 567 | * @return string path |
||
| 568 | */ |
||
| 569 | private function getGeneratedValidationXmlPath($namespace) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns the field string as described in the json file |
||
| 576 | * |
||
| 577 | * @param JsonDefinition $jsonDef The json def |
||
| 578 | * |
||
| 579 | * @return string CommandLine string for the generator command |
||
| 580 | */ |
||
| 581 | private function getFieldString(JsonDefinition $jsonDef) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
| 600 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
| 601 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
| 602 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
| 603 | * |
||
| 604 | * @param string $routerBase router base |
||
| 605 | * |
||
| 606 | * @return bool true if yes, false if not |
||
| 607 | */ |
||
| 608 | private function isNotWhitelistedController($routerBase) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * renders and stores the validation.xml file of a bundle. |
||
| 619 | * |
||
| 620 | * what are we doing here? |
||
| 621 | * well, when we started to generate our subclasses (hashes in our own service) as own |
||
| 622 | * Document classes, i had the problem that the validation.xml always got overwritten by the |
||
| 623 | * console task. sadly, validation.xml is one file for all classes in the bundle. |
||
| 624 | * so here we merge the generated validation.xml we saved in the loop before back into the |
||
| 625 | * final validation.xml again. the final result should be one validation.xml including all |
||
| 626 | * the validation rules for all the documents in this bundle. |
||
| 627 | * |
||
| 628 | * @todo we might just make this an option to the resource generator, i need to grok why this was an issue |
||
| 629 | * |
||
| 630 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
| 631 | * @param string $location Location where to store the file. |
||
| 632 | * |
||
| 633 | * @return void |
||
| 634 | */ |
||
| 635 | private function generateValidationXml(XmlManipulator $xmlManipulator, $location) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
| 646 | * |
||
| 647 | * @param string $bundleDir directory of the bundle |
||
| 648 | * @param string $hash the hash to save |
||
| 649 | * |
||
| 650 | * @return void |
||
| 651 | */ |
||
| 652 | private function generateGenerationHashFile($bundleDir, $hash) |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Returns an XMLElement from a generated validation.xml that was generated during Resources generation. |
||
| 662 | * |
||
| 663 | * @param string $namespace Namespace, ie GravitonDyn\ShowcaseBundle |
||
| 664 | * |
||
| 665 | * @return \SimpleXMLElement The element |
||
| 666 | * |
||
| 667 | * @deprecated is this really used? |
||
| 668 | */ |
||
| 669 | public function getGeneratedValidationXml($namespace) |
||
| 681 | } |
||
| 682 |
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..