| Conditions | 8 |
| Paths | 25 |
| Total Lines | 72 |
| Code Lines | 40 |
| 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 declare(strict_types=1); |
||
| 60 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 61 | { |
||
| 62 | $io = new SymfonyStyle($input, $output); |
||
| 63 | |||
| 64 | $files = (new Finder()) |
||
| 65 | ->in($this->platformUpdatesPath) |
||
| 66 | ->files() |
||
| 67 | ->sortByName() |
||
| 68 | ->depth('0') |
||
| 69 | ->name('*.md') |
||
| 70 | ->exclude('_archive.md') |
||
| 71 | ->getIterator(); |
||
| 72 | |||
| 73 | $filesInOrder = array_reverse(iterator_to_array($files)); |
||
| 74 | |||
| 75 | $filesSorted = []; |
||
| 76 | |||
| 77 | /** @var SplFileInfo $file */ |
||
| 78 | foreach ($filesInOrder as $file) { |
||
| 79 | $baseName = $file->getBasename('.md'); |
||
| 80 | |||
| 81 | if ($baseName === '_archive') { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | |||
| 85 | $io->write("* Rendering: $baseName \n"); |
||
| 86 | |||
| 87 | $parts = explode('-', $baseName); |
||
| 88 | |||
| 89 | if (count($parts) < 3) { |
||
| 90 | throw new \Exception(sprintf('File %s is invalidly named', $file->getRelativePathname())); |
||
| 91 | } |
||
| 92 | |||
| 93 | $date = \DateTimeImmutable::createFromFormat('Y-m-d', implode('-', array_slice($parts, 0, 3))); |
||
| 94 | |||
| 95 | $month = $date->format('Y-m'); |
||
| 96 | |||
| 97 | if (!isset($filesSorted[$month])) { |
||
| 98 | $filesSorted[$month] = []; |
||
| 99 | } |
||
| 100 | |||
| 101 | $filesSorted[$month][] = new PlatformUpdatesDocument($date, $file, false, ''); |
||
| 102 | } |
||
| 103 | |||
| 104 | $rendered = [self::TEMPLATE_HEADER]; |
||
| 105 | foreach ($filesSorted as $month => $documents) { |
||
| 106 | $rendered[] = sprintf(self::TEMPLATE_MONTH, |
||
| 107 | \DateTimeImmutable::createFromFormat('Y-m', $month)->format('F Y') |
||
| 108 | ); |
||
| 109 | |||
| 110 | /** @var PlatformUpdatesDocument $document */ |
||
| 111 | foreach ($documents as $document) { |
||
| 112 | $rendered[] = sprintf(self::TEMPLATE_HEADLINE, |
||
| 113 | $document->getDate()->format('Y-m-d'), |
||
| 114 | $document->getMetadata()->getTitleEn() |
||
| 115 | ); |
||
| 116 | |||
| 117 | $rendered[] = $document->getHtml()->render(new DocumentTree())->getContents(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $archivedfile = $this->platformUpdatesPath . '/_archive.md'; |
||
| 122 | |||
| 123 | if (file_exists($archivedfile)) { |
||
| 124 | $io->note('Loading archive file'); |
||
| 125 | $rendered[] = file_get_contents($archivedfile); |
||
| 126 | } |
||
| 127 | |||
| 128 | $fileContents = implode(PHP_EOL, $rendered); |
||
| 129 | file_put_contents($this->targetFile, $fileContents); |
||
| 130 | |||
| 131 | $io->success('Done'); |
||
| 132 | } |
||
| 134 |