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