Completed
Pull Request — master (#228)
by Juliette
03:19
created

NewFunctionArrayDereferencingSniff::process()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 5
nop 2
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
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...
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
        if ($this->supportsBelow('5.3') === false) {
41
            return;
42
        }
43
44
        $tokens = $phpcsFile->getTokens();
45
46
        $ignore = array(
47
                T_DOUBLE_COLON,
48
                T_OBJECT_OPERATOR,
49
                T_FUNCTION,
50
                T_CONST,
51
        );
52
53
        $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
54
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
55
            // Not a call to a PHP function.
56
            return;
57
        }
58
59
        $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
60
        if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) {
61
            return;
62
        }
63
64
        $closeParenthesis = $tokens[$open]['parenthesis_closer'];
65
        $nextNonEmpty     = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($closeParenthesis + 1), null, true, null, true);
66
        if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET') {
67
            $phpcsFile->addError('Function array dereferencing is not present in PHP version 5.3 or earlier', $nextNonEmpty);
68
        }
69
70
    }//end process()
71
}//end class
72