| Conditions | 11 |
| Paths | 4 |
| 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 |
||
| 9 | public static function backspaceCompare(string $s, string $t): bool |
||
| 10 | { |
||
| 11 | if (empty($s) || empty($t)) { |
||
| 12 | return false; |
||
| 13 | } |
||
| 14 | $helper = static function (string &$s, int &$i) { |
||
| 15 | $n = 0; |
||
| 16 | while ($i >= 0 && ($n > 0 || $s[$i] === '#')) { |
||
| 17 | $n = $s[$i] === '#' ? $n + 1 : $n - 1; |
||
| 18 | $i--; |
||
| 19 | } |
||
| 20 | return $i >= 0 ? $s[$i] : '#'; |
||
| 21 | }; |
||
| 22 | [$i, $j] = [strlen($s) - 1, strlen($t) - 1]; |
||
| 23 | while ($i >= 0 || $j >= 0) { |
||
| 24 | $p = $helper($s, $i); |
||
| 25 | $q = $helper($t, $j); |
||
| 26 | if ($p !== $q) { |
||
| 27 | return false; |
||
| 28 | } |
||
| 29 | $i--; |
||
| 30 | $j--; |
||
| 31 | } |
||
| 32 | |||
| 33 | return true; |
||
| 34 | } |
||
| 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.