Conditions | 19 |
Paths | 168 |
Total Lines | 73 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 | extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
||
61 | |||
62 | // canonical URL? |
||
63 | if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
||
64 | $base = rtrim($this->baseurl, '/'); |
||
65 | } |
||
66 | if ($canonical === false) { |
||
67 | $base = ''; |
||
68 | } |
||
69 | |||
70 | // value is empty (ie: `url()`) |
||
71 | if (is_null($value) || empty($value) || $value == '/') { |
||
72 | $this->url = $base.'/'; |
||
73 | |||
74 | return; |
||
75 | } |
||
76 | |||
77 | // potential page id |
||
78 | $pageId = null; |
||
79 | if (is_string($value)) { |
||
80 | $pageId = self::$slugifier->slugify($value); |
||
81 | } |
||
82 | |||
83 | switch (true) { |
||
84 | // Page |
||
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]; |
||
90 | } |
||
91 | if (!$format) { |
||
92 | $format = 'html'; |
||
93 | } |
||
94 | } |
||
95 | $this->url = $base.'/'.ltrim($value->getUrl($format, $this->config), '/'); |
||
96 | break; |
||
97 | // Asset |
||
98 | case $value instanceof Asset: |
||
99 | $asset = $value; |
||
100 | $this->url = $base.'/'.ltrim($asset['path'], '/'); |
||
101 | /** @var Asset $asset */ |
||
102 | $asset->save(); |
||
103 | break; |
||
104 | // External URL |
||
105 | case Util::isExternalUrl($value): |
||
106 | $this->url = $value; |
||
107 | break; |
||
108 | // asset as string |
||
109 | case false !== strpos($value, '.') ? true : false: |
||
110 | $this->url = $base.'/'.ltrim($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 | // others cases? |
||
118 | default: |
||
119 | // others cases |
||
120 | $this->url = $base.'/'.$value; |
||
121 | } |
||
144 |