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