ValidVariableNameSniff::process()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 12
rs 9.9666
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
namespace Mediact\CodingStandard\MediactCommon\Sniffs\NamingConventions;
9
10
use PHP_CodeSniffer\Files\File;
11
use PHP_CodeSniffer\Sniffs\Sniff;
12
13
class ValidVariableNameSniff implements Sniff
14
{
15
    /** @var array  */
16
    public $allowedNames = [
17
        '_GET',
18
        '_POST',
19
        '_COOKIE',
20
        '_FILES',
21
        '_REQUEST',
22
        '_SERVER',
23
        '_SESSION'
24
    ];
25
26
    /**
27
     * Listen to variable name tokens.
28
     *
29
     * @return int[]
30
     */
31
    public function register()
32
    {
33
        return [T_VARIABLE];
34
    }
35
36
    /**
37
     * Check variable names to make sure no underscores are used.
38
     *
39
     * @param File $phpcsFile
40
     * @param int  $stackPtr
41
     *
42
     * @return void
43
     */
44
    public function process(File $phpcsFile, $stackPtr)
45
    {
46
        $tokens  = $phpcsFile->getTokens();
47
        $varName = ltrim($tokens[$stackPtr]['content'], '$');
48
49
        if (
50
            !in_array($varName, $this->allowedNames)
51
            && preg_match('/^_/', $varName)
52
        ) {
53
            $phpcsFile->addWarning(
54
                'Variable names may not start with an underscore',
55
                $stackPtr,
56
                'IllegalVariableNameUnderscore'
57
            );
58
        }
59
    }
60
}
61