| Conditions | 8 |
| Paths | 4 |
| Total Lines | 52 |
| Code Lines | 22 |
| 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 |
||
| 51 | public function parse(string $trigger, Input $input): bool |
||
| 52 | { |
||
| 53 | if ($this->matchesPattern($this->pattern, $trigger) === true) { |
||
| 54 | $triggerString = $trigger; |
||
| 55 | $matches = $this->getMatchesFromPattern($this->pattern, $triggerString); |
||
| 56 | $sets = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Replace every "set" in the trigger to their index number |
||
| 60 | * found in the string. |
||
| 61 | * |
||
| 62 | * Example: |
||
| 63 | * |
||
| 64 | * "I (am|love) a robot. I like (my|style)" |
||
| 65 | * |
||
| 66 | * Will be replaced with: |
||
| 67 | * |
||
| 68 | * "I {0} a robot. I like {1}" |
||
| 69 | */ |
||
| 70 | foreach ($matches as $index => $match) { |
||
|
|
|||
| 71 | $set = explode("|", $match[2]); |
||
| 72 | |||
| 73 | if (count($set) > 0) { |
||
| 74 | $triggerString = str_replace($match[0], "{{$index}}", $triggerString); |
||
| 75 | $sets [] = $set; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | $combinations = $this->getCombinations(...$sets); |
||
| 80 | |||
| 81 | if (count($combinations) > 0) { |
||
| 82 | $sentences = []; |
||
| 83 | |||
| 84 | foreach ($combinations as $combination) { |
||
| 85 | $tmp = $triggerString; |
||
| 86 | foreach ($combination as $index => $string) { |
||
| 87 | $tmp = str_replace("{{$index}}", $string, $tmp); |
||
| 88 | } |
||
| 89 | |||
| 90 | $sentences [] = $tmp; |
||
| 91 | } |
||
| 92 | |||
| 93 | $result = array_filter($sentences, static function (string $sentence) use ($input) { |
||
| 94 | return (strtolower($sentence) === strtolower($input->source())); |
||
| 95 | }); |
||
| 96 | |||
| 97 | if (count($result) > 0) { |
||
| 98 | return $input->source(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | return false; |
||
| 103 | } |
||
| 133 |