Conditions | 10 |
Paths | 17 |
Total Lines | 46 |
Code Lines | 24 |
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 |
||
79 | public function checkPattern(string $string, string $pattern): bool |
||
80 | { |
||
81 | $parts = explode('*', $pattern); |
||
82 | $index = 0; |
||
83 | |||
84 | $shouldBeFirst = true; |
||
85 | |||
86 | foreach ($parts as $part) { |
||
87 | if ($part == '') { |
||
88 | $shouldBeFirst = false; |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | $index = strpos($string, $part, $index); |
||
93 | |||
94 | if ($index === false) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | if ($shouldBeFirst && $index > 0) { |
||
99 | return false; |
||
100 | } |
||
101 | |||
102 | $shouldBeFirst = false; |
||
103 | $index += strlen($part); |
||
104 | } |
||
105 | |||
106 | if (count($parts) == 1) { |
||
107 | return $string == $pattern; |
||
108 | } |
||
109 | |||
110 | $last = end($parts); |
||
111 | |||
112 | if ($last == '') { |
||
113 | return true; |
||
114 | } |
||
115 | |||
116 | if (strrpos($string, $last) === false) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | if (strlen($string) - strlen($last) - strrpos($string, $last) > 0) { |
||
121 | return false; |
||
122 | } |
||
123 | |||
124 | return true; |
||
125 | } |
||
126 | } |