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) |
|
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 | * we cannot just delete the BundleBundle at the beginning, we need to prefill |
||
327 | * it with all existing dynamic bundles.. |
||
328 | * |
||
329 | * @param string $baseDir base dir |
||
330 | * |
||
331 | * @return void |
||
332 | */ |
||
333 | private function createInitialBundleBundle($baseDir) |
||
347 | |||
348 | /** |
||
349 | * from a name of a folder of a bundle, this function returns the corresponding class name |
||
350 | * |
||
351 | * @param string $folderName folder name |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | private function getBundleClassnameFromFolder($folderName) |
||
363 | |||
364 | /** |
||
365 | * returns a finder that iterates all bundle directories |
||
366 | * |
||
367 | * @param string $baseDir the base dir to search |
||
368 | * |
||
369 | * @return Finder|null finder or null if basedir does not exist |
||
370 | */ |
||
371 | private function getBundleFinder($baseDir) |
||
384 | |||
385 | /** |
||
386 | * Calculates a hash of all templates that generator uses to output it's file. |
||
387 | * That way a regeneration will be triggered when one of them changes.. |
||
388 | * |
||
389 | * @return string hash |
||
390 | */ |
||
391 | private function getTemplateHash() |
||
402 | |||
403 | /** |
||
404 | * Generate Bundle entities |
||
405 | * |
||
406 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
407 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
408 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
409 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
410 | * @param string $namespace Absolute path to the bundle root dir. |
||
411 | * |
||
412 | * @return void |
||
413 | * @throws \Exception |
||
414 | */ |
||
415 | 4 | protected function generateSubResources( |
|
442 | |||
443 | /** |
||
444 | * Generate the actual Bundle |
||
445 | * |
||
446 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
447 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
448 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
449 | * |
||
450 | * @return void |
||
451 | */ |
||
452 | protected function generateMainResource(OutputInterface $output, JsonDefinition $jsonDef, $bundleName) |
||
468 | |||
469 | /** |
||
470 | * Get all sub hashes |
||
471 | * |
||
472 | * @param JsonDefinition $definition Main JSON definition |
||
473 | * @return JsonDefinition[] |
||
474 | */ |
||
475 | 6 | protected function getSubResources(JsonDefinition $definition) |
|
494 | |||
495 | /** |
||
496 | * Gathers data for the command to run. |
||
497 | * |
||
498 | * @param array $arguments Set of cli arguments passed to the command |
||
499 | * @param OutputInterface $output Output channel to send messages to. |
||
500 | * @param JsonDefinition $jsonDef Configuration of the service |
||
501 | * |
||
502 | * @return void |
||
503 | * @throws \LogicException |
||
504 | */ |
||
505 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
515 | |||
516 | /** |
||
517 | * Generates a Bundle via command line (wrapping graviton:generate:bundle) |
||
518 | * |
||
519 | * @param string $namespace Namespace |
||
520 | * @param string $bundleName Name of bundle |
||
521 | * @param InputInterface $input Input |
||
522 | * @param OutputInterface $output Output |
||
523 | * @param string $deleteBefore Delete before directory |
||
524 | * |
||
525 | * @return void |
||
526 | * |
||
527 | * @throws \LogicException |
||
528 | */ |
||
529 | private function generateBundle( |
||
558 | |||
559 | /** |
||
560 | * Generates our BundleBundle for dynamic bundles. |
||
561 | * It basically replaces the Bundle main class that got generated |
||
562 | * by the Sensio bundle task and it includes all of our bundles there. |
||
563 | * |
||
564 | * @return void |
||
565 | */ |
||
566 | private function generateBundleBundleClass() |
||
582 | |||
583 | /** |
||
584 | * Returns the path to the generated validation.xml |
||
585 | * |
||
586 | * @param string $namespace Namespace |
||
587 | * |
||
588 | * @return string path |
||
589 | */ |
||
590 | private function getGeneratedValidationXmlPath($namespace) |
||
594 | |||
595 | /** |
||
596 | * Returns the field string as described in the json file |
||
597 | * |
||
598 | * @param JsonDefinition $jsonDef The json def |
||
599 | * |
||
600 | * @return string CommandLine string for the generator command |
||
601 | */ |
||
602 | private function getFieldString(JsonDefinition $jsonDef) |
||
618 | |||
619 | /** |
||
620 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
621 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
622 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
623 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
624 | * |
||
625 | * @param string $routerBase router base |
||
626 | * |
||
627 | * @return bool true if yes, false if not |
||
628 | */ |
||
629 | private function isNotWhitelistedController($routerBase) |
||
637 | |||
638 | /** |
||
639 | * renders and stores the validation.xml file of a bundle. |
||
640 | * |
||
641 | * what are we doing here? |
||
642 | * well, when we started to generate our subclasses (hashes in our own service) as own |
||
643 | * Document classes, i had the problem that the validation.xml always got overwritten by the |
||
644 | * console task. sadly, validation.xml is one file for all classes in the bundle. |
||
645 | * so here we merge the generated validation.xml we saved in the loop before back into the |
||
646 | * final validation.xml again. the final result should be one validation.xml including all |
||
647 | * the validation rules for all the documents in this bundle. |
||
648 | * |
||
649 | * @todo we might just make this an option to the resource generator, i need to grok why this was an issue |
||
650 | * |
||
651 | * @param XmlManipulator $xmlManipulator Helper to safe the validation xml file. |
||
652 | * @param string $location Location where to store the file. |
||
653 | * |
||
654 | * @return void |
||
655 | */ |
||
656 | private function generateValidationXml(XmlManipulator $xmlManipulator, $location) |
||
664 | |||
665 | /** |
||
666 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
667 | * |
||
668 | * @param string $bundleDir directory of the bundle |
||
669 | * @param string $hash the hash to save |
||
670 | * |
||
671 | * @return void |
||
672 | */ |
||
673 | private function generateGenerationHashFile($bundleDir, $hash) |
||
680 | |||
681 | /** |
||
682 | * Returns an XMLElement from a generated validation.xml that was generated during Resources generation. |
||
683 | * |
||
684 | * @param string $namespace Namespace, ie GravitonDyn\ShowcaseBundle |
||
685 | * |
||
686 | * @return \SimpleXMLElement The element |
||
687 | * |
||
688 | * @deprecated is this really used? |
||
689 | */ |
||
690 | public function getGeneratedValidationXml($namespace) |
||
702 | } |
||
703 |
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..