| Conditions | 15 |
| Paths | 37 |
| Total Lines | 29 |
| Code Lines | 18 |
| 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 |
||
| 43 | public static function isMatch2(string $s, string $p): bool |
||
| 44 | { |
||
| 45 | if (empty($s) && empty($p)) { |
||
| 46 | return true; |
||
| 47 | } |
||
| 48 | [$m, $n] = [strlen($s) + 1, strlen($p) + 1]; |
||
| 49 | $dp = array_fill(0, $m, array_fill(0, $n, false)); |
||
| 50 | $dp[0][0] = true; // since empty string matches empty pattern |
||
| 51 | for ($j = 2; $j < $n; $j += 2) { |
||
| 52 | if ($p[$j - 1] === '*' && $dp[0][$j - 2]) { |
||
| 53 | $dp[0][$j] = true; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | for ($i = 1; $i < $m; $i++) { |
||
| 57 | for ($j = 1; $j < $n; $j++) { |
||
| 58 | if ($s[$i - 1] === $p[$j - 1] || $p[$j - 1] === '.') { |
||
| 59 | $dp[$i][$j] = $dp[$i - 1][$j - 1]; |
||
| 60 | } |
||
| 61 | if ($p[$j - 1] === '*') { |
||
| 62 | if ($p[$j - 2] !== '.' && $p[$j - 2] !== $s[$i - 1]) { |
||
| 63 | $dp[$i][$j] = $dp[$i][$j - 2]; |
||
| 64 | } else { |
||
| 65 | $dp[$i][$j] = ($dp[$i][$j - 2] || $dp[$i - 1][$j - 2] || $dp[$i - 1][$j]); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return (bool) $dp[$m - 1][$n - 1]; |
||
| 72 | } |
||
| 74 |