| Conditions | 7 |
| Paths | 13 |
| Total Lines | 54 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 107 | protected function preauth(string $value): bool |
||
| 108 | { |
||
| 109 | $parts = explode('|', $value); |
||
| 110 | |||
| 111 | if (count($parts) !== 2) { |
||
| 112 | $this->logger->warning('invalid header x-preauth value, parts != 2', [ |
||
| 113 | 'category' => get_class($this) |
||
| 114 | ]); |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | $found = false; |
||
| 120 | foreach ($this->source as $ip) { |
||
| 121 | if ($this->ipInRange($_SERVER['REMOTE_ADDR'], $ip)) { |
||
| 122 | $this->logger->debug('x-preauth authentication request from known network ['.$_SERVER['REMOTE_ADDR'].']', [ |
||
| 123 | 'category' => get_class($this) |
||
| 124 | ]); |
||
| 125 | |||
| 126 | $found = true; |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($found === false) { |
||
| 132 | $this->logger->warning('x-preauth authentication request from unknown network ['.$_SERVER['REMOTE_ADDR'].']', [ |
||
| 133 | 'category' => get_class($this) |
||
| 134 | ]); |
||
| 135 | |||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | $key = $parts[1]; |
||
| 140 | $account = $parts[0]; |
||
| 141 | |||
| 142 | if (!$this->checkLdapUser($account)) { |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($key !== $this->key) { |
||
| 147 | $this->logger->warning('invalid x-preauth key value, wrong key?', [ |
||
| 148 | 'category' => get_class($this) |
||
| 149 | ]); |
||
| 150 | |||
| 151 | return false; |
||
| 152 | } else { |
||
| 153 | $this->logger->info('valid x-preauth key value found', [ |
||
| 154 | 'category' => get_class($this) |
||
| 155 | ]); |
||
| 156 | |||
| 157 | $this->identity = $account; |
||
| 158 | return true; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 224 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: