| Conditions | 24 |
| Paths | 66 |
| Total Lines | 82 |
| Code Lines | 58 |
| 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 |
||
| 113 | public function detect(string $userAgent): void |
||
| 114 | { |
||
| 115 | $regex = $this->regex(); |
||
| 116 | $regexLength = count($regex); |
||
| 117 | $i = 0; |
||
| 118 | $match = null; |
||
| 119 | $matches = []; |
||
| 120 | |||
| 121 | while ($i < $regexLength && ! $matches) { |
||
|
|
|||
| 122 | $reg = $regex[$i]; |
||
| 123 | $property = $regex[$i + 1]; |
||
| 124 | |||
| 125 | $j = 0; |
||
| 126 | $k = 0; |
||
| 127 | |||
| 128 | $regLength = count($reg); |
||
| 129 | while ($j < $regLength && ! $matches) { |
||
| 130 | $pattern = $reg[$j++]; |
||
| 131 | if (is_string($pattern)) { |
||
| 132 | preg_match($pattern, $userAgent, $matches); |
||
| 133 | } |
||
| 134 | |||
| 135 | if (count($matches) > 0) { |
||
| 136 | $lengthProperty = count($property); |
||
| 137 | for ($p = 0; $p < $lengthProperty; $p++) { |
||
| 138 | $q = $property[$p]; |
||
| 139 | if (array_key_exists(++$k, $matches)) { |
||
| 140 | $match = $matches[$k]; |
||
| 141 | } |
||
| 142 | |||
| 143 | if (is_array($q) && count($q) > 0) { |
||
| 144 | if (count($q) === 2) { |
||
| 145 | if (Helper::startsWith($q[1], '__')) { |
||
| 146 | $functionName = Helper::replaceFirst('__', '', $q[1]); |
||
| 147 | $result = null; |
||
| 148 | if (method_exists($this, $functionName)) { |
||
| 149 | $result = $this->{$functionName}($match); |
||
| 150 | } |
||
| 151 | $this->fillEntity([$q[0] => $result]); |
||
| 152 | } else { |
||
| 153 | $this->fillEntity([$q[0] => $q[1]]); |
||
| 154 | } |
||
| 155 | } elseif (count($q) === 3) { |
||
| 156 | if (Helper::startsWith($q[1], '__')) { |
||
| 157 | $functionName = Helper::replaceFirst('__', '', $q[1]); |
||
| 158 | $args = explode('.', $q[2]); |
||
| 159 | $argument = $this->maps(); |
||
| 160 | if (is_array($args)) { |
||
| 161 | foreach ($args as $key) { |
||
| 162 | $argument = $argument[$key]; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | $result = null; |
||
| 166 | if (method_exists($this, $functionName)) { |
||
| 167 | $result = $this->{$functionName}($match, $argument); |
||
| 168 | } |
||
| 169 | $this->fillEntity([$q[0] => $result]); |
||
| 170 | } else { |
||
| 171 | $replacedMatch = preg_replace($q[1], $q[2], $match); |
||
| 172 | if ($replacedMatch !== null) { |
||
| 173 | $this->fillEntity([$q[0] => $replacedMatch]); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } elseif (count($q) === 4) { |
||
| 177 | if (Helper::startsWith($q[3], '__')) { |
||
| 178 | $functionName = Helper::replaceFirst('__', '', $q[3]); |
||
| 179 | $result = preg_replace($q[1], $q[2], $match); |
||
| 180 | if (method_exists($this, $functionName)) { |
||
| 181 | $result = $this->{$functionName}($result); |
||
| 182 | } |
||
| 183 | $this->fillEntity([$q[0] => $result]); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } else { |
||
| 187 | if (is_string($q)) { |
||
| 188 | $this->fillEntity([$q => $match]); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | $i += 2; |
||
| 195 | } |
||
| 281 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.