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