Conditions | 11 |
Paths | 35 |
Total Lines | 73 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 3 |
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 namespace CMPayments\SchemaValidator\Validators; |
||
22 | public function validateRegex($data, $schema, $path) |
||
23 | { |
||
24 | if (!isset($schema->pattern)) { |
||
25 | |||
26 | return; |
||
27 | } |
||
28 | |||
29 | $pattern = '/' . trim($schema->pattern, '/') . '/'; |
||
30 | |||
31 | if (!in_array($schema->type, [BaseValidator::NUMBER, BaseValidator::STRING]) && !is_scalar($data)) { |
||
32 | |||
33 | $this->addError(ValidateException::ERROR_USER_REGEX_DATA_NOT_SCALAR, [$data]); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Use try catch to be able to handle malformed regex |
||
38 | */ |
||
39 | try { |
||
40 | |||
41 | // must suppress the output... !@#$%^&*( |
||
42 | $result = @preg_match($pattern, $data); |
||
43 | |||
44 | // successful preg_match but no match |
||
45 | if ($result === 0) { |
||
46 | |||
47 | $this->addError(ValidateException::ERROR_USER_REGEX_NO_MATCH, [$data, $path]); |
||
48 | } elseif ($result === false) { |
||
49 | |||
50 | // preg_match resulted in an error, there are multiple causes why preg_match would result in an error |
||
51 | $pregErrors = [ |
||
52 | PREG_INTERNAL_ERROR => ValidateException::PREG_INTERNAL_ERROR, |
||
53 | PREG_BACKTRACK_LIMIT_ERROR => ValidateException::PREG_BACKTRACK_LIMIT_ERROR, |
||
54 | PREG_RECURSION_LIMIT_ERROR => ValidateException::PREG_RECURSION_LIMIT_ERROR, |
||
55 | PREG_BAD_UTF8_ERROR => ValidateException::PREG_BAD_UTF8_ERROR, |
||
56 | PREG_BAD_UTF8_OFFSET_ERROR => ValidateException::PREG_BAD_UTF8_OFFSET_ERROR |
||
57 | ]; |
||
58 | |||
59 | // when dealing with a > PHP 7 environment another preg_last_error() error became available, if so add it to the list |
||
60 | if (PHP_VERSION_ID > 70000) { |
||
61 | |||
62 | $pregErrors[PREG_JIT_STACKLIMIT_ERROR] = ValidateException::PREG_JIT_STACK_LIMIT_ERROR; |
||
63 | } |
||
64 | |||
65 | // check for preg_match error occurred |
||
66 | if (isset($pregErrors[preg_last_error()])) { |
||
67 | |||
68 | $this->addError(ValidateException::ERROR_USER_REGEX_PREG_LAST_ERROR_OCCURRED, [$schema->pattern, $data, $pregErrors[preg_last_error()]]); |
||
69 | } elseif (($error = error_get_last()) !== null) { |
||
70 | |||
71 | // if the string 'preg_match()' is part of the error message we need to strip it |
||
72 | // because the error message is returned to the user and we don't want to reveal |
||
73 | // anything to the user about how we are matching patterns on a string. |
||
74 | // HHVM and PHP return different kind of error messages.. |
||
75 | // HHVM does not prepend the string 'preg_match()' to the error message where PHP does prepend 'preg_match()'.. |
||
76 | $this->addError( |
||
77 | ValidateException::ERROR_USER_REGEX_ERROR_LAST_ERROR_OCCURRED, |
||
78 | [ |
||
79 | $schema->pattern, |
||
80 | $data, |
||
81 | ((strpos($error['message'], 'preg_match()') !== false) ? substr($error['message'], strlen('preg_match(): ')) : $error['message']) |
||
82 | ] |
||
83 | ); |
||
84 | } else { |
||
85 | |||
86 | // unknown error.. |
||
87 | $this->addError(ValidateException::ERROR_USER_REGEX_UNKNOWN_ERROR_OCCURRED, [$schema->pattern, $data]); |
||
88 | } |
||
89 | } |
||
90 | } catch (\Exception $ex) { |
||
91 | |||
92 | $this->addError(ValidateException::ERROR_USER_REGEX_GENERAL_ERROR_OCCURRED, [$schema->pattern, $data]); |
||
93 | } |
||
94 | } |
||
95 | |||
104 |