Total Complexity | 46 |
Total Lines | 235 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like ConvertToAbsolutePath often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConvertToAbsolutePath, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ConvertToAbsolutePath implements ConverterInterface |
||
33 | { |
||
34 | |||
35 | /** @var string */ |
||
36 | private $pagePath; |
||
37 | /** @var string|null */ |
||
38 | private $baseTag = null; |
||
39 | /** @var string|null */ |
||
40 | private $starterPath=null; |
||
41 | /** |
||
42 | * @var array|false|int|string|null |
||
43 | */ |
||
44 | private $baseTagParsing; |
||
45 | /** |
||
46 | * @var array|false|int|string|null |
||
47 | */ |
||
48 | private $pagePathParsing; |
||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $domain; |
||
53 | private $scheme; |
||
54 | |||
55 | /** |
||
56 | * ConvertToAbsolutePath constructor. |
||
57 | * @param $pagePath |
||
58 | */ |
||
59 | public function __construct($pagePath=NULL) |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @inheritDoc |
||
68 | */ |
||
69 | public function convert($path) |
||
106 | } |
||
107 | |||
108 | public function onlySitePath($url) { |
||
109 | // $url = preg_replace('/(^https?:\/\/.+?\/)(.*)$/i', '$1', $url); |
||
110 | // return rtrim($url, '/'); |
||
111 | $parseUrl = parse_url($url); |
||
112 | if ($this->isCorrectUrl($parseUrl)) { |
||
113 | return ''; |
||
114 | }elseif(isset($parseUrl['scheme'])){ |
||
115 | return $parseUrl['scheme'] . '://' . $parseUrl['host']; |
||
116 | } else { |
||
117 | return $parseUrl['host']; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // Get the path with last directory |
||
122 | // http://example.com/some/fake/path/page.html => http://example.com/some/fake/path/ |
||
123 | public function upToLastDir($url) { |
||
124 | $parseUrl = parse_url($url); |
||
125 | $path = ''; |
||
126 | if(isset($parseUrl['path'])) |
||
127 | $path = preg_replace('/\/([^\/]+)$/i', '', $parseUrl['path']); |
||
128 | return rtrim($parseUrl['scheme'] . '://' . $parseUrl['host'] . $path, '/') . '/'; |
||
129 | } |
||
130 | |||
131 | |||
132 | public function getPagePath() |
||
133 | { |
||
134 | return $this->pagePath; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string $pagePath |
||
139 | */ |
||
140 | public function setPagePath(string $pagePath): void |
||
141 | { |
||
142 | $this->pagePath = $pagePath; |
||
143 | } |
||
144 | |||
145 | |||
146 | public function getBaseTag() |
||
147 | { |
||
148 | return $this->baseTag; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $baseTag |
||
153 | */ |
||
154 | public function setBaseTag(string $baseTag): void |
||
155 | { |
||
156 | $this->baseTag = $baseTag; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getStarterPath(): string |
||
163 | { |
||
164 | if($this->starterPath===null){ |
||
165 | if($this->getBaseTag() === null) { |
||
166 | $this->starterPath =$this->getPagePath(); |
||
167 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
|
|||
168 | $this->starterPath = $this->getBaseTag() ; |
||
169 | }else{ |
||
170 | $this->starterPath = $this->getPagePathParsing()['scheme'] . '://' . $this->getPagePathParsing()['host'] . $this->getBaseTag(); |
||
171 | } |
||
172 | } |
||
173 | return $this->starterPath; |
||
174 | } |
||
175 | |||
176 | public function getDomain() |
||
177 | { |
||
178 | if ($this->domain === null) { |
||
179 | if($this->getBaseTag() === null) { |
||
180 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
181 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
182 | $this->domain = $this->getBaseTagParsing()['host'] . '/'; |
||
183 | }else{ |
||
184 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
185 | } |
||
186 | } |
||
187 | return $this->domain; |
||
188 | } |
||
189 | |||
190 | public function getScheme() |
||
191 | { |
||
192 | if ($this->scheme === null) { |
||
193 | if($this->getBaseTag() === null) { |
||
194 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
195 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
196 | $this->scheme = $this->getBaseTagParsing()['scheme']; |
||
197 | }else{ |
||
198 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
199 | } |
||
200 | } |
||
201 | return $this->scheme; |
||
202 | } |
||
203 | |||
204 | public function getBaseTagParsing() |
||
205 | { |
||
206 | if($this->baseTagParsing == null) |
||
207 | $this->baseTagParsing = parse_url($this->getBaseTag()); |
||
208 | return $this->baseTagParsing; |
||
209 | } |
||
210 | |||
211 | public function getPagePathParsing() |
||
212 | { |
||
213 | if($this->pagePathParsing == null) |
||
214 | $this->pagePathParsing = parse_url($this->getPagePath()); |
||
215 | return $this->pagePathParsing; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param $path |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isHaveQueryOrFragment($path): bool |
||
223 | { |
||
224 | return substr($path, 0, 1) == '#' || substr($path, 0, 1) == '?'; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param $parseUrl |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isCorrectUrl($parseUrl): bool |
||
232 | { |
||
233 | return !isset($parseUrl['scheme']) AND !isset($parseUrl['host']); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $path |
||
238 | * @return string |
||
239 | */ |
||
240 | public function checkPathIsAbsoluteOrForAnotherApp($path): string |
||
241 | { |
||
242 | if (preg_match('/services:\/\//i', $path)) |
||
243 | return ''; |
||
244 | if (preg_match('/whatsapp:\/\//i', $path)) |
||
245 | return ''; |
||
246 | if (preg_match('/tel:/i', $path)) |
||
247 | return ''; |
||
248 | return $path; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param $path |
||
253 | * @return false|int |
||
254 | */ |
||
255 | public function isPathAbsoluteOrForAnotherApp($path) |
||
256 | { |
||
257 | return preg_match('/[a-z0-9-]{1,}(:\/\/)/i', $path); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param $path |
||
262 | * @return false|int |
||
263 | */ |
||
264 | public function isPathJavaScript($path) |
||
267 | } |
||
268 | } |