| Conditions | 7 |
| Paths | 18 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 77 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 78 | { |
||
| 79 | $this->router->getContext()->setHost($input->getArgument('host')); |
||
|
|
|||
| 80 | $this->router->getContext()->setScheme($input->getOption('scheme')); |
||
| 81 | $this->router->getContext()->setBaseUrl($input->getOption('baseurl')); |
||
| 82 | |||
| 83 | $tempFolder = sys_get_temp_dir().'/sonata_sitemap_'.md5(__DIR__); |
||
| 84 | |||
| 85 | $fs = new Filesystem(); |
||
| 86 | |||
| 87 | // step 1 |
||
| 88 | $output->writeln(sprintf('Creating temporary folder: %s', $tempFolder)); |
||
| 89 | |||
| 90 | if ($fs->exists($tempFolder)) { |
||
| 91 | $output->writeln('<error>The temporary folder already exists</error>'); |
||
| 92 | $output->writeln('<error>If the task is not running please delete this folder</error>'); |
||
| 93 | |||
| 94 | return 1; |
||
| 95 | } |
||
| 96 | |||
| 97 | $fs->mkdir($tempFolder); |
||
| 98 | |||
| 99 | // step 2 |
||
| 100 | $output->writeln(sprintf('Generating sitemap - this can take a while')); |
||
| 101 | foreach ($this->sitemapManager as $group => $sitemap) { |
||
| 102 | $write = new SitemapWriter($tempFolder, $group, $sitemap->types, false); |
||
| 103 | |||
| 104 | try { |
||
| 105 | Handler::create($sitemap->sources, $write)->export(); |
||
| 106 | } catch (\Exception $e) { |
||
| 107 | $fs->remove($tempFolder); |
||
| 108 | |||
| 109 | throw $e; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // generate global sitemap index |
||
| 114 | $appendPath = $input->hasOption('sitemap_path') ? $input->getOption('sitemap_path') : $input->getOption('baseurl'); |
||
| 115 | SitemapWriter::generateSitemapIndex($tempFolder, sprintf('%s://%s%s', $input->getOption('scheme'), $input->getArgument('host'), $appendPath), 'sitemap*.xml', 'sitemap.xml'); |
||
| 116 | |||
| 117 | // step 3 |
||
| 118 | $output->writeln(sprintf('Moving temporary file to %s ...', $input->getArgument('folder'))); |
||
| 119 | |||
| 120 | $oldFiles = Finder::create()->files()->name('sitemap*.xml')->in($input->getArgument('folder')); |
||
| 121 | foreach ($oldFiles as $file) { |
||
| 122 | $fs->remove($file->getRealPath()); |
||
| 123 | } |
||
| 124 | |||
| 125 | $newFiles = Finder::create()->files()->name('sitemap*.xml')->in($tempFolder); |
||
| 126 | foreach ($newFiles as $file) { |
||
| 127 | $fs->rename($file->getRealPath(), sprintf('%s/%s', $input->getArgument('folder'), $file->getFilename())); |
||
| 128 | } |
||
| 129 | |||
| 130 | $fs->remove($tempFolder); |
||
| 131 | |||
| 132 | $output->writeln('<info>done!</info>'); |
||
| 133 | } |
||
| 134 | } |
||
| 135 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.