| Conditions | 9 |
| Paths | 22 |
| Total Lines | 60 |
| Code Lines | 37 |
| 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 |
||
| 103 | public function show(string $productKey, string $version, string $page = null) |
||
| 104 | { |
||
| 105 | // ensure product exists |
||
| 106 | $product = $this->productFinder->findProduct($productKey); |
||
| 107 | if ($product === null) { |
||
| 108 | abort(404); |
||
| 109 | } |
||
| 110 | |||
| 111 | // get default version for product |
||
| 112 | $defaultVersion = $product->getDefaultVersion(); |
||
| 113 | if (!$product->hasVersion($version)) { |
||
| 114 | return redirect(route( |
||
| 115 | $this->configProvider->getProductPageRouteName(), |
||
| 116 | [$product->getKey(), $defaultVersion] |
||
| 117 | ), 301); |
||
| 118 | } |
||
| 119 | |||
| 120 | // get page content |
||
| 121 | $page = $page ?: self::DEFAULT_PAGE; |
||
| 122 | $content = $this->documentationProvider->getPage($product, $version, $page); |
||
|
|
|||
| 123 | |||
| 124 | // ensure page has content |
||
| 125 | if (empty($content)) { |
||
| 126 | $this->logger->warning( |
||
| 127 | sprintf('Documentation page (%s) for %s has no content.', $page, $product->getName()), |
||
| 128 | ['product' => $product] |
||
| 129 | ); |
||
| 130 | abort(404); |
||
| 131 | } |
||
| 132 | |||
| 133 | $title = (new Crawler($content))->filterXPath('//h1'); |
||
| 134 | $section = ''; |
||
| 135 | |||
| 136 | // ensure section exists |
||
| 137 | if ($this->documentationProvider->sectionExists($product, $version, $page)) { |
||
| 138 | $section .= "/${page}"; |
||
| 139 | } elseif (!empty($page)) { |
||
| 140 | // section does not exist, go to version index |
||
| 141 | return redirect()->route($this->configProvider->getProductPageRouteName(), [$product->getKey(), $version]); |
||
| 142 | } |
||
| 143 | |||
| 144 | // set canonical |
||
| 145 | $canonical = null; |
||
| 146 | if ($this->documentationProvider->sectionExists($product, $defaultVersion, $page)) { |
||
| 147 | $canonical = route( |
||
| 148 | $this->configProvider->getProductPageRouteName(), |
||
| 149 | [$product->getKey(), $defaultVersion, $page] |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | return view('docweaver::page', [ |
||
| 154 | 'canonical' => $canonical, |
||
| 155 | 'content' => $content, |
||
| 156 | 'currentProduct' => $product, |
||
| 157 | 'currentSection' => $section, |
||
| 158 | 'currentVersion' => $version, |
||
| 159 | 'index' => $this->documentationProvider->getPage($product, $version), |
||
| 160 | 'page' => $page, |
||
| 161 | 'title' => count($title) ? $title->text() : null, |
||
| 162 | 'versions' => $product->getVersions(), |
||
| 163 | ]); |
||
| 166 |