Conditions | 12 |
Paths | 4 |
Total Lines | 79 |
Code Lines | 35 |
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 |
||
64 | public function parse(string $trigger, Input $input): bool |
||
65 | { |
||
66 | if ($this->matchesPattern($this->pattern, $trigger) === true) { |
||
67 | $triggerString = $trigger; |
||
68 | $matches = $this->getMatchesFromPattern($this->pattern, $triggerString); |
||
69 | $sets = []; |
||
70 | |||
71 | /** |
||
72 | * Replace every "set" in the trigger to their index number |
||
73 | * found in the string. |
||
74 | * |
||
75 | * Example: |
||
76 | * |
||
77 | * "I (am|love) a robot. I like (my|style)" |
||
78 | * |
||
79 | * Will be replaced with: |
||
80 | * |
||
81 | * "I {0} a robot. I like {1}" |
||
82 | */ |
||
83 | |||
84 | foreach ($matches as $index => $match) { |
||
|
|||
85 | $set = explode("|", $match[2]); |
||
86 | |||
87 | if (count($set) > 0) { |
||
88 | if ($match[1] === '(') { |
||
89 | foreach ($set as $setIndex => $item) { |
||
90 | $set[$setIndex] = $this->signatures['alternation'] . $item; |
||
91 | } |
||
92 | $triggerString = str_replace($match[0], "{{$index}}", $triggerString); |
||
93 | $sets [] = $set; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * We need to add possible optionals to the |
||
98 | * set as well. This programming is a bit odd |
||
99 | * but there is no way around it. |
||
100 | */ |
||
101 | if ($match[1] === '[') { |
||
102 | $set[] = $this->signatures['optional']; // "__\x01\x20__"; |
||
103 | $triggerString = str_replace($match[0], "{{$index}}", $triggerString); |
||
104 | $sets [] = $set; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | |||
110 | $combinations = $this->getCombinations(...$sets); |
||
111 | |||
112 | if (count($combinations) > 0) { |
||
113 | $sentences = []; |
||
114 | |||
115 | foreach ($combinations as $combination) { |
||
116 | $tmp = $triggerString; |
||
117 | foreach ($combination as $index => $string) { |
||
118 | $tmp = str_replace("{{$index}}", $string, $tmp); |
||
119 | } |
||
120 | |||
121 | $tmp = str_replace([$this->signatures['optional'] . " ", $this->signatures['optional']], "", $tmp); |
||
122 | $tmp = trim($tmp); |
||
123 | |||
124 | $sentences [] = $tmp; |
||
125 | } |
||
126 | |||
127 | $signature = $this->signatures['alternation']; |
||
128 | $cmp = [$this, 'isMatchesWithoutSignature']; |
||
129 | |||
130 | $result = array_filter($sentences, static function (string $sentence) use ($input, $signature, $cmp) { |
||
131 | if (strpos($sentence, $signature) > -1) { |
||
132 | return $cmp(strtolower($sentence), strtolower($input->source())); |
||
133 | } |
||
134 | return false; |
||
135 | }); |
||
136 | |||
137 | if (count($result) > 0) { |
||
138 | return $input->source(); |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | return false; |
||
143 | } |
||
204 |