Failed Conditions
Push — test-scrutinizer-coverage ( 1fb662...215aeb )
by Juliette
03:50
created

NewGroupUseDeclarationsSniff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 57
ccs 14
cts 22
cp 0.6364
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 8 2
B process() 0 30 6
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
/**
13
 * PHPCompatibility_Sniffs_PHP_NewGroupUseDeclarationsSniff.
14
 *
15
 * PHP version 7.0
16
 *
17
 * @category PHP
18
 * @package  PHPCompatibility
19
 * @author   Wim Godden <[email protected]>
20
 */
21
class PHPCompatibility_Sniffs_PHP_NewGroupUseDeclarationsSniff 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...
22
{
23
    /**
24
     * Returns an array of tokens this test wants to listen for.
25
     *
26
     * @return array
27
     */
28 14
    public function register()
29
    {
30 14
        if (defined('T_OPEN_USE_GROUP')) {
31 14
            return array(T_OPEN_USE_GROUP);
32
        } else {
33
            return array(T_USE);
34
        }
35
    }//end register()
36
37
38
    /**
39
     * Processes this test, when one of its tokens is encountered.
40
     *
41
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
42
     * @param int                  $stackPtr  The position of the current token in
43
     *                                        the stack passed in $tokens.
44
     *
45
     * @return void
46
     */
47 2
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
48
    {
49 2
        if ($this->supportsBelow('5.6') === false) {
50 1
            return;
51
        }
52
53 1
        $tokens = $phpcsFile->getTokens();
54 1
        $token  = $tokens[$stackPtr];
55
56
        // Deal with PHPCS pre-2.6.0.
57 1
        if ($token['code'] === T_USE) {
58
            $hasCurlyBrace = $phpcsFile->findNext(T_OPEN_CURLY_BRACKET, ($stackPtr + 1), null, false, null, true);
59
            if ($hasCurlyBrace === false) {
60
                return;
61
            }
62
63
            $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($hasCurlyBrace - 1), null, true);
64
            if ($prevToken === false || $tokens[$prevToken]['code'] !== T_NS_SEPARATOR) {
65
                return;
66
            }
67
        }
68
69
        // Still here ? In that case, it is a group use statement.
70 1
        $phpcsFile->addError(
71 1
            'Group use declarations are not allowed in PHP 5.6 or earlier',
72 1
            $stackPtr,
73
            'Found'
74 1
        );
75
76 1
    }//end process()
77
}//end class
78