ReservedKeywordHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 91
c 1
b 0
f 0
dl 0
loc 110
ccs 0
cts 12
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isReservedKeyword() 0 3 1
A validate() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Validator;
6
7
use Yiisoft\Validator\Exception\UnexpectedRuleException;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\RuleHandlerInterface;
10
use Yiisoft\Validator\ValidationContext;
11
12
final class ReservedKeywordHandler implements RuleHandlerInterface
13
{
14
    private const KEYWORDS = [
15
        '__class__',
16
        '__dir__',
17
        '__file__',
18
        '__function__',
19
        '__line__',
20
        '__method__',
21
        '__namespace__',
22
        '__trait__',
23
        'abstract',
24
        'and',
25
        'array',
26
        'as',
27
        'break',
28
        'case',
29
        'catch',
30
        'callable',
31
        'cfunction',
32
        'class',
33
        'clone',
34
        'const',
35
        'continue',
36
        'declare',
37
        'default',
38
        'die',
39
        'do',
40
        'echo',
41
        'else',
42
        'elseif',
43
        'empty',
44
        'enddeclare',
45
        'endfor',
46
        'endforeach',
47
        'endif',
48
        'endswitch',
49
        'endwhile',
50
        'eval',
51
        'exception',
52
        'exit',
53
        'extends',
54
        'final',
55
        'finally',
56
        'for',
57
        'foreach',
58
        'function',
59
        'global',
60
        'goto',
61
        'if',
62
        'implements',
63
        'include',
64
        'include_once',
65
        'instanceof',
66
        'insteadof',
67
        'interface',
68
        'isset',
69
        'list',
70
        'namespace',
71
        'new',
72
        'old_function',
73
        'or',
74
        'parent',
75
        'php_user_filter',
76
        'print',
77
        'private',
78
        'protected',
79
        'public',
80
        'require',
81
        'require_once',
82
        'return',
83
        'static',
84
        'switch',
85
        'this',
86
        'throw',
87
        'trait',
88
        'try',
89
        'unset',
90
        'use',
91
        'var',
92
        'while',
93
        'xor',
94
        'fn',
95
    ];
96
97
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
98
    {
99
        if (!$rule instanceof ReservedKeywordRule) {
100
            throw new UnexpectedRuleException(ReservedKeywordRule::class, $rule);
101
        }
102
103
        $result = new Result();
104
        if (self::isReservedKeyword($value)) {
105
            $result->addError(
106
                message: 'The value {value} is reserved keyword.',
107
                parameters: ['value' => $value],
108
            );
109
        }
110
111
        return $result;
112
    }
113
114
    /**
115
     * @param string $value the attribute to be validated
116
     *
117
     * @return bool whether the value is a reserved PHP keyword.
118
     */
119
    private static function isReservedKeyword(string $value): bool
120
    {
121
        return in_array(strtolower($value), self::KEYWORDS, true);
122
    }
123
}
124