| Conditions | 12 |
| Paths | 19 |
| Total Lines | 32 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 isMatch(string $s, string $p): bool |
||
| 10 | { |
||
| 11 | if (empty($s) && empty($p)) { |
||
| 12 | return true; |
||
| 13 | } |
||
| 14 | [$m, $n] = [strlen($s) + 1, strlen($p) + 1]; |
||
| 15 | $dp = array_fill(0, $m, array_fill(0, $n, false)); |
||
| 16 | $dp[0][0] = true; |
||
| 17 | |||
| 18 | for ($j = 1; $j < $n; $j++) { |
||
| 19 | if ($p[$j - 1] === '*') { |
||
| 20 | $dp[0][$j] = $dp[0][$j - 2]; |
||
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | for ($i = 1; $i < $m; $i++) { |
||
| 25 | for ($j = 1; $j < $n; $j++) { |
||
| 26 | if ($p[$j - 1] === '.' || $p[$j - 1] === $s[$i - 1]) { |
||
| 27 | $dp[$i][$j] = $dp[$i - 1][$j - 1]; |
||
| 28 | } elseif ($p[$j - 1] === '*') { |
||
| 29 | $dp[$i][$j] = $dp[$i][$j - 2]; |
||
| 30 | |||
| 31 | if ($p[$j - 2] === '.' || $p[$j - 2] === $s[$i - 1]) { |
||
| 32 | $dp[$i][$j] |= $dp[$i - 1][$j]; |
||
| 33 | } |
||
| 34 | } else { |
||
| 35 | $dp[$i][$j] = false; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | return (bool) $dp[$m - 1][$n - 1]; |
||
| 41 | } |
||
| 74 |