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