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 | 4 | public function __construct( |
|
107 | |||
108 | /** |
||
109 | * {@inheritDoc} |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | 4 | 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 | 4 | protected function generateSubResources( |
|
412 | |||
413 | 4 | /** |
|
414 | * Generate the actual Bundle |
||
415 | * |
||
416 | * @param OutputInterface $output Instance to sent text to be displayed on stout. |
||
417 | * @param JsonDefinition $jsonDef Configuration to be generated the entity from. |
||
418 | * @param string $bundleName Name of the bundle the entity shall be generated for. |
||
419 | * |
||
420 | * @return void |
||
421 | */ |
||
422 | protected function generateMainResource(OutputInterface $output, JsonDefinition $jsonDef, $bundleName) |
||
435 | |||
436 | /** |
||
437 | * Get all sub hashes |
||
438 | * |
||
439 | * @param JsonDefinition $definition Main JSON definition |
||
440 | * @return JsonDefinition[] |
||
441 | */ |
||
442 | protected function getSubResources(JsonDefinition $definition) |
||
461 | 2 | ||
462 | /** |
||
463 | * Gathers data for the command to run. |
||
464 | 6 | * |
|
465 | * @param array $arguments Set of cli arguments passed to the command |
||
466 | * @param OutputInterface $output Output channel to send messages to. |
||
467 | * @param JsonDefinition $jsonDef Configuration of the service |
||
468 | * |
||
469 | * @return void |
||
470 | * @throws \LogicException |
||
471 | */ |
||
472 | private function generateResource(array $arguments, OutputInterface $output, JsonDefinition $jsonDef) |
||
482 | |||
483 | /** |
||
484 | * Generates a Bundle via command line (wrapping graviton:generate:bundle) |
||
485 | * |
||
486 | * @param string $namespace Namespace |
||
487 | * @param string $bundleName Name of bundle |
||
488 | * @param InputInterface $input Input |
||
489 | * @param OutputInterface $output Output |
||
490 | * @param string $deleteBefore Delete before directory |
||
491 | * |
||
492 | * @return void |
||
493 | * |
||
494 | * @throws \LogicException |
||
495 | */ |
||
496 | private function generateBundle( |
||
524 | |||
525 | /** |
||
526 | * Generates our BundleBundle for dynamic bundles. |
||
527 | * It basically replaces the Bundle main class that got generated |
||
528 | * by the Sensio bundle task and it includes all of our bundles there. |
||
529 | * |
||
530 | * @return void |
||
531 | */ |
||
532 | private function generateBundleBundleClass() |
||
548 | |||
549 | /** |
||
550 | * Checks an optional environment setting if this $routerBase is whitelisted there. |
||
551 | * If something is 'not whitelisted' (return true) means that the controller should not be generated. |
||
552 | * This serves as a lowlevel possibility to disable the generation of certain controllers. |
||
553 | * If we have no whitelist defined, we consider that all services should be generated (default). |
||
554 | * |
||
555 | * @param string $routerBase router base |
||
556 | * |
||
557 | * @return bool true if yes, false if not |
||
558 | */ |
||
559 | private function isNotWhitelistedController($routerBase) |
||
567 | |||
568 | /** |
||
569 | * Generates the file containing the hash to determine if this bundle needs regeneration |
||
570 | * |
||
571 | * @param string $bundleDir directory of the bundle |
||
572 | * @param string $hash the hash to save |
||
573 | * |
||
574 | * @return void |
||
575 | */ |
||
576 | private function generateGenerationHashFile($bundleDir, $hash) |
||
583 | } |
||
584 |
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.