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( |
|
| 87 | CommandRunner $runner, |
||
| 88 | LoaderInterface $definitionLoader, |
||
| 89 | SerializerInterface $serializer, |
||
| 90 | $bundleAdditions = null, |
||
| 91 | $serviceWhitelist = null, |
||
| 92 | $name = null |
||
| 93 | ) { |
||
| 94 | 4 | parent::__construct($name); |
|
| 95 | |||
| 96 | 4 | $this->runner = $runner; |
|
| 97 | 4 | $this->definitionLoader = $definitionLoader; |
|
| 98 | 4 | $this->serializer = $serializer; |
|
| 99 | |||
| 100 | 4 | if ($bundleAdditions !== null && $bundleAdditions !== '') { |
|
| 101 | $this->bundleAdditions = $bundleAdditions; |
||
| 102 | } |
||
| 103 | 4 | if ($serviceWhitelist !== null && $serviceWhitelist !== '') { |
|
| 104 | $this->serviceWhitelist = $serviceWhitelist; |
||
| 105 | } |
||
| 106 | 4 | } |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritDoc} |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | 4 | protected function configure() |
|
| 114 | { |
||
| 115 | 4 | parent::configure(); |
|
| 116 | |||
| 117 | 4 | $this->addOption( |
|
| 118 | 4 | 'json', |
|
| 119 | 4 | '', |
|
| 120 | 4 | InputOption::VALUE_OPTIONAL, |
|
| 121 | 2 | 'Path to the json definition.' |
|
| 122 | 2 | ) |
|
| 123 | 4 | ->addOption( |
|
| 124 | 4 | 'srcDir', |
|
| 125 | 4 | '', |
|
| 126 | 4 | InputOption::VALUE_OPTIONAL, |
|
| 127 | 4 | 'Src Dir', |
|
| 128 | 4 | dirname(__FILE__) . '/../../../' |
|
| 129 | 2 | ) |
|
| 130 | 4 | ->addOption( |
|
| 131 | 4 | 'bundleBundleName', |
|
| 132 | 4 | '', |
|
| 133 | 4 | InputOption::VALUE_OPTIONAL, |
|
| 134 | 4 | 'Which BundleBundle to manipulate to add our stuff', |
|
| 135 | 2 | 'GravitonDynBundleBundle' |
|
| 136 | 2 | ) |
|
| 137 | 4 | ->addOption( |
|
| 138 | 4 | 'bundleFormat', |
|
| 139 | 4 | '', |
|
| 140 | 4 | InputOption::VALUE_OPTIONAL, |
|
| 141 | 4 | 'Which format', |
|
| 142 | 2 | 'xml' |
|
| 143 | 2 | ) |
|
| 144 | 4 | ->setName('graviton:generate:dynamicbundles') |
|
| 145 | 4 | ->setDescription( |
|
| 146 | 'Generates all dynamic bundles in the GravitonDyn namespace. Either give a path '. |
||
| 147 | 2 | 'to a single JSON file or a directory path containing multiple files.' |
|
| 148 | 2 | ); |
|
| 149 | 4 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | * |
||
| 154 | * @param InputInterface $input input |
||
| 155 | * @param OutputInterface $output output |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 2 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 160 | { |
||
| 161 | /** |
||
| 162 | * GENERATE THE BUNDLEBUNDLE |
||
| 163 | */ |
||
| 164 | 2 | $namespace = sprintf(self::BUNDLE_NAME_MASK, 'Bundle'); |
|
| 165 | |||
| 166 | // GravitonDynBundleBundle |
||
| 167 | 2 | $bundleName = str_replace('/', '', $namespace); |
|
| 168 | |||
| 169 | // bundlebundle stuff.. |
||
| 170 | 2 | $this->bundleBundleNamespace = $namespace; |
|
| 171 | 2 | $this->bundleBundleDir = $input->getOption('srcDir') . $namespace; |
|
| 172 | 2 | $this->bundleBundleClassname = $bundleName; |
|
| 173 | 2 | $this->bundleBundleClassfile = $this->bundleBundleDir . '/' . $this->bundleBundleClassname . '.php'; |
|
| 174 | |||
| 175 | 2 | $filesToWorkOn = $this->definitionLoader->load($input->getOption('json')); |
|
| 176 | |||
| 177 | 2 | if (count($filesToWorkOn) < 1) { |
|
| 178 | 2 | throw new \LogicException("Could not find any usable JSON files."); |
|
| 179 | } |
||
| 180 | |||
| 181 | $fs = new Filesystem(); |
||
| 182 | |||
| 183 | $this->createInitialBundleBundle($input->getOption('srcDir')); |
||
| 184 | |||
| 185 | $templateHash = $this->getTemplateHash(); |
||
| 186 | $existingBundles = $this->getExistingBundleHashes($input->getOption('srcDir')); |
||
| 187 | |||
| 188 | /** |
||
| 189 | * GENERATE THE BUNDLE(S) |
||
| 190 | */ |
||
| 191 | foreach ($filesToWorkOn as $jsonDef) { |
||
| 192 | $thisIdName = $jsonDef->getId(); |
||
| 193 | $namespace = sprintf(self::BUNDLE_NAME_MASK, $thisIdName); |
||
| 194 | |||
| 195 | $jsonDef->setNamespace($namespace); |
||
| 196 | |||
| 197 | $bundleName = str_replace('/', '', $namespace); |
||
| 198 | $this->bundleBundleList[] = $namespace; |
||
| 199 | |||
| 200 | try { |
||
| 201 | $bundleDir = $input->getOption('srcDir').$namespace; |
||
| 202 | $thisHash = sha1($templateHash.PATH_SEPARATOR.serialize($jsonDef)); |
||
| 203 | |||
| 204 | $needsGeneration = true; |
||
| 205 | if (isset($existingBundles[$bundleDir])) { |
||
| 206 | if ($existingBundles[$bundleDir] == $thisHash) { |
||
| 207 | $needsGeneration = false; |
||
| 208 | } |
||
| 209 | unset($existingBundles[$bundleDir]); |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($needsGeneration) { |
||
| 213 | $this->generateBundle($namespace, $bundleName, $input, $output, $bundleDir); |
||
| 214 | $this->generateGenerationHashFile($bundleDir, $thisHash); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->generateBundleBundleClass(); |
||
| 218 | |||
| 219 | if ($needsGeneration) { |
||
| 220 | $this->generateSubResources($output, $jsonDef, $bundleName); |
||
| 221 | $this->generateMainResource($output, $jsonDef, $bundleName); |
||
| 222 | |||
| 223 | $output->write( |
||
| 224 | PHP_EOL. |
||
| 225 | sprintf('<info>Generated "%s" from definition %s</info>', $bundleName, $jsonDef->getId()). |
||
| 226 | PHP_EOL |
||
| 227 | ); |
||
| 228 | } else { |
||
| 229 | $output->write( |
||
| 230 | PHP_EOL. |
||
| 231 | sprintf('<info>Using pre-existing "%s"</info>', $bundleName). |
||
| 232 | PHP_EOL |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | } catch (\Exception $e) { |
||
| 236 | $output->writeln( |
||
| 237 | sprintf('<error>%s</error>', $e->getMessage()) |
||
| 238 | ); |
||
| 239 | |||
| 240 | // remove failed bundle from list |
||
| 241 | array_pop($this->bundleBundleList); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | // whatever is left in $existingBundles is not defined anymore and needs to be deleted.. |
||
| 246 | foreach ($existingBundles as $dirName => $hash) { |
||
| 247 | $fileInfo = new \SplFileInfo($dirName); |
||
| 248 | $bundleClassName = $this->getBundleClassnameFromFolder($fileInfo->getFilename()); |
||
| 249 | |||
| 250 | // remove from bundlebundle list |
||
| 251 | unset($this->bundleBundleList[array_search($bundleClassName, $this->bundleBundleList)]); |
||
| 252 | |||
| 253 | $fs->remove($dirName); |
||
| 254 | |||
| 255 | $output->write( |
||
| 256 | PHP_EOL. |
||
| 257 | sprintf('<info>Deleted obsolete bundle "%s"</info>', $dirName). |
||
| 258 | PHP_EOL |
||
| 259 | ); |
||
| 260 | } |
||
| 261 | |||
| 262 | $this->generateBundleBundleClass(); |
||
| 263 | } |
||
| 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) |
||
| 274 | { |
||
| 275 | $existingBundles = []; |
||
| 276 | $fs = new Filesystem(); |
||
| 277 | $bundleBaseDir = $baseDir.self::BUNDLE_NAMESPACE; |
||
| 278 | |||
| 279 | if (!$fs->exists($bundleBaseDir)) { |
||
| 280 | return $existingBundles; |
||
| 281 | } |
||
| 282 | |||
| 283 | $bundleFinder = $this->getBundleFinder($baseDir); |
||
| 284 | |||
| 285 | foreach ($bundleFinder as $bundleDir) { |
||
|
|
|||
| 286 | $genHash = ''; |
||
| 287 | $hashFileFinder = new Finder(); |
||
| 288 | $hashFileIterator = $hashFileFinder |
||
| 289 | ->files() |
||
| 290 | ->in($bundleDir->getPathname()) |
||
| 291 | ->name(self::GENERATION_HASHFILE_FILENAME) |
||
| 292 | ->depth('== 0') |
||
| 293 | ->getIterator(); |
||
| 294 | |||
| 295 | $hashFileIterator->rewind(); |
||
| 296 | |||
| 297 | $hashFile = $hashFileIterator->current(); |
||
| 298 | if ($hashFile instanceof SplFileInfo) { |
||
| 299 | $genHash = $hashFile->getContents(); |
||
| 300 | } |
||
| 301 | |||
| 302 | $existingBundles[$bundleDir->getPathname()] = $genHash; |
||
| 303 | } |
||
| 304 | |||
| 305 | return $existingBundles; |
||
| 306 | } |
||
| 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) |
||
| 317 | { |
||
| 318 | $bundleFinder = $this->getBundleFinder($baseDir); |
||
| 319 | |||
| 320 | if (!$bundleFinder) { |
||
| 321 | return; |
||
| 322 | } |
||
| 323 | |||
| 324 | foreach ($bundleFinder as $bundleDir) { |
||
| 325 | $this->bundleBundleList[] = $this->getBundleClassnameFromFolder($bundleDir->getFilename()); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->generateBundleBundleClass(); |
||
| 329 | } |
||
| 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) |
||
| 339 | { |
||
| 340 | if (substr($folderName, -6) == 'Bundle') { |
||
| 341 | $folderName = substr($folderName, 0, -6); |
||
| 342 | } |
||
| 343 | |||
| 344 | return sprintf(self::BUNDLE_NAME_MASK, $folderName); |
||
| 345 | } |
||
| 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 | /** |
||
| 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 | 6 | protected function getSubResources(JsonDefinition $definition) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Gathers data for the command to run. |
||
| 464 | * |
||
| 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.