Conditions | 17 |
Paths | 60 |
Total Lines | 89 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
54 | public function createUrl($value = null, $options = null) |
||
55 | { |
||
56 | $baseurl = (string) $this->config->get('baseurl'); |
||
57 | $hash = md5((string) $this->config->get('time')); |
||
58 | $base = ''; |
||
59 | |||
60 | // handles options |
||
61 | $canonical = null; |
||
62 | $addhash = false; |
||
63 | $format = null; |
||
64 | extract(is_array($options) ? $options : []); |
||
65 | |||
66 | // set baseurl |
||
67 | if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
||
68 | $base = rtrim($baseurl, '/'); |
||
69 | } |
||
70 | if ($canonical === false) { |
||
71 | $base = ''; |
||
72 | } |
||
73 | |||
74 | // value is empty: url() |
||
75 | if (empty($value) || $value == '/') { |
||
76 | return $base.'/'; |
||
77 | } |
||
78 | |||
79 | // value is a Page item |
||
80 | if ($value instanceof Page) { |
||
81 | if (!$format) { |
||
82 | $format = $value->getVariable('output'); |
||
83 | if (is_array($value->getVariable('output'))) { |
||
84 | $format = $value->getVariable('output')[0]; |
||
85 | } |
||
86 | if (!$format) { |
||
87 | $format = 'html'; |
||
88 | } |
||
89 | } |
||
90 | $url = $value->getUrl($format, $this->config); |
||
91 | $url = $base.'/'.ltrim($url, '/'); |
||
92 | |||
93 | return $url; |
||
94 | } |
||
95 | |||
96 | // value is an Asset object |
||
97 | if ($value instanceof Asset) { |
||
98 | $asset = $value; |
||
99 | $url = $asset['path']; |
||
100 | if ($addhash) { |
||
101 | $url .= '?'.$hash; |
||
102 | } |
||
103 | $url = $base.'/'.ltrim($url, '/'); |
||
104 | $asset['path'] = $url; |
||
105 | |||
106 | return $asset; |
||
107 | } |
||
108 | |||
109 | // value is an external URL |
||
110 | if (Util::isExternalUrl($value)) { |
||
111 | $url = $value; |
||
112 | |||
113 | return $url; |
||
114 | } |
||
115 | |||
116 | // value is a string |
||
117 | $value = Util::joinPath($value); |
||
118 | |||
119 | // value is (certainly) a path to a ressource (ie: 'path/file.pdf') |
||
120 | if (false !== strpos($value, '.')) { |
||
121 | $url = $value; |
||
122 | if ($addhash) { |
||
123 | $url .= '?'.$hash; |
||
124 | } |
||
125 | $url = $base.'/'.ltrim($url, '/'); |
||
126 | |||
127 | return $url; |
||
128 | } |
||
129 | |||
130 | // others cases |
||
131 | $url = $base.'/'.$value; |
||
132 | |||
133 | // value is a page ID (ie: 'path/my-page') |
||
134 | try { |
||
135 | $pageId = self::$slugifier->slugify($value); |
||
136 | $page = $this->builder->getPages()->get($pageId); |
||
137 | $url = $this->createUrl($page, $options); |
||
138 | } catch (\DomainException $e) { |
||
139 | // nothing to do |
||
140 | } |
||
141 | |||
142 | return $url; |
||
143 | } |
||
145 |