| Conditions | 20 |
| Paths | 104 |
| Total Lines | 81 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 2 | 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 | $this->baseurl = (string) $this->config->get('baseurl'); |
||
| 59 | $this->version = (string) $this->builder->time; |
||
| 60 | |||
| 61 | // handles options |
||
| 62 | $canonical = null; |
||
| 63 | $addhash = false; |
||
| 64 | $format = null; |
||
| 65 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
| 66 | |||
| 67 | // canonical URL? |
||
| 68 | if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
||
| 69 | $base = rtrim($this->baseurl, '/'); |
||
| 70 | } |
||
| 71 | if ($canonical === false) { |
||
| 72 | $base = ''; |
||
| 73 | } |
||
| 74 | |||
| 75 | // value is empty (ie: `url()`) |
||
| 76 | if (is_null($value) || empty($value) || $value == '/') { |
||
| 77 | $this->url = $base.'/'; |
||
| 78 | |||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | // potential page id |
||
| 83 | $pageId = self::$slugifier->slugify((string) $value); |
||
| 84 | |||
| 85 | switch (true) { |
||
| 86 | // Page |
||
| 87 | case $value instanceof Page: |
||
| 88 | if (!$format) { |
||
| 89 | $format = $value->getVariable('output'); |
||
| 90 | if (is_array($value->getVariable('output'))) { |
||
| 91 | $format = $value->getVariable('output')[0]; |
||
| 92 | } |
||
| 93 | if (!$format) { |
||
| 94 | $format = 'html'; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $this->url = $base.'/'.ltrim($value->getUrl($format, $this->config), '/'); |
||
| 98 | break; |
||
| 99 | // Asset |
||
| 100 | case $value instanceof Asset: |
||
| 101 | $asset = $value; |
||
| 102 | $url = $asset['path']; |
||
| 103 | if ($addhash) { |
||
| 104 | $url .= '?'.$this->version; |
||
| 105 | } |
||
| 106 | $url = $base.'/'.ltrim($url, '/'); |
||
| 107 | $asset['path'] = $url; |
||
| 108 | |||
| 109 | $this->url = (string) $asset; |
||
| 110 | break; |
||
| 111 | // External URL |
||
| 112 | case Util::isExternalUrl($value): |
||
| 113 | $this->url = $value; |
||
| 114 | break; |
||
| 115 | // asset as string |
||
| 116 | case false !== strpos($value, '.') ? true : false: |
||
| 117 | $url = $value; |
||
| 118 | if ($addhash) { |
||
| 119 | $url .= '?'.$this->version; |
||
| 120 | } |
||
| 121 | $this->url = $base.'/'.ltrim($url, '/'); |
||
| 122 | break; |
||
| 123 | // Page ID as string |
||
| 124 | case $this->builder->getPages()->has($pageId): |
||
| 125 | $page = $this->builder->getPages()->get($pageId); |
||
| 126 | $this->url = new self($this->builder, $page, $options); |
||
| 127 | break; |
||
| 128 | // others cases? |
||
| 129 | default: |
||
| 130 | // others cases |
||
| 131 | $this->url = $base.'/'.$value; |
||
| 132 | } |
||
| 155 |