| Conditions | 16 |
| Paths | 39 |
| Total Lines | 25 |
| Code Lines | 17 |
| 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 |
||
| 36 | public static function backspaceCompare2(string $s, string $t): bool |
||
| 37 | { |
||
| 38 | if (empty($s) || empty($t)) { |
||
| 39 | return false; |
||
| 40 | } |
||
| 41 | [$i, $j] = [strlen($s) - 1, strlen($t) - 1]; |
||
| 42 | $m = $n = 0; |
||
| 43 | while (true) { |
||
| 44 | while ($i >= 0 && ($m > 0 || $s[$i] === '#')) { |
||
| 45 | $m += $s[$i] === '#' ? 1 : -1; |
||
| 46 | $i--; |
||
| 47 | } |
||
| 48 | while ($j >= 0 && ($n > 0 || $t[$j] === '#')) { |
||
| 49 | $n += $t[$j] === '#' ? 1 : -1; |
||
| 50 | $j--; |
||
| 51 | } |
||
| 52 | if ($i >= 0 && $j >= 0 && $s[$i] === $t[$j]) { |
||
| 53 | $i--; |
||
| 54 | $j--; |
||
| 55 | } else { |
||
| 56 | break; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | return $i === -1 && $j === -1; |
||
| 61 | } |
||
| 87 |
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.