Test Failed
Pull Request — master (#2)
by Ashoka
03:06
created

ValidVariableNameSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 43
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 3
A register() 0 3 1
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\CodingStandard\MediactCommon\Sniffs\NamingConventions;
8
9
use PHP_CodeSniffer\Files\File;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Files\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PHP_CodeSniffer\Sniffs\Sniff;
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer\Sniffs\Sniff was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class ValidVariableNameSniff implements Sniff
13
{
14
    /** @var array  */
15
    public $allowedNames = [
16
        '_GET',
17
        '_POST',
18
        '_COOKIE',
19
        '_FILES',
20
        '_REQUEST',
21
        '_SERVER',
22
        '_SESSION'
23
    ];
24
25
    /**
26
     * Listen to variable name tokens.
27
     *
28
     * @return int[]
29
     */
30
    public function register()
31
    {
32
        return [T_VARIABLE];
33
    }
34
35
    /**
36
     * Check variable names to make sure no underscores are used.
37
     *
38
     * @param File $phpcsFile
39
     * @param int  $stackPtr
40
     *
41
     * @return void
42
     */
43
    public function process(File $phpcsFile, $stackPtr)
44
    {
45
        $tokens  = $phpcsFile->getTokens();
46
        $varName = ltrim($tokens[$stackPtr]['content'], '$');
47
48
        if (!in_array($varName, $this->allowedNames)
49
            && preg_match('/^_/', $varName)
50
        ) {
51
            $phpcsFile->addWarning(
52
                'Variable names may not start with an underscore',
53
                $stackPtr,
54
                'IllegalVariableNameUnderscore'
55
            );
56
        }
57
    }
58
}
59