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