| Conditions | 31 | 
| Paths | 576 | 
| Total Lines | 94 | 
| Code Lines | 60 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 8 | ||
| Bugs | 7 | Features | 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 | ||
| 51 | public function __construct(Builder $builder, $value, ?array $options = null) | ||
| 52 |     { | ||
| 53 | $this->builder = $builder; | ||
| 54 | $this->config = $builder->getConfig(); | ||
| 55 |         if (!self::$slugifier instanceof Slugify) { | ||
| 56 | self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); | ||
| 57 | } | ||
| 58 | |||
| 59 | // handles options | ||
| 60 | $canonical = null; // if true prefix url with baseurl config | ||
| 61 | $format = null; // output format | ||
| 62 | $language = null; // force language | ||
| 63 | extract(\is_array($options) ? $options : [], EXTR_IF_EXISTS); | ||
| 64 | |||
| 65 | // base URL | ||
| 66 | $base = ''; | ||
| 67 | // enable canonical URL | ||
| 68 |         if ($this->config->isEnabled('canonicalurl') || $canonical === true) { | ||
| 69 |             $base = rtrim((string) $this->config->get('baseurl'), '/'); | ||
| 70 | } | ||
| 71 | // disable canonical URL by option | ||
| 72 |         if ($canonical === false) { | ||
|  | |||
| 73 | $base = ''; | ||
| 74 | } | ||
| 75 | // use URL path as base if exists | ||
| 76 |         if ($base == '' && '/' != $basepath = parse_url((string) $this->config->get('baseurl'), PHP_URL_PATH)) { | ||
| 77 |             if (\is_string($basepath)) { | ||
| 78 | $base = '/' . trim($basepath, '/'); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | // if value is empty returns base URL | ||
| 83 |         if (\is_null($value) || empty($value) || $value == '/') { | ||
| 84 | $this->url = $base; | ||
| 85 | |||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 |         switch (true) { | ||
| 90 | case $value instanceof Page: // $value is a Page | ||
| 91 | /** @var Page $value */ | ||
| 92 |                 if (!$format) { | ||
| 93 |                     $format = $value->getVariable('output'); | ||
| 94 |                     if (\is_array($value->getVariable('output'))) { | ||
| 95 |                         $default = array_search('html', $value->getVariable('output')) ?: 0; | ||
| 96 |                         $format = $value->getVariable('output')[$default]; | ||
| 97 | } | ||
| 98 |                     if (!$format) { | ||
| 99 | $format = 'html'; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | $this->url = $base . '/' . ltrim((new PageRenderer($this->builder, $value))->getPath($format), '/'); | ||
| 103 |                 if ($canonical && $value->hasVariable('canonical') && $value->getVariable('canonical')['url']) { // canonical URL | ||
| 104 |                     $this->url = $value->getVariable('canonical')['url']; | ||
| 105 | } | ||
| 106 | break; | ||
| 107 | case $value instanceof MenuEntry: // $value is a Menu Entry | ||
| 108 | /** @var MenuEntry $value */ | ||
| 109 |                 if (Util\File::isRemote($value['url'])) { | ||
| 110 | $this->url = $value['url']; | ||
| 111 | break; | ||
| 112 | } | ||
| 113 | $this->url = $base . '/' . ltrim($value['url'], '/'); | ||
| 114 | break; | ||
| 115 | case $value instanceof Asset: // $value is an Asset | ||
| 116 | /** @var Asset $value */ | ||
| 117 | $this->url = $base . '/' . ltrim($value['path'], '/'); | ||
| 118 |                 if ($value->isImageInCdn()) { | ||
| 119 | $this->url = (string) $value; | ||
| 120 | } | ||
| 121 | break; | ||
| 122 | case \is_string($value): // others cases | ||
| 123 | /** @var string $value */ | ||
| 124 | // $value is a potential Page ID | ||
| 125 | $pageId = self::$slugifier->slugify($value); | ||
| 126 | // should force language? | ||
| 127 | $lang = ''; | ||
| 128 |                 if ($language !== null && $language != $this->config->getLanguageDefault()) { | ||
| 129 | $pageId = "$language/$pageId"; | ||
| 130 | $lang = "$language/"; | ||
| 131 | } | ||
| 132 |                 switch (true) { | ||
| 133 | case Util\File::isRemote($value): // $value is an external URL | ||
| 134 | $this->url = $value; | ||
| 135 | break; | ||
| 136 | case $this->builder->getPages()->has($pageId): // $pageId exists in pages collection | ||
| 137 | $this->url = (string) new self($this->builder, $this->builder->getPages()->get($pageId), $options); | ||
| 138 | break; | ||
| 139 | default: | ||
| 140 | // remove double language prefix | ||
| 141 |                         if ($lang && Util\Str::startsWith($value, $lang)) { | ||
| 142 | $value = substr($value, \strlen($lang)); | ||
| 143 | } | ||
| 144 | $this->url = $base . '/' . $lang . ltrim($value, '/'); | ||
| 145 | } | ||
| 165 |