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