Conditions | 10 |
Paths | 7 |
Total Lines | 26 |
Code Lines | 14 |
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 |
||
58 | public function ThrowIfLocalAddress(string $uri) : void { |
||
59 | $host = parse_url($uri, PHP_URL_HOST); |
||
60 | if ($host === false || $host === null) { |
||
61 | $this->logger->warning("Could not detect any host in $uri"); |
||
62 | throw new LocalServerException('Could not detect any host'); |
||
63 | } |
||
64 | |||
65 | $host = strtolower($host); |
||
66 | // Remove brackets from IPv6 addresses |
||
67 | if (strpos($host, '[') === 0 && substr($host, -1) === ']') { |
||
68 | $host = substr($host, 1, -1); |
||
69 | } |
||
70 | |||
71 | // Disallow localhost and local network |
||
72 | if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') { |
||
73 | $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
||
74 | throw new LocalServerException('Host violates local access rules'); |
||
75 | } |
||
76 | |||
77 | // Disallow hostname only |
||
78 | if (substr_count($host, '.') === 0 && !(bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
79 | $this->logger->warning("Host $host was not connected to because it violates local access rules"); |
||
80 | throw new LocalServerException('Host violates local access rules'); |
||
81 | } |
||
82 | |||
83 | $this->ThrowIfLocalIp($host); |
||
84 | } |
||
86 |