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