| Conditions | 10 |
| Paths | 18 |
| Total Lines | 102 |
| Code Lines | 88 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 63 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 64 | { |
||
| 65 | $versionReplacer = function (string $ver) { |
||
| 66 | /** @noinspection PhpUndefinedFieldInspection */ |
||
| 67 | $this->version = $ver; |
||
| 68 | }; |
||
| 69 | $logger = new ConsoleLogger($output); |
||
| 70 | $destination = $input->getArgument('destination'); |
||
| 71 | $readable = $input->getOption('readable'); |
||
| 72 | $output->writeln('Creating directory tree...'); |
||
| 73 | try { |
||
| 74 | $destination = TgScraper::getTargetDirectory($destination); |
||
| 75 | mkdir($destination . '/custom/json', 0755, true); |
||
| 76 | mkdir($destination . '/custom/yaml', 0755, true); |
||
| 77 | mkdir($destination . '/postman', 0755, true); |
||
| 78 | mkdir($destination . '/openapi/json', 0755, true); |
||
| 79 | mkdir($destination . '/openapi/yaml', 0755, true); |
||
| 80 | mkdir($destination . '/stubs', 0755, true); |
||
| 81 | } catch (Exception $e) { |
||
| 82 | $logger->critical((string)$e); |
||
| 83 | return Command::FAILURE; |
||
| 84 | } |
||
| 85 | $versions = array_keys( |
||
| 86 | (new ReflectionClass(Versions::class)) |
||
| 87 | ->getConstants()['URLS'] |
||
| 88 | ); |
||
| 89 | $versions = array_diff($versions, ['latest']); |
||
| 90 | foreach ($versions as $version) { |
||
| 91 | $output->writeln(sprintf('Generating v%s schemas...', $version)); |
||
| 92 | $filename = 'v' . str_replace('.', '', $version); |
||
| 93 | try { |
||
| 94 | $logger->info($version . ': Fetching data...'); |
||
| 95 | $generator = TgScraper::fromVersion($logger, $version); |
||
| 96 | } catch (Throwable $e) { |
||
| 97 | $logger->critical((string)$e); |
||
| 98 | return Command::FAILURE; |
||
| 99 | } |
||
| 100 | $versionReplacer->call($generator, $version); |
||
| 101 | $custom = $generator->toArray(); |
||
| 102 | $postman = $generator->toPostman(); |
||
| 103 | $openapi = $generator->toOpenApi(); |
||
| 104 | try { |
||
| 105 | $logger->info($version . ': Creating stubs...'); |
||
| 106 | $generator->toStubs("$destination/tmp", $input->getOption('namespace-prefix')); |
||
| 107 | } catch (Exception) { |
||
| 108 | $logger->critical($version . ': Could not create stubs.'); |
||
| 109 | return Command::FAILURE; |
||
| 110 | } |
||
| 111 | $logger->info($version . ': Compressing stubs...'); |
||
| 112 | $zip = new PharData("$destination/stubs/$filename.zip"); |
||
| 113 | $zip->buildFromDirectory("$destination/tmp"); |
||
| 114 | self::rrmdir("$destination/tmp"); |
||
| 115 | $logger->info($version . ': Saving schemas...'); |
||
| 116 | if ($this->saveFile( |
||
| 117 | $logger, |
||
| 118 | $output, |
||
| 119 | "$destination/custom/json/$filename.json", |
||
| 120 | Encoder::toJson($custom, readable: $readable), |
||
| 121 | sprintf('v%s custom (JSON): ', $version) |
||
| 122 | ) !== Command::SUCCESS) { |
||
| 123 | return Command::FAILURE; |
||
| 124 | } |
||
| 125 | if ($this->saveFile( |
||
| 126 | $logger, |
||
| 127 | $output, |
||
| 128 | "$destination/custom/yaml/$filename.yaml", |
||
| 129 | Encoder::toYaml($custom), |
||
| 130 | sprintf('v%s custom (YAML): ', $version) |
||
| 131 | ) !== Command::SUCCESS) { |
||
| 132 | return Command::FAILURE; |
||
| 133 | } |
||
| 134 | if ($this->saveFile( |
||
| 135 | $logger, |
||
| 136 | $output, |
||
| 137 | "$destination/postman/$filename.json", |
||
| 138 | Encoder::toJson($postman, readable: $readable), |
||
| 139 | sprintf('v%s Postman: ', $version) |
||
| 140 | ) !== Command::SUCCESS) { |
||
| 141 | return Command::FAILURE; |
||
| 142 | } |
||
| 143 | if ($this->saveFile( |
||
| 144 | $logger, |
||
| 145 | $output, |
||
| 146 | "$destination/openapi/json/$filename.json", |
||
| 147 | Encoder::toJson($openapi, readable: $readable), |
||
| 148 | sprintf('v%s OpenAPI (JSON): ', $version) |
||
| 149 | ) !== Command::SUCCESS) { |
||
| 150 | return Command::FAILURE; |
||
| 151 | } |
||
| 152 | if ($this->saveFile( |
||
| 153 | $logger, |
||
| 154 | $output, |
||
| 155 | "$destination/openapi/yaml/$filename.yaml", |
||
| 156 | Encoder::toYaml($openapi), |
||
| 157 | sprintf('v%s OpenAPI (YAML): ', $version) |
||
| 158 | ) !== Command::SUCCESS) { |
||
| 159 | return Command::FAILURE; |
||
| 160 | } |
||
| 161 | $logger->info($version . ': Done!'); |
||
| 162 | } |
||
| 163 | $output->writeln('Done!'); |
||
| 164 | return Command::SUCCESS; |
||
| 165 | } |
||
| 167 | } |