Completed
Push — master ( ae5de1...069994 )
by Wim
07:17 queued 03:06
created

NewConstVisibilitySniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 0 22 5
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewConstVisibility.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_NewConstVisibility.
14
 *
15
 * Visibility for class constants is available since PHP 7.1.
16
 *
17
 * PHP version 7.1
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Juliette Reinders Folmer <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_NewConstVisibilitySniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    /**
26
     * Returns an array of tokens this test wants to listen for.
27
     *
28
     * @return array
29
     */
30
    public function register()
31
    {
32
        return array(T_CONST);
33
34
    }//end register()
35
36
    /**
37
     * Processes this test, when one of its tokens is encountered.
38
     *
39
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
40
     * @param int                  $stackPtr  The position of the current token
41
     *                                        in the stack passed in $tokens.
42
     *
43
     * @return void
44
     */
45
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
46
    {
47
        $tokens    = $phpcsFile->getTokens();
48
        $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
49
50
        if ($prevToken === false) {
51
            return;
52
        }
53
54
        // Is the previous token a visibility indicator ?
55
        if (in_array($tokens[$prevToken]['code'], PHP_CodeSniffer_Tokens::$scopeModifiers, true) === false) {
56
            return;
57
        }
58
59
        if ($this->tokenHasScope($phpcsFile, $stackPtr, array(T_CLASS, T_INTERFACE)) === true && $this->supportsBelow('7.0') === true) {
60
            $error = 'Visibility indicators for class constants are not supported in PHP 7.0 or earlier. Found "%s const"';
61
            $data  = array($tokens[$prevToken]['content']);
62
            $phpcsFile->addError($error, $stackPtr, 'Found', $data);
63
64
        }
65
66
    }//end process()
67
68
}//end class
69