| Conditions | 11 |
| Paths | 12 |
| Total Lines | 92 |
| 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 |
||
| 109 | private function writeMarkdown($parsedRoutes) |
||
| 110 | { |
||
| 111 | $outputPath = $this->option('output'); |
||
| 112 | $targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md'; |
||
| 113 | $compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md'; |
||
| 114 | |||
| 115 | $infoText = view('apidoc::partials.info') |
||
| 116 | ->with('outputPath', ltrim($outputPath, 'public/')) |
||
| 117 | ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')); |
||
| 118 | |||
| 119 | $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) { |
||
| 120 | return $routeGroup->map(function ($route) { |
||
| 121 | $route['output'] = (string) view('apidoc::partials.route')->with('parsedRoute', $route)->render(); |
||
| 122 | |||
| 123 | return $route; |
||
| 124 | }); |
||
| 125 | }); |
||
| 126 | |||
| 127 | $frontmatter = view('apidoc::partials.frontmatter'); |
||
| 128 | /* |
||
| 129 | * In case the target file already exists, we should check if the documentation was modified |
||
| 130 | * and skip the modified parts of the routes. |
||
| 131 | */ |
||
| 132 | if (file_exists($targetFile) && file_exists($compareFile)) { |
||
| 133 | $generatedDocumentation = file_get_contents($targetFile); |
||
| 134 | $compareDocumentation = file_get_contents($compareFile); |
||
| 135 | |||
| 136 | if (preg_match('/---(.*)---\\s<!-- START_INFO -->/is', $generatedDocumentation, $generatedFrontmatter)) { |
||
| 137 | $frontmatter = trim($generatedFrontmatter[1], "\n"); |
||
| 138 | } |
||
| 139 | |||
| 140 | $parsedRouteOutput->transform(function ($routeGroup) use ($generatedDocumentation, $compareDocumentation) { |
||
| 141 | return $routeGroup->transform(function ($route) use ($generatedDocumentation, $compareDocumentation) { |
||
| 142 | if (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $generatedDocumentation, $routeMatch)) { |
||
| 143 | $routeDocumentationChanged = (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $compareDocumentation, $compareMatch) && $compareMatch[1] !== $routeMatch[1]); |
||
| 144 | if ($routeDocumentationChanged === false || $this->option('force')) { |
||
| 145 | if ($routeDocumentationChanged) { |
||
| 146 | $this->warn('Discarded manual changes for route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $this->warn('Skipping modified route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 150 | $route['modified_output'] = $routeMatch[0]; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | return $route; |
||
| 155 | }); |
||
| 156 | }); |
||
| 157 | } |
||
| 158 | |||
| 159 | $documentarian = new Documentarian(); |
||
| 160 | |||
| 161 | $markdown = view('apidoc::documentarian') |
||
| 162 | ->with('writeCompareFile', false) |
||
| 163 | ->with('frontmatter', $frontmatter) |
||
| 164 | ->with('infoText', $infoText) |
||
| 165 | ->with('outputPath', $this->option('output')) |
||
| 166 | ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')) |
||
| 167 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 168 | |||
| 169 | if (! is_dir($outputPath)) { |
||
| 170 | $documentarian->create($outputPath); |
||
| 171 | } |
||
| 172 | |||
| 173 | // Write output file |
||
| 174 | file_put_contents($targetFile, $markdown); |
||
| 175 | |||
| 176 | // Write comparable markdown file |
||
| 177 | $compareMarkdown = view('apidoc::documentarian') |
||
| 178 | ->with('writeCompareFile', true) |
||
| 179 | ->with('frontmatter', $frontmatter) |
||
| 180 | ->with('infoText', $infoText) |
||
| 181 | ->with('outputPath', $this->option('output')) |
||
| 182 | ->with('showPostmanCollectionButton', ! $this->option('noPostmanCollection')) |
||
| 183 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 184 | |||
| 185 | file_put_contents($compareFile, $compareMarkdown); |
||
| 186 | |||
| 187 | $this->info('Wrote index.md to: '.$outputPath); |
||
| 188 | |||
| 189 | $this->info('Generating API HTML code'); |
||
| 190 | |||
| 191 | $documentarian->generate($outputPath); |
||
| 192 | |||
| 193 | $this->info('Wrote HTML documentation to: '.$outputPath.'/index.html'); |
||
| 194 | |||
| 195 | if ($this->option('noPostmanCollection') !== true) { |
||
| 196 | $this->info('Generating Postman collection'); |
||
| 197 | |||
| 198 | file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes)); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 333 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.