Conditions | 9 |
Paths | 4 |
Total Lines | 62 |
Code Lines | 25 |
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 |
||
55 | public function parse(string $trigger, Input $input): bool |
||
56 | { |
||
57 | if ($this->matchesPattern($this->pattern, $trigger) === true) { |
||
58 | $triggerString = $trigger; |
||
59 | $matches = $this->getMatchesFromPattern($this->pattern, $triggerString); |
||
60 | $sets = []; |
||
61 | |||
62 | /** |
||
63 | * Replace every "set" in the trigger to their index number |
||
64 | * found in the string. |
||
65 | * |
||
66 | * Example: |
||
67 | * |
||
68 | * "I (am|love) a robot. I like (my|style)" |
||
69 | * |
||
70 | * Will be replaced with: |
||
71 | * |
||
72 | * "I {0} a robot. I like {1}" |
||
73 | */ |
||
74 | foreach ($matches as $index => $match) { |
||
|
|||
75 | $set = explode("|", $match[2]); |
||
76 | |||
77 | /** |
||
78 | * To the set we add and empty value. This will emulate |
||
79 | * the optional keywords not being used. |
||
80 | */ |
||
81 | $set[] = ""; |
||
82 | |||
83 | if (count($set) > 0) { |
||
84 | $triggerString = str_replace($match[0], "{{$index}}", $triggerString); |
||
85 | $sets [] = $set; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $combinations = $this->getCombinations(...$sets); |
||
90 | |||
91 | if (count($combinations) > 0) { |
||
92 | $sentences = []; |
||
93 | |||
94 | foreach ($combinations as $combination) { |
||
95 | $tmp = $triggerString; |
||
96 | foreach ($combination as $index => $string) { |
||
97 | $tmp = str_replace("{{$index}}", $string, $tmp); |
||
98 | |||
99 | if (empty($string) === true) { |
||
100 | $tmp = str_replace("\x20\x20", " ", $tmp); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | $sentences [] = trim($tmp); |
||
105 | } |
||
106 | |||
107 | $result = array_filter($sentences, static function (string $sentence) use ($input) { |
||
108 | return (strtolower($sentence) === strtolower($input->source())); |
||
109 | }); |
||
110 | |||
111 | if (count($result) > 0) { |
||
112 | return $input->source(); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | return false; |
||
117 | } |
||
147 |