| Total Complexity | 43 |
| Total Lines | 208 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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) |
||
| 112 | } |
||
| 113 | |||
| 114 | public function onlySitePath($url) { |
||
| 115 | // $url = preg_replace('/(^https?:\/\/.+?\/)(.*)$/i', '$1', $url); |
||
| 116 | // return rtrim($url, '/'); |
||
| 117 | $parseUrl = parse_url($url); |
||
| 118 | if ($this->isCorrectUrl($parseUrl)) { |
||
| 119 | return ''; |
||
| 120 | }elseif(isset($parseUrl['scheme'])){ |
||
| 121 | return $parseUrl['scheme'] . '://' . $parseUrl['host']; |
||
| 122 | } else { |
||
| 123 | return $parseUrl['host']; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | // Get the path with last directory |
||
| 128 | // http://example.com/some/fake/path/page.html => http://example.com/some/fake/path/ |
||
| 129 | public function upToLastDir($url) { |
||
| 130 | $parseUrl = parse_url($url); |
||
| 131 | $path = ''; |
||
| 132 | if(isset($parseUrl['path'])) |
||
| 133 | $path = preg_replace('/\/([^\/]+)$/i', '', $parseUrl['path']); |
||
| 134 | return rtrim($parseUrl['scheme'] . '://' . $parseUrl['host'] . $path, '/') . '/'; |
||
| 135 | } |
||
| 136 | |||
| 137 | |||
| 138 | public function getPagePath() |
||
| 139 | { |
||
| 140 | return $this->pagePath; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $pagePath |
||
| 145 | */ |
||
| 146 | public function setPagePath(string $pagePath): void |
||
| 147 | { |
||
| 148 | $this->pagePath = $pagePath; |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | public function getBaseTag() |
||
| 153 | { |
||
| 154 | return $this->baseTag; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $baseTag |
||
| 159 | */ |
||
| 160 | public function setBaseTag(string $baseTag): void |
||
| 161 | { |
||
| 162 | $this->baseTag = $baseTag; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getStarterPath(): string |
||
| 169 | { |
||
| 170 | if($this->starterPath===null){ |
||
| 171 | if($this->getBaseTag() === null) { |
||
| 172 | $this->starterPath =$this->getPagePath(); |
||
| 173 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
|
|
|||
| 174 | $this->starterPath = $this->getBaseTag() ; |
||
| 175 | }else{ |
||
| 176 | $this->starterPath = $this->getPagePathParsing()['scheme'] . '://' . $this->getPagePathParsing()['host'] . $this->getBaseTag(); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | return $this->starterPath; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getDomain() |
||
| 183 | { |
||
| 184 | if ($this->domain === null) { |
||
| 185 | if($this->getBaseTag() === null) { |
||
| 186 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
| 187 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
| 188 | $this->domain = $this->getBaseTagParsing()['host'] . '/'; |
||
| 189 | }else{ |
||
| 190 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | return $this->domain; |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getScheme() |
||
| 197 | { |
||
| 198 | if ($this->scheme === null) { |
||
| 199 | if($this->getBaseTag() === null) { |
||
| 200 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
| 201 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
| 202 | $this->scheme = $this->getBaseTagParsing()['scheme']; |
||
| 203 | }else{ |
||
| 204 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | return $this->scheme; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function getBaseTagParsing() |
||
| 215 | } |
||
| 216 | |||
| 217 | public function getPagePathParsing() |
||
| 218 | { |
||
| 219 | if($this->pagePathParsing == null) |
||
| 220 | $this->pagePathParsing = parse_url($this->getPagePath()); |
||
| 221 | return $this->pagePathParsing; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param $path |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | public function isHaveQueryOrFragment($path): bool |
||
| 229 | { |
||
| 230 | return substr($path, 0, 1) == '#' || substr($path, 0, 1) == '?'; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param $parseUrl |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function isCorrectUrl($parseUrl): bool |
||
| 240 | } |
||
| 241 | } |