NewFunctionArrayDereferencingSniff   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 70
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 48 10
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\NewFunctionArrayDereferencingSniff.
4
 *
5
 * PHP version 5.4
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\NewFunctionArrayDereferencingSniff.
18
 *
19
 * PHP version 5.4
20
 *
21
 * @category PHP
22
 * @package  PHPCompatibility
23
 * @author   Wim Godden <[email protected]>
24
 */
25
class NewFunctionArrayDereferencingSniff 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
        return array(T_STRING);
35
    }//end register()
36
37
    /**
38
     * Processes this test, when one of its tokens is encountered.
39
     *
40
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
41
     * @param int                   $stackPtr  The position of the current token in
42
     *                                         the stack passed in $tokens.
43
     *
44
     * @return void
45
     */
46
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
47
    {
48
        if ($this->supportsBelow('5.3') === false) {
49
            return;
50
        }
51
52
        $tokens = $phpcsFile->getTokens();
53
54
        // Next non-empty token should be the open parenthesis.
55
        $openParenthesis = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true);
56
        if ($openParenthesis === false || $tokens[$openParenthesis]['code'] !== T_OPEN_PARENTHESIS) {
57
            return;
58
        }
59
60
        // Don't throw errors during live coding.
61
        if (isset($tokens[$openParenthesis]['parenthesis_closer']) === false) {
62
            return;
63
        }
64
65
        // Is this T_STRING really a function or method call ?
66
        $prevToken = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
67
        if ($prevToken !== false && in_array($tokens[$prevToken]['code'], array(T_DOUBLE_COLON, T_OBJECT_OPERATOR), true) === false) {
68
            $ignore = array(
69
                T_FUNCTION,
70
                T_CONST,
71
                T_USE,
72
                T_NEW,
73
                T_CLASS,
74
                T_INTERFACE,
75
            );
76
77
            if (in_array($tokens[$prevToken]['code'], $ignore, true) === true) {
78
                // Not a call to a PHP function or method.
79
                return;
80
            }
81
        }
82
83
        $closeParenthesis = $tokens[$openParenthesis]['parenthesis_closer'];
84
        $nextNonEmpty     = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($closeParenthesis + 1), null, true, null, true);
85
        if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['type'] === 'T_OPEN_SQUARE_BRACKET') {
86
            $phpcsFile->addError(
87
                'Function array dereferencing is not present in PHP version 5.3 or earlier',
88
                $nextNonEmpty,
89
                'Found'
90
            );
91
        }
92
93
    }//end process()
94
}//end class
95