2DSharp /
Phypes
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of Phypes <https://github.com/2DSharp/Phypes>. |
||
| 4 | * |
||
| 5 | * (c) Dedipyaman Das <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | |||
| 12 | namespace Phypes\Rule\Pattern; |
||
| 13 | |||
| 14 | |||
| 15 | use Phypes\Error\RuleError; |
||
| 16 | use Phypes\Error\RuleErrorCode; |
||
| 17 | use Phypes\Exception\InvalidRuleOption; |
||
| 18 | use Phypes\Result\Failure; |
||
| 19 | use Phypes\Result\Result; |
||
| 20 | use Phypes\Result\Success; |
||
| 21 | use Phypes\Rule\Rule; |
||
| 22 | |||
| 23 | class ContainsPattern implements Rule |
||
| 24 | { |
||
| 25 | const NUMBER = 0; |
||
| 26 | const UPPERCASE = 1; |
||
| 27 | const LOWERCASE = 2; |
||
| 28 | const SPECIAL_CHARS = 3; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int $pattern |
||
| 32 | */ |
||
| 33 | private $pattern; |
||
| 34 | /** |
||
| 35 | * ContainsPattern constructor. |
||
| 36 | * @param int $patternType |
||
| 37 | * @throws InvalidRuleOption |
||
| 38 | */ |
||
| 39 | public function __construct(int $patternType) |
||
| 40 | { |
||
| 41 | if ($patternType < 0 || $patternType > 3) |
||
| 42 | throw new InvalidRuleOption($patternType); |
||
|
0 ignored issues
–
show
|
|||
| 43 | |||
| 44 | $this->pattern = $patternType; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function validate($data): Result |
||
| 48 | { |
||
| 49 | $isValid = true; |
||
| 50 | switch ($this->pattern) |
||
| 51 | { |
||
| 52 | case self::NUMBER: |
||
| 53 | $isValid = preg_match('/[\d]/', $data); |
||
| 54 | break; |
||
| 55 | case self::SPECIAL_CHARS: |
||
| 56 | $isValid = preg_match('/[\W]/', $data); |
||
| 57 | break; |
||
| 58 | case self::LOWERCASE: |
||
| 59 | $isValid = preg_match('/[a-z]/', $data); |
||
| 60 | break; |
||
| 61 | case self::UPPERCASE: |
||
| 62 | $isValid = preg_match('/[A-Z]/', $data); |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($isValid) |
||
| 67 | return new Success(); |
||
| 68 | else |
||
| 69 | return new Failure(new RuleError(RuleErrorCode::PATTERN_MISMATCH, |
||
| 70 | "The string doesn't contain the required pattern")); |
||
| 71 | } |
||
| 72 | } |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.