Conditions | 6 |
Paths | 8 |
Total Lines | 61 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
36 | private function generateJson(InputInterface $input): void |
||
37 | { |
||
38 | $template_file = (new Finder()) |
||
39 | ->in(getRootPath() . PathHelper::ROOT) |
||
40 | ->files() |
||
41 | ->name('openapi.yaml'); |
||
42 | |||
43 | if (!$template_file->hasResults()) { |
||
44 | echo 'no openapi.yaml file found' . PHP_EOL; |
||
45 | |||
46 | return; |
||
47 | } |
||
48 | |||
49 | $template = $this->getFirstElementOfFileIterator($template_file); |
||
|
|||
50 | |||
51 | $template = Yaml::parse($template->getContents()); |
||
52 | $components = $template['components'] ?? []; |
||
53 | |||
54 | $node_compactor = static fn (Traversable $node_iterator) => array_reduce( |
||
55 | iterator_to_array($node_iterator), |
||
56 | static fn ($carry, $item) => array_merge($carry, $item), |
||
57 | [], |
||
58 | ); |
||
59 | |||
60 | $components['schemas'] = (object) $node_compactor($this->getContentGenerator(PathHelper::SCHEMAS)); |
||
61 | $components['responses'] = (object) $node_compactor($this->getContentGenerator(PathHelper::RESPONSES)); |
||
62 | $components['parameters'] = (object) $node_compactor($this->getContentGenerator(PathHelper::PARAMETERS)); |
||
63 | $components['examples'] = (object) $node_compactor($this->getContentGenerator(PathHelper::EXAMPLES)); |
||
64 | $components['requestBodies'] = (object) $node_compactor($this->getContentGenerator(PathHelper::REQUEST_BODIES)); |
||
65 | $components['headers'] = (object) $node_compactor($this->getContentGenerator(PathHelper::HEADERS)); |
||
66 | $components['securitySchemes'] = (object) $node_compactor($this->getContentGenerator(PathHelper::SECURITY_SCHEMES)); |
||
67 | $components['links'] = (object) $node_compactor($this->getContentGenerator(PathHelper::LINKS)); |
||
68 | $components['callbacks'] = (object) $node_compactor($this->getContentGenerator(PathHelper::CALLBACKS)); |
||
69 | |||
70 | $template['paths'] = (object) $node_compactor($this->getContentGenerator(PathHelper::PATHS)); |
||
71 | $template['components'] = (object) $components; |
||
72 | |||
73 | $arg_path = $input->getArgument('path'); |
||
74 | |||
75 | if (!is_string($arg_path)) { |
||
76 | throw new \RuntimeException('Path argument must be a string'); |
||
77 | } |
||
78 | |||
79 | $path = '/' !== substr($arg_path, 0, 1) ? '/' . $arg_path : $arg_path; |
||
80 | |||
81 | $openapi_file_path = getRootPath() . $path . '/openapi.json'; |
||
82 | if (!$file = fopen($openapi_file_path, 'w')) { |
||
83 | echo 'error generating openapi.json file' . PHP_EOL; |
||
84 | |||
85 | return; |
||
86 | } |
||
87 | |||
88 | if ($input->getOption('pretty-json')) { |
||
89 | fwrite($file, \json_encode($template, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)); |
||
90 | fclose($file); |
||
91 | |||
92 | return; |
||
93 | } |
||
94 | |||
95 | fwrite($file, \json_encode($template, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)); |
||
96 | fclose($file); |
||
97 | } |
||
121 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.