ValidVariableNameSniff   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 44
ccs 0
cts 10
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 13 3
A register() 0 3 1
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