Completed
Push — master ( a532a0...eea2a5 )
by Wim
02:34
created

NewFunctionArrayDereferencingSniff::process()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 15
c 1
b 1
f 0
nc 5
nop 2
dl 0
loc 26
rs 8.439
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff.
12
 *
13
 * @category  PHP
14
 * @package   PHPCompatibility
15
 * @author    Wim Godden <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_NewFunctionArrayDereferencingSniff extends PHPCompatibility_Sniff
18
{
19
    /**
20
     * Returns an array of tokens this test wants to listen for.
21
     *
22
     * @return array
23
     */
24
    public function register()
25
    {
26
        return array(T_STRING);
27
    }//end register()
28
    
29
    /**
30
     * Processes this test, when one of its tokens is encountered.
31
     *
32
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
33
     * @param int                  $stackPtr  The position of the current token in
34
     *                                        the stack passed in $tokens.
35
     *
36
     * @return void
37
     */
38
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
39
    {
40
        $tokens = $phpcsFile->getTokens();
41
42
        $ignore = array(
43
                T_DOUBLE_COLON,
44
                T_OBJECT_OPERATOR,
45
                T_FUNCTION,
46
                T_CONST,
47
        );
48
49
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
50
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
51
            // Not a call to a PHP function.
52
            return;
53
        }
54
        
55
        if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['type'] == 'T_OPEN_PARENTHESIS') {
56
            $closeParenthesis = $tokens[$stackPtr + 1]['parenthesis_closer'];
57
            if ($tokens[$closeParenthesis + 1]['type'] == 'T_OPEN_SQUARE_BRACKET') {
58
                if ($this->supportsBelow('5.3')) {
59
                    $phpcsFile->addError('Function array dereferencing is not present in PHP version 5.3 or earlier', $stackPtr + 3);
60
                }
61
            }
62
        }
63
    }//end process()
64
}//end class
65