Completed
Push — php7.2/group-use-trailing-comm... ( 1d35f1 )
by Juliette
04:40 queued 01:10
created

NewGroupUseDeclarationsSniff::process()   C

Complexity

Conditions 9
Paths 15

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.2941
c 0
b 0
f 0
cc 9
eloc 27
nc 15
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\NewGroupUseDeclarationsSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Wim Godden <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\NewGroupUseDeclarationsSniff.
18
 *
19
 * PHP version 7.0
20
 *
21
 * @category PHP
22
 * @package  PHPCompatibility
23
 * @author   Wim Godden <[email protected]>
24
 */
25
class NewGroupUseDeclarationsSniff extends Sniff
26
{
27
    /**
28
     * Returns an array of tokens this test wants to listen for.
29
     *
30
     * @return array
31
     */
32
    public function register()
33
    {
34
        if (defined('T_OPEN_USE_GROUP')) {
35
            return array(T_OPEN_USE_GROUP);
36
        } else {
37
            return array(T_USE);
38
        }
39
    }//end register()
40
41
42
    /**
43
     * Processes this test, when one of its tokens is encountered.
44
     *
45
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
46
     * @param int                   $stackPtr  The position of the current token in
47
     *                                         the stack passed in $tokens.
48
     *
49
     * @return void
50
     */
51
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
52
    {
53
        if ($this->supportsBelow('7.1') === false) {
54
            return;
55
        }
56
57
        $tokens = $phpcsFile->getTokens();
58
        $token  = $tokens[$stackPtr];
59
60
        // Deal with PHPCS pre-2.6.0.
61
        if ($token['code'] === T_USE) {
62
            $hasCurlyBrace = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), null, false, null, true);
63
            if ($hasCurlyBrace === false) {
64
                return;
65
            }
66
67
            $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($hasCurlyBrace - 1), null, true);
68
            if ($prevToken === false || $tokens[$prevToken]['code'] !== T_NS_SEPARATOR) {
69
                return;
70
            }
71
72
            $stackPtr = $hasCurlyBrace;
73
        }
74
75
        // Still here ? In that case, it is a group use statement.
76
        if ($this->supportsBelow('5.6') === true) {
77
            $phpcsFile->addError(
78
                'Group use declarations are not allowed in PHP 5.6 or earlier',
79
                $stackPtr,
80
                'Found'
81
            );
82
        }
83
84
        $closeCurly = $phpcsFile->findNext(array(T_CLOSE_USE_GROUP, T_CLOSE_CURLY_BRACKET), ($stackPtr + 1), null, false, null, true);
85
        if ($closeCurly === false) {
86
            return;
87
        }
88
89
        $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($closeCurly - 1), null, true);
90
        if ($tokens[$prevToken]['code'] === T_COMMA) {
91
            $phpcsFile->addError(
92
                'Trailing comma\'s are not allowed in group use statements in PHP 7.1 or earlier',
93
                $prevToken,
94
                'TrailingCommaFound'
95
            );
96
        }
97
    }//end process()
98
99
}//end class
100