| Total Complexity | 44 |
| Total Lines | 200 |
| 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) |
||
| 60 | { |
||
| 61 | if(isset($pagePath)) { |
||
| 62 | $this->setPagePath($pagePath); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @inheritDoc |
||
| 68 | */ |
||
| 69 | public function convert($path) |
||
| 122 | } |
||
| 123 | |||
| 124 | public function onlySitePath($url) { |
||
| 125 | // $url = preg_replace('/(^https?:\/\/.+?\/)(.*)$/i', '$1', $url); |
||
| 126 | // return rtrim($url, '/'); |
||
| 127 | $parseUrl = parse_url($url); |
||
| 128 | if (!isset($parseUrl['scheme']) AND !isset($parseUrl['host'])) { |
||
| 129 | return ''; |
||
| 130 | }elseif(isset($parseUrl['scheme'])){ |
||
| 131 | return $parseUrl['scheme'] . '://' . $parseUrl['host']; |
||
| 132 | } else { |
||
| 133 | return $parseUrl['host']; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // Get the path with last directory |
||
| 138 | // http://example.com/some/fake/path/page.html => http://example.com/some/fake/path/ |
||
| 139 | public function upToLastDir($url) { |
||
| 140 | $parseUrl = parse_url($url); |
||
| 141 | $path = ''; |
||
| 142 | if(isset($parseUrl['path'])) |
||
| 143 | $path = preg_replace('/\/([^\/]+)$/i', '', $parseUrl['path']); |
||
| 144 | return rtrim($parseUrl['scheme'] . '://' . $parseUrl['host'] . $path, '/') . '/'; |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | public function getPagePath() |
||
| 149 | { |
||
| 150 | return $this->pagePath; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $pagePath |
||
| 155 | */ |
||
| 156 | public function setPagePath(string $pagePath): void |
||
| 157 | { |
||
| 158 | $this->pagePath = $pagePath; |
||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | public function getBaseTag() |
||
| 163 | { |
||
| 164 | return $this->baseTag; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $baseTag |
||
| 169 | */ |
||
| 170 | public function setBaseTag(string $baseTag): void |
||
| 171 | { |
||
| 172 | $this->baseTag = $baseTag; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | private function getStarterPath(): string |
||
| 179 | { |
||
| 180 | if($this->starterPath===null){ |
||
| 181 | if($this->getBaseTag() === null) { |
||
| 182 | $this->starterPath =$this->getPagePath(); |
||
| 183 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
|
|
|||
| 184 | $this->starterPath = $this->getBaseTag() ; |
||
| 185 | }else{ |
||
| 186 | $this->starterPath = $this->getPagePathParsing()['scheme'] . '://' . $this->getPagePathParsing()['host'] . $this->getBaseTag(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | return $this->starterPath; |
||
| 190 | } |
||
| 191 | |||
| 192 | private function getDomain() |
||
| 193 | { |
||
| 194 | if ($this->domain === null) { |
||
| 195 | if($this->getBaseTag() === null) { |
||
| 196 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
| 197 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
| 198 | $this->domain = $this->getBaseTagParsing()['host'] . '/'; |
||
| 199 | }else{ |
||
| 200 | $this->domain = $this->getPagePathParsing()['host'] . '/'; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | return $this->domain; |
||
| 204 | } |
||
| 205 | |||
| 206 | private function getScheme() |
||
| 207 | { |
||
| 208 | if ($this->scheme === null) { |
||
| 209 | if($this->getBaseTag() === null) { |
||
| 210 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
| 211 | }elseif(array_key_exists('scheme', $this->getBaseTagParsing())){ |
||
| 212 | $this->scheme = $this->getBaseTagParsing()['scheme']; |
||
| 213 | }else{ |
||
| 214 | $this->scheme = $this->getPagePathParsing()['scheme']; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | return $this->scheme; |
||
| 218 | } |
||
| 219 | |||
| 220 | private function getBaseTagParsing() |
||
| 225 | } |
||
| 226 | |||
| 227 | private function getPagePathParsing() |
||
| 228 | { |
||
| 229 | if($this->pagePathParsing == null) |
||
| 232 | } |
||
| 233 | } |