| Conditions | 23 |
| Paths | 58 |
| Total Lines | 81 |
| Code Lines | 57 |
| 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 |
||
| 111 | public function detect(string $userAgent): void |
||
| 112 | { |
||
| 113 | $regex = $this->regex(); |
||
| 114 | $regexLength = count($regex); |
||
| 115 | $i = 0; |
||
| 116 | $match = ''; |
||
| 117 | $matches = []; |
||
| 118 | |||
| 119 | while ($i < $regexLength && ! $matches) { |
||
|
|
|||
| 120 | $reg = $regex[$i]; |
||
| 121 | $property = $regex[$i + 1]; |
||
| 122 | |||
| 123 | $j = 0; |
||
| 124 | $k = 0; |
||
| 125 | |||
| 126 | $regLength = count($reg); |
||
| 127 | while ($j < $regLength && ! $matches) { |
||
| 128 | $pattern = $reg[$j++]; |
||
| 129 | if (is_string($pattern)) { |
||
| 130 | preg_match($pattern, $userAgent, $matches); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (count($matches) > 0) { |
||
| 134 | $lengthProperty = count($property); |
||
| 135 | for ($p = 0; $p < $lengthProperty; $p++) { |
||
| 136 | $q = $property[$p]; |
||
| 137 | if (array_key_exists(++$k, $matches)) { |
||
| 138 | $match = $matches[$k]; |
||
| 139 | } |
||
| 140 | |||
| 141 | if (is_array($q) && count($q) > 0) { |
||
| 142 | if (count($q) === 2) { |
||
| 143 | if (Helper::startsWith($q[1], '__')) { |
||
| 144 | $functionName = Helper::replaceFirst('__', '', $q[1]); |
||
| 145 | $result = null; |
||
| 146 | if (method_exists($this, $functionName)) { |
||
| 147 | $result = $this->{$functionName}($match); |
||
| 148 | } |
||
| 149 | $this->fillEntity([$q[0] => $result]); |
||
| 150 | } else { |
||
| 151 | $this->fillEntity([$q[0] => $q[1]]); |
||
| 152 | } |
||
| 153 | } elseif (count($q) === 3) { |
||
| 154 | if (Helper::startsWith($q[1], '__')) { |
||
| 155 | $functionName = Helper::replaceFirst('__', '', $q[1]); |
||
| 156 | $args = explode('.', $q[2]); |
||
| 157 | $argument = $this->maps(); |
||
| 158 | foreach ($args as $key) { |
||
| 159 | $argument = $argument[$key]; |
||
| 160 | } |
||
| 161 | |||
| 162 | $result = null; |
||
| 163 | if (method_exists($this, $functionName)) { |
||
| 164 | $result = $this->{$functionName}($match, $argument); |
||
| 165 | } |
||
| 166 | $this->fillEntity([$q[0] => $result]); |
||
| 167 | } else { |
||
| 168 | $replacedMatch = preg_replace($q[1], $q[2], (string)$match); |
||
| 169 | if ($replacedMatch !== null) { |
||
| 170 | $this->fillEntity([$q[0] => $replacedMatch]); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } elseif (count($q) === 4) { |
||
| 174 | if (Helper::startsWith($q[3], '__')) { |
||
| 175 | $functionName = Helper::replaceFirst('__', '', $q[3]); |
||
| 176 | $result = preg_replace($q[1], $q[2], (string)$match); |
||
| 177 | if (method_exists($this, $functionName)) { |
||
| 178 | $result = $this->{$functionName}($result); |
||
| 179 | } |
||
| 180 | $this->fillEntity([$q[0] => $result]); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | if (is_string($q)) { |
||
| 185 | $this->fillEntity([$q => $match]); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | $i += 2; |
||
| 192 | } |
||
| 282 |
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.