Completed
Pull Request — master (#401)
by Juliette
02:30
created

NewUseConstFunctionSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 73
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
C process() 0 38 7
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewUseConstFunctionSniff.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_NewUseConstFunctionSniff.
14
 *
15
 * The use operator has been extended to support importing functions and
16
 * constants in addition to classes. This is achieved via the use function
17
 * and use const constructs, respectively.
18
 *
19
 * PHP version 5.6
20
 *
21
 * @category PHP
22
 * @package  PHPCompatibility
23
 * @author   Juliette Reinders Folmer <[email protected]>
24
 */
25
class PHPCompatibility_Sniffs_PHP_NewUseConstFunctionSniff 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...
26
{
27
28
    /**
29
     * A list of keywords that can follow use statements.
30
     *
31
     * @var array(string => string)
32
     */
33
    protected $validUseNames = array(
34
        'const'    => true,
35
        'function' => true,
36
    );
37
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43
    public function register()
44
    {
45
        return array(T_USE);
46
    }
47
48
49
    /**
50
     * Processes this test, when one of its tokens is encountered.
51
     *
52
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
53
     * @param int                  $stackPtr  The position of the current token in the
54
     *                                        stack passed in $tokens.
55
     *
56
     * @return void
57
     */
58
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
59
    {
60
        if ($this->supportsBelow('5.5') !== true) {
61
            return;
62
        }
63
64
        $tokens = $phpcsFile->getTokens();
65
66
        $nextNonEmpty = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
67
        if ($nextNonEmpty === false) {
68
            // Live coding.
69
            return;
70
        }
71
72
        if (isset($this->validUseNames[strtolower($tokens[$nextNonEmpty]['content'])]) === false) {
73
            // Not a `use const` or `use function` statement.
74
            return;
75
        }
76
77
        // `use const` and `use function` have to be followed by the function/constant name.
78
        $functionOrConstName = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true);
79
        if ($functionOrConstName === false
80
            // Identifies as T_AS or T_STRING, this covers both.
81
            || ($tokens[$functionOrConstName]['content'] === 'as'
82
            || $tokens[$functionOrConstName]['code'] === T_COMMA)
83
        ) {
84
            // Live coding or incorrect use of reserved keyword, but that is
85
            // covered by the ForbiddenNames sniff.
86
            return;
87
        }
88
89
        // Still here ? In that case we have encountered a `use const` or `use function` statement.
90
        $phpcsFile->addError(
91
            'Importing functions and constants through a "use" statement is not supported in PHP 5.5 or lower.',
92
            $nextNonEmpty,
93
            'Found'
94
        );
95
    }
96
97
}
98