| Conditions | 15 |
| Paths | 96 |
| Total Lines | 113 |
| 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 |
||
| 72 | private function writeMarkdown($parsedRoutes) |
||
| 73 | { |
||
| 74 | $outputPath = config('apidoc.output'); |
||
| 75 | $targetFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'index.md'; |
||
| 76 | $compareFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'.compare.md'; |
||
| 77 | $prependFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'prepend.md'; |
||
| 78 | $appendFile = $outputPath.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'append.md'; |
||
| 79 | |||
| 80 | $infoText = view('apidoc::partials.info') |
||
| 81 | ->with('outputPath', ltrim($outputPath, 'public/')) |
||
| 82 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection()); |
||
| 83 | |||
| 84 | $parsedRouteOutput = $parsedRoutes->map(function ($routeGroup) { |
||
| 85 | return $routeGroup->map(function ($route) { |
||
| 86 | if (count($route['cleanBodyParameters'])) { |
||
| 87 | $route['headers']['Content-Type'] = 'application/json'; |
||
| 88 | } |
||
| 89 | $route['output'] = (string) view('apidoc::partials.route')->with('route', $route)->render(); |
||
| 90 | |||
| 91 | return $route; |
||
| 92 | }); |
||
| 93 | }); |
||
| 94 | |||
| 95 | $frontmatter = view('apidoc::partials.frontmatter'); |
||
| 96 | /* |
||
| 97 | * In case the target file already exists, we should check if the documentation was modified |
||
| 98 | * and skip the modified parts of the routes. |
||
| 99 | */ |
||
| 100 | if (file_exists($targetFile) && file_exists($compareFile)) { |
||
| 101 | $generatedDocumentation = file_get_contents($targetFile); |
||
| 102 | $compareDocumentation = file_get_contents($compareFile); |
||
| 103 | |||
| 104 | if (preg_match('/---(.*)---\\s<!-- START_INFO -->/is', $generatedDocumentation, $generatedFrontmatter)) { |
||
| 105 | $frontmatter = trim($generatedFrontmatter[1], "\n"); |
||
| 106 | } |
||
| 107 | |||
| 108 | $parsedRouteOutput->transform(function ($routeGroup) use ($generatedDocumentation, $compareDocumentation) { |
||
| 109 | return $routeGroup->transform(function ($route) use ($generatedDocumentation, $compareDocumentation) { |
||
| 110 | if (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $generatedDocumentation, $existingRouteDoc)) { |
||
| 111 | $routeDocumentationChanged = (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $compareDocumentation, $lastDocWeGeneratedForThisRoute) && $lastDocWeGeneratedForThisRoute[1] !== $existingRouteDoc[1]); |
||
| 112 | if ($routeDocumentationChanged === false || $this->option('force')) { |
||
| 113 | if ($routeDocumentationChanged) { |
||
| 114 | $this->warn('Discarded manual changes for route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | $this->warn('Skipping modified route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 118 | $route['modified_output'] = $existingRouteDoc[0]; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $route; |
||
| 123 | }); |
||
| 124 | }); |
||
| 125 | } |
||
| 126 | |||
| 127 | $prependFileContents = file_exists($prependFile) |
||
| 128 | ? file_get_contents($prependFile)."\n" : ''; |
||
| 129 | $appendFileContents = file_exists($appendFile) |
||
| 130 | ? "\n".file_get_contents($appendFile) : ''; |
||
| 131 | |||
| 132 | $documentarian = new Documentarian(); |
||
| 133 | |||
| 134 | $markdown = view('apidoc::documentarian') |
||
| 135 | ->with('writeCompareFile', false) |
||
| 136 | ->with('frontmatter', $frontmatter) |
||
| 137 | ->with('infoText', $infoText) |
||
| 138 | ->with('prependMd', $prependFileContents) |
||
| 139 | ->with('appendMd', $appendFileContents) |
||
| 140 | ->with('outputPath', config('apidoc.output')) |
||
| 141 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection()) |
||
| 142 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 143 | |||
| 144 | if (! is_dir($outputPath)) { |
||
| 145 | $documentarian->create($outputPath); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Write output file |
||
| 149 | file_put_contents($targetFile, $markdown); |
||
| 150 | |||
| 151 | // Write comparable markdown file |
||
| 152 | $compareMarkdown = view('apidoc::documentarian') |
||
| 153 | ->with('writeCompareFile', true) |
||
| 154 | ->with('frontmatter', $frontmatter) |
||
| 155 | ->with('infoText', $infoText) |
||
| 156 | ->with('prependMd', $prependFileContents) |
||
| 157 | ->with('appendMd', $appendFileContents) |
||
| 158 | ->with('outputPath', config('apidoc.output')) |
||
| 159 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection()) |
||
| 160 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 161 | |||
| 162 | file_put_contents($compareFile, $compareMarkdown); |
||
| 163 | |||
| 164 | $this->info('Wrote index.md to: '.$outputPath); |
||
| 165 | |||
| 166 | $this->info('Generating API HTML code'); |
||
| 167 | |||
| 168 | $documentarian->generate($outputPath); |
||
| 169 | |||
| 170 | $this->info('Wrote HTML documentation to: '.$outputPath.'/index.html'); |
||
| 171 | |||
| 172 | if ($this->shouldGeneratePostmanCollection()) { |
||
| 173 | $this->info('Generating Postman collection'); |
||
| 174 | |||
| 175 | file_put_contents($outputPath.DIRECTORY_SEPARATOR.'collection.json', $this->generatePostmanCollection($parsedRoutes)); |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($logo = config('apidoc.logo')) { |
||
| 179 | copy( |
||
| 180 | $logo, |
||
| 181 | $outputPath.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'logo.png' |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 274 |