| Conditions | 25 |
| Paths | > 20000 |
| Total Lines | 122 |
| Code Lines | 80 |
| 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 |
||
| 95 | public function __construct($string = null) |
||
| 96 | { |
||
| 97 | $this->origin = isset($_SERVER[ 'HTTP_HOST' ]) |
||
| 98 | ? $_SERVER[ 'HTTP_HOST' ] |
||
| 99 | : $_SERVER[ 'SERVER_NAME' ]; |
||
| 100 | $this->scheme = is_https() |
||
| 101 | ? 'https' |
||
| 102 | : 'http'; |
||
| 103 | |||
| 104 | $paths = explode('.php', $_SERVER[ 'PHP_SELF' ]); |
||
| 105 | $paths = explode('/', trim($paths[ 0 ], '/')); |
||
| 106 | array_pop($paths); |
||
| 107 | |||
| 108 | $this->path = empty($paths) |
||
| 109 | ? null |
||
| 110 | : implode('/', $paths); |
||
| 111 | |||
| 112 | if (isset($string)) { |
||
| 113 | $this->string = trim($string, '/'); |
||
| 114 | $metadata = parse_url($string); |
||
| 115 | $metadata[ 'path' ] = empty($metadata[ 'path' ]) |
||
| 116 | ? null |
||
| 117 | : $metadata[ 'path' ]; |
||
| 118 | |||
| 119 | $this->scheme = empty($metadata[ 'scheme' ]) |
||
| 120 | ? $this->scheme |
||
| 121 | : $metadata[ 'scheme' ]; |
||
| 122 | |||
| 123 | if ($metadata[ 'path' ] === $this->string) { |
||
| 124 | $paths = explode('/', $this->string); |
||
| 125 | $this->origin = $paths[ 0 ]; |
||
| 126 | |||
| 127 | $this->path = implode('/', array_slice($paths, 1)); |
||
| 128 | } elseif (isset($metadata[ 'host' ])) { |
||
| 129 | $this->path = trim($metadata[ 'path' ]); |
||
| 130 | $this->origin = $metadata[ 'host' ]; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $directories = explode('/', str_replace('\\', '/', dirname($_SERVER[ 'SCRIPT_FILENAME' ]))); |
||
| 135 | $paths = explode('/', $this->path); |
||
| 136 | $paths = array_intersect($paths, $directories); |
||
| 137 | |||
| 138 | $this->path = '/' . trim(implode('/', $paths), '/'); |
||
| 139 | |||
| 140 | if (strpos($this->origin, 'www') !== false) { |
||
| 141 | $this->www = true; |
||
| 142 | $this->origin = ltrim($this->origin, 'www.'); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (preg_match('/(:)([0-9]+)/', $this->string, $matches)) { |
||
| 146 | $this->port = $matches[ 2 ]; |
||
| 147 | } |
||
| 148 | |||
| 149 | if (filter_var($this->origin, FILTER_VALIDATE_IP) !== false) { |
||
| 150 | $tlds = [$this->origin]; |
||
| 151 | } else { |
||
| 152 | $tlds = explode('.', $this->origin); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (count($tlds) > 1) { |
||
| 156 | foreach ($tlds as $key => $tld) { |
||
| 157 | if (strlen($tld) <= 3 AND $key >= 1) { |
||
| 158 | $this->tlds[] = $tld; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if (empty($this->tlds)) { |
||
| 163 | $this->tlds[] = end($tlds); |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->tld = '.' . implode('.', $this->tlds); |
||
|
|
|||
| 167 | |||
| 168 | $this->subDomains = array_diff($tlds, $this->tlds); |
||
| 169 | $this->subDomains = count($this->subDomains) == 0 |
||
| 170 | ? $this->tlds |
||
| 171 | : $this->subDomains; |
||
| 172 | |||
| 173 | $this->parentDomain = end($this->subDomains); |
||
| 174 | array_pop($this->subDomains); |
||
| 175 | |||
| 176 | $this->parentDomain = implode('.', array_slice($this->subDomains, 1)) |
||
| 177 | . '.' |
||
| 178 | . $this->parentDomain |
||
| 179 | . $this->tld; |
||
| 180 | $this->parentDomain = ltrim($this->parentDomain, '.'); |
||
| 181 | |||
| 182 | if (count($this->subDomains) > 0) { |
||
| 183 | $this->subDomain = reset($this->subDomains); |
||
| 184 | } |
||
| 185 | } else { |
||
| 186 | $this->parentDomain = $this->origin; |
||
| 187 | } |
||
| 188 | |||
| 189 | $ordinalEnds = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']; |
||
| 190 | |||
| 191 | foreach ($this->subDomains as $key => $subdomain) { |
||
| 192 | $ordinalNumber = count($tlds) - $key; |
||
| 193 | |||
| 194 | if ((($ordinalNumber % 100) >= 11) && (($ordinalNumber % 100) <= 13)) { |
||
| 195 | $ordinalKey = $ordinalNumber . 'th'; |
||
| 196 | } else { |
||
| 197 | $ordinalKey = $ordinalNumber . $ordinalEnds[ $ordinalNumber % 10 ]; |
||
| 198 | } |
||
| 199 | |||
| 200 | $this->subDomains[ $ordinalKey ] = $subdomain; |
||
| 201 | |||
| 202 | unset($this->subDomains[ $key ]); |
||
| 203 | } |
||
| 204 | |||
| 205 | foreach ($this->tlds as $key => $tld) { |
||
| 206 | $ordinalNumber = count($this->tlds) - $key; |
||
| 207 | |||
| 208 | if ((($ordinalNumber % 100) >= 11) && (($ordinalNumber % 100) <= 13)) { |
||
| 209 | $ordinalKey = $ordinalNumber . 'th'; |
||
| 210 | } else { |
||
| 211 | $ordinalKey = $ordinalNumber . $ordinalEnds[ $ordinalNumber % 10 ]; |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->tlds[ $ordinalKey ] = $tld; |
||
| 215 | |||
| 216 | unset($this->tlds[ $key ]); |
||
| 217 | } |
||
| 389 | } |