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