1 | <?php |
||
23 | class CamelCaseVariableSniff extends AbstractSniff |
||
24 | { |
||
25 | use VariableRegistrationTrait; |
||
26 | |||
27 | /** |
||
28 | * You MUST provide your vars in camel case, lower case first. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | public const CODE_NOT_CAMEL_CASE = 'NotCamelCase'; |
||
33 | |||
34 | /** |
||
35 | * The error message for this sniff. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private const MESSAGE_NOT_CAMEL_CASE = 'Variable %s should be in camelCase (lowercase first).'; |
||
40 | |||
41 | /** |
||
42 | * The previously sniffed file. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private string $prevSniffedFile = ''; |
||
|
|||
47 | |||
48 | /** |
||
49 | * The vars which were sniffed in this file. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private array $sniffedVars = []; |
||
54 | |||
55 | /** |
||
56 | * Returns true if there is a value assignment or a property declaration, but which can be without an assignment. |
||
57 | * |
||
58 | * @return bool |
||
59 | */ |
||
60 | protected function areRequirementsMet(): bool |
||
61 | { |
||
62 | $var = $this->token['content']; |
||
63 | |||
64 | // We need to check everything != $this. |
||
65 | if ($return = $var !== '$this') { |
||
66 | $nextPos = (int) TokenHelper::findNextEffective($this->file, $this->stackPos + 1); |
||
67 | |||
68 | if ($nextPos > 0) { |
||
69 | $isProperty = PropertyHelper::isProperty($this->file, $this->stackPos); |
||
70 | $nextTokenCode = $this->tokens[$nextPos]['code']; |
||
71 | |||
72 | // The var should be followed by an "=" or can be followed by a semicolon if its a property. |
||
73 | $return = ($nextTokenCode === T_EQUAL) || ($nextTokenCode === T_SEMICOLON && $isProperty); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return $return; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Processes the token. |
||
82 | * |
||
83 | * @see self::setUp() |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | protected function processToken(): void |
||
88 | { |
||
89 | // "Sniff" the var name only once, but register the possible error everytime the var is declared. |
||
90 | if (!$this->sniffedVars[$var = $this->token['content']]) { |
||
91 | $this->file->recordMetric($this->stackPos, 'CamelCase var name', 'no'); |
||
92 | |||
93 | if (!$this->isSniffSuppressed(static::CODE_NOT_CAMEL_CASE)) { |
||
94 | $this->file->addError( |
||
95 | self::MESSAGE_NOT_CAMEL_CASE, |
||
96 | $this->stackPos, |
||
97 | static::CODE_NOT_CAMEL_CASE, |
||
98 | [ |
||
99 | $var |
||
100 | ] |
||
101 | ); |
||
102 | } |
||
103 | } else { |
||
104 | $this->file->recordMetric($this->stackPos, 'CamelCase var name', 'yes'); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sets up the sniff. |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | protected function setUp(): void |
||
114 | { |
||
115 | parent::setUp(); |
||
116 | |||
117 | $this->prevSniffedFile = $this->file->getFilename(); |
||
118 | |||
119 | if (!array_key_exists($var = $this->token['content'], $this->sniffedVars)) { |
||
120 | // "Sniff" the var name only once ... |
||
121 | $this->sniffedVars[$var] = Common::isCamelCaps(substr($var, 1)); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Cleans the cache of this sniff. |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | protected function tearDown(): void |
||
131 | { |
||
132 | parent::tearDown(); |
||
133 | |||
134 | if ($this->prevSniffedFile && $this->prevSniffedFile !== $this->file->getFilename()) { |
||
135 | $this->sniffedVars = []; |
||
136 | $this->prevSniffedFile = ''; |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 |