-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLowerCamelCaseVariable.php
41 lines (33 loc) · 1.31 KB
/
LowerCamelCaseVariable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace Rareloop\Twigcs\Rule;
use FriendsOfTwig\Twigcs\Lexer;
use FriendsOfTwig\Twigcs\TwigPort\Token;
use FriendsOfTwig\Twigcs\Rule\AbstractRule;
use FriendsOfTwig\Twigcs\Rule\RuleInterface;
use FriendsOfTwig\Twigcs\TwigPort\TokenStream;
class LowerCamelCaseVariable extends AbstractRule implements RuleInterface
{
public function check(TokenStream $tokens)
{
$violations = [];
while (!$tokens->isEOF()) {
$token = $tokens->getCurrent();
if ($token->getType() === Token::NAME_TYPE && $this->isNotLowerCamelCase($token->getValue())) {
if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') {
$violations[] = $this->createViolation(
$tokens->getSourceContext()->getPath(),
$token->getLine(),
$token->columnno,
sprintf('The "%s" variable should be in lowerCamelCase.', $token->getValue())
);
}
}
$tokens->next();
}
return $violations;
}
private function isNotLowerCamelCase(string $string): bool
{
return !preg_match('/^[a-z]+((\d)|([A-Z0-9][a-z0-9]+))*([A-Z])?$/', $string);
}
}