Conditions | 39 |
Paths | 30 |
Total Lines | 107 |
Code Lines | 57 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
237 | public function checkSyntax(): string |
||
238 | { |
||
239 | if (starts_with($this->source, '!')) { |
||
240 | # ! Definition |
||
241 | # - Must be formatted like this: |
||
242 | # ! type name = value |
||
243 | # OR |
||
244 | # ! type = value |
||
245 | # - Type options are NOT enforceable, for future compatibility; if RiveScript |
||
246 | # encounters a new type that it can't handle, it can safely warn and skip it. |
||
247 | if ($this->matchesPattern("/^.+(?:\s+.+|)\s*=\s*.+?$/", $this->source) === false) { |
||
248 | return "Invalid format for !Definition line: must be '! type name = value' OR '! type = value'"; |
||
249 | } |
||
250 | } elseif (starts_with($this->source, '>')) { |
||
251 | # > Label |
||
252 | # - The "begin" label must have only one argument ("begin") |
||
253 | # - "topic" labels must be lowercase but can inherit other topics ([A-Za-z0-9_\s]) |
||
254 | # - "object" labels follow the same rules as "topic" labels, but don't need be lowercase |
||
255 | if ($this->matchesPattern("/^begin/", $this->value) === true |
||
256 | && $this->matchesPattern("/^begin$/", $this->value) === false) { |
||
257 | return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"; |
||
258 | } elseif ($this->matchesPattern("/^topic/", $this->value) === true |
||
259 | && $this->matchesPattern("/[^a-z0-9_\-\s]/", $this->value) === true) { |
||
260 | return "Topics should be lowercased and contain only numbers and letters!"; |
||
261 | } elseif ($this->matchesPattern("/^object/", $this->value) === true |
||
262 | && $this->matchesPattern("/[^a-z0-9_\-\s]/", $this->value) === true) { |
||
263 | return "Objects can only contain numbers and lowercase letters!"; |
||
264 | } |
||
265 | } elseif (starts_with($this->source, '+') || starts_with($this->source, '%') |
||
266 | || starts_with($this->source, '@')) { |
||
267 | # + Trigger, % Previous, @ Redirect |
||
268 | # This one is strict. The triggers are to be run through Perl's regular expression |
||
269 | # engine. Therefore, it should be acceptable by the regexp engine. |
||
270 | # - Entirely lowercase |
||
271 | # - No symbols except: ( | ) [ ] * _ # @ { } < > = |
||
272 | # - All brackets should be matched |
||
273 | |||
274 | if ($this->allowUtf8 === true) { |
||
275 | if ($this->matchesPattern("/[A-Z\\.]/", $this->value) === true) { |
||
276 | return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."; |
||
277 | } |
||
278 | } elseif ($this->matchesPattern("/[^a-z0-9(\|)\[\]*_#\@{}<>=\s]/", $this->value) === true) { |
||
279 | return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="; |
||
280 | } |
||
281 | |||
282 | $parens = 0; # Open parenthesis |
||
283 | $square = 0; # Open square brackets |
||
284 | $curly = 0; # Open curly brackets |
||
285 | $chevron = 0; # Open angled brackets |
||
286 | $len = strlen($this->value); |
||
287 | |||
288 | for ($i = 0; $i < $len; $i++) { |
||
289 | $chr = $this->value[$i]; |
||
290 | |||
291 | # Count brackets. |
||
292 | if ($chr === '(') { |
||
293 | $parens++; |
||
294 | } |
||
295 | if ($chr === ')') { |
||
296 | $parens--; |
||
297 | } |
||
298 | if ($chr === '[') { |
||
299 | $square++; |
||
300 | } |
||
301 | if ($chr === ']') { |
||
302 | $square--; |
||
303 | } |
||
304 | if ($chr === '{') { |
||
305 | $curly++; |
||
306 | } |
||
307 | if ($chr === '}') { |
||
308 | $curly--; |
||
309 | } |
||
310 | if ($chr === '<') { |
||
311 | $chevron++; |
||
312 | } |
||
313 | if ($chr === '>') { |
||
314 | $chevron--; |
||
315 | } |
||
316 | } |
||
317 | |||
318 | if ($parens) { |
||
319 | return "Unmatched " . ($parens > 0 ? "left" : "right") . " parenthesis bracket ()"; |
||
320 | } |
||
321 | if ($square) { |
||
322 | return "Unmatched " . ($square > 0 ? "left" : "right") . " square bracket []"; |
||
323 | } |
||
324 | if ($curly) { |
||
325 | return "Unmatched " . ($curly > 0 ? "left" : "right") . " curly bracket {}"; |
||
326 | } |
||
327 | if ($chevron) { |
||
328 | return "Unmatched " . ($chevron > 0 ? "left" : "right") . " angled bracket <>"; |
||
329 | } |
||
330 | } elseif (starts_with($this->source, '-') || starts_with($this->source, '^') |
||
331 | || starts_with($this->source, '/')) { |
||
332 | # - Trigger, ^ Continue, / Comment |
||
333 | # These commands take verbatim arguments, so their syntax is loose. |
||
334 | } elseif (starts_with($this->source, '*') === true && $this->isComment() === false) { |
||
335 | # * Condition |
||
336 | # Syntax for a conditional is as follows: |
||
337 | # * value symbol value => response |
||
338 | if ($this->matchesPattern("/.+?\s(==|eq|!=|ne|<>|<|<=|>|>=)\s.+?=>.+?$/", $this->value) === false) { |
||
339 | return "Invalid format for !Condition: should be like `* value symbol value => response`"; |
||
340 | } |
||
341 | } |
||
342 | |||
343 | return ""; |
||
344 | } |
||
361 |