| Conditions | 9 |
| Paths | 4 |
| Total Lines | 82 |
| 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 |
||
| 84 | public function writeMarkdownAndSourceFiles(Collection $parsedRoutes, string $sourceOutputPath) |
||
| 85 | { |
||
| 86 | $targetFile = $sourceOutputPath.'/source/index.md'; |
||
| 87 | $compareFile = $sourceOutputPath.'/source/.compare.md'; |
||
| 88 | |||
| 89 | $infoText = view('apidoc::partials.info') |
||
| 90 | ->with('outputPath', 'docs') |
||
| 91 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection); |
||
| 92 | |||
| 93 | $settings = ['languages' => $this->config->get('example_languages')]; |
||
| 94 | // Generate Markdown for each route |
||
| 95 | $parsedRouteOutput = $this->generateMarkdownOutputForEachRoute($parsedRoutes, $settings); |
||
| 96 | |||
| 97 | $frontmatter = view('apidoc::partials.frontmatter') |
||
| 98 | ->with('settings', $settings); |
||
| 99 | |||
| 100 | /* |
||
| 101 | * If the target file already exists, |
||
| 102 | * we check if the documentation was modified |
||
| 103 | * and skip the modified parts of the routes. |
||
| 104 | */ |
||
| 105 | if (file_exists($targetFile) && file_exists($compareFile)) { |
||
| 106 | $generatedDocumentation = file_get_contents($targetFile); |
||
| 107 | $compareDocumentation = file_get_contents($compareFile); |
||
| 108 | |||
| 109 | $parsedRouteOutput->transform(function (Collection $routeGroup) use ($generatedDocumentation, $compareDocumentation) { |
||
| 110 | return $routeGroup->transform(function (array $route) use ($generatedDocumentation, $compareDocumentation) { |
||
| 111 | if (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $generatedDocumentation, $existingRouteDoc)) { |
||
| 112 | $routeDocumentationChanged = (preg_match('/<!-- START_'.$route['id'].' -->(.*)<!-- END_'.$route['id'].' -->/is', $compareDocumentation, $lastDocWeGeneratedForThisRoute) && $lastDocWeGeneratedForThisRoute[1] !== $existingRouteDoc[1]); |
||
| 113 | if ($routeDocumentationChanged === false || $this->forceIt) { |
||
| 114 | if ($routeDocumentationChanged) { |
||
| 115 | $this->output->warn('Discarded manual changes for route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | $this->output->warn('Skipping modified route ['.implode(',', $route['methods']).'] '.$route['uri']); |
||
| 119 | $route['modified_output'] = $existingRouteDoc[0]; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return $route; |
||
| 124 | }); |
||
| 125 | }); |
||
| 126 | } |
||
| 127 | |||
| 128 | $prependFileContents = $this->getMarkdownToPrepend($sourceOutputPath); |
||
| 129 | $appendFileContents = $this->getMarkdownToAppend($sourceOutputPath); |
||
| 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', $this->config->get('output')) |
||
| 138 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection) |
||
| 139 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 140 | |||
| 141 | $this->output->info('Writing index.md and source files to: '.$sourceOutputPath); |
||
| 142 | |||
| 143 | if (! is_dir($sourceOutputPath)) { |
||
| 144 | $documentarian = new Documentarian(); |
||
| 145 | $documentarian->create($sourceOutputPath); |
||
| 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', $this->config->get('output')) |
||
| 159 | ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection) |
||
| 160 | ->with('parsedRoutes', $parsedRouteOutput); |
||
| 161 | |||
| 162 | file_put_contents($compareFile, $compareMarkdown); |
||
| 163 | |||
| 164 | $this->output->info('Wrote index.md and source files to: '.$sourceOutputPath); |
||
| 165 | } |
||
| 166 | |||
| 310 |