-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathValidVariableNameSniff.php
More file actions
79 lines (66 loc) · 2.21 KB
/
ValidVariableNameSniff.php
File metadata and controls
79 lines (66 loc) · 2.21 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types = 1);
namespace Consistence\Sniffs\NamingConventions;
use PHP_CodeSniffer\Files\File as PhpCsFile;
use PHP_CodeSniffer\Util\Common as PhpCsUtil;
class ValidVariableNameSniff extends \PHP_CodeSniffer\Sniffs\AbstractVariableSniff
{
public const CODE_CAMEL_CAPS = 'NotCamelCaps';
/** @var string[] */
private static array $phpReservedVariables = [
'_SERVER',
'_GET',
'_POST',
'_REQUEST',
'_SESSION',
'_ENV',
'_COOKIE',
'_FILES',
'GLOBALS',
];
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processVariable(PhpCsFile $file, $stackPointer): void
{
$tokens = $file->getTokens();
$varName = ltrim($tokens[$stackPointer]['content'], '$');
if (in_array($varName, self::$phpReservedVariables, true)) {
return; // skip PHP reserved vars
}
$objOperator = $file->findPrevious([T_WHITESPACE], ($stackPointer - 1), null, true);
if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
return; // skip MyClass::$variable, there might be no control over the declaration
}
if (!PhpCsUtil::isCamelCaps($varName, false, true, false)) {
$error = 'Variable "%s" is not in valid camel caps format';
$data = [$varName];
$file->addError($error, $stackPointer, self::CODE_CAMEL_CAPS, $data);
}
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @codeCoverageIgnore
*
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processMemberVar(PhpCsFile $file, $stackPointer): void
{
// handled by PSR2.Classes.PropertyDeclaration
}
/**
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @codeCoverageIgnore
*
* @param \PHP_CodeSniffer\Files\File $file
* @param int $stackPointer position of the double quoted string
*/
protected function processVariableInString(PhpCsFile $file, $stackPointer): void
{
// Consistence standard does not allow variables in strings, handled by Squiz.Strings.DoubleQuoteUsage
}
}