| 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 |
||
| 262 | public function checkSyntax(): string |
||
| 263 | { |
||
| 264 | if (starts_with($this->source, '!')) { |
||
| 265 | # ! Definition |
||
| 266 | # - Must be formatted like this: |
||
| 267 | # ! type name = value |
||
| 268 | # OR |
||
| 269 | # ! type = value |
||
| 270 | # - Type options are NOT enforceable, for future compatibility; if RiveScript |
||
| 271 | # encounters a new type that it can't handle, it can safely warn and skip it. |
||
| 272 | if ($this->matchesPattern("/^.+(?:\s+.+|)\s*=\s*.+?$/", $this->source) === false) { |
||
| 273 | return "Invalid format for !Definition line: must be '! type name = value' OR '! type = value'"; |
||
| 274 | } |
||
| 275 | } elseif (starts_with($this->source, '>')) { |
||
| 276 | # > Label |
||
| 277 | # - The "begin" label must have only one argument ("begin") |
||
| 278 | # - "topic" labels must be lowercase but can inherit other topics ([A-Za-z0-9_\s]) |
||
| 279 | # - "object" labels follow the same rules as "topic" labels, but don't need be lowercase |
||
| 280 | if ($this->matchesPattern("/^begin/", $this->value) === true |
||
| 281 | && $this->matchesPattern("/^begin$/", $this->value) === false) { |
||
| 282 | return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"; |
||
| 283 | } elseif ($this->matchesPattern("/^topic/", $this->value) === true |
||
| 284 | && $this->matchesPattern("/[^a-z0-9_\-\s]/", $this->value) === true) { |
||
| 285 | return "Topics should be lowercased and contain only numbers and letters!"; |
||
| 286 | } elseif ($this->matchesPattern("/^object/", $this->value) === true |
||
| 287 | && $this->matchesPattern("/[^a-z0-9_\-\s]/", $this->value) === true) { |
||
| 288 | return "Objects can only contain numbers and lowercase letters!"; |
||
| 289 | } |
||
| 290 | } elseif (starts_with($this->source, '+') || starts_with($this->source, '%') |
||
| 291 | || starts_with($this->source, '@')) { |
||
| 292 | # + Trigger, % Previous, @ Redirect |
||
| 293 | # This one is strict. The triggers are to be run through Perl's regular expression |
||
| 294 | # engine. Therefore, it should be acceptable by the regexp engine. |
||
| 295 | # - Entirely lowercase |
||
| 296 | # - No symbols except: ( | ) [ ] * _ # @ { } < > = |
||
| 297 | # - All brackets should be matched |
||
| 298 | |||
| 299 | if ($this->allowUtf8 === true) { |
||
| 300 | if ($this->matchesPattern("/[A-Z\\.]/", $this->value) === true) { |
||
| 301 | return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."; |
||
| 302 | } |
||
| 303 | } elseif ($this->matchesPattern("/[^a-z0-9(\|)\[\]*_#\@{}<>=\s]/", $this->value) === true) { |
||
| 304 | return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="; |
||
| 305 | } |
||
| 306 | |||
| 307 | $parens = 0; # Open parenthesis |
||
| 308 | $square = 0; # Open square brackets |
||
| 309 | $curly = 0; # Open curly brackets |
||
| 310 | $chevron = 0; # Open angled brackets |
||
| 311 | $len = strlen($this->value); |
||
| 312 | |||
| 313 | for ($i = 0; $i < $len; $i++) { |
||
| 314 | $chr = $this->value[$i]; |
||
| 315 | |||
| 316 | # Count brackets. |
||
| 317 | if ($chr === '(') { |
||
| 318 | $parens++; |
||
| 319 | } |
||
| 320 | if ($chr === ')') { |
||
| 321 | $parens--; |
||
| 322 | } |
||
| 323 | if ($chr === '[') { |
||
| 324 | $square++; |
||
| 325 | } |
||
| 326 | if ($chr === ']') { |
||
| 327 | $square--; |
||
| 328 | } |
||
| 329 | if ($chr === '{') { |
||
| 330 | $curly++; |
||
| 331 | } |
||
| 332 | if ($chr === '}') { |
||
| 333 | $curly--; |
||
| 334 | } |
||
| 335 | if ($chr === '<') { |
||
| 336 | $chevron++; |
||
| 337 | } |
||
| 338 | if ($chr === '>') { |
||
| 339 | $chevron--; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | |||
| 343 | if ($parens) { |
||
| 344 | return "Unmatched " . ($parens > 0 ? "left" : "right") . " parenthesis bracket ()"; |
||
| 345 | } |
||
| 346 | if ($square) { |
||
| 347 | return "Unmatched " . ($square > 0 ? "left" : "right") . " square bracket []"; |
||
| 348 | } |
||
| 349 | if ($curly) { |
||
| 350 | return "Unmatched " . ($curly > 0 ? "left" : "right") . " curly bracket {}"; |
||
| 351 | } |
||
| 352 | if ($chevron) { |
||
| 353 | return "Unmatched " . ($chevron > 0 ? "left" : "right") . " angled bracket <>"; |
||
| 354 | } |
||
| 355 | } elseif (starts_with($this->source, '-') || starts_with($this->source, '^') |
||
| 356 | || starts_with($this->source, '/')) { |
||
| 357 | # - Trigger, ^ Continue, / Comment |
||
| 358 | # These commands take verbatim arguments, so their syntax is loose. |
||
| 359 | } elseif (starts_with($this->source, '*') === true && $this->isComment() === false) { |
||
| 360 | # * Condition |
||
| 361 | # Syntax for a conditional is as follows: |
||
| 362 | # * value symbol value => response |
||
| 363 | if ($this->matchesPattern("/.+?\s(==|eq|!=|ne|<>|<|<=|>|>=)\s.+?=>.+?$/", $this->value) === false) { |
||
| 364 | return "Invalid format for !Condition: should be like `* value symbol value => response`"; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | return ""; |
||
| 369 | } |
||
| 386 |
This check looks for private methods that have been defined, but are not used inside the class.