Completed
Pull Request — master (#664)
by Juliette
05:26 queued 02:03
created

EmptyNonVariableSniff::process()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30

Duplication

Lines 6
Ratio 20 %

Importance

Changes 0
Metric Value
dl 6
loc 30
rs 8.5066
c 0
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\EmptyNonVariableSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\EmptyNonVariableSniff.
18
 *
19
 * Verify that nothing but variables are passed to empty().
20
 *
21
 * PHP version 5.5
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class EmptyNonVariableSniff extends Sniff
28
{
29
30
    /**
31
     * Returns an array of tokens this test wants to listen for.
32
     *
33
     * @return array
34
     */
35
    public function register()
36
    {
37
        return array(T_EMPTY);
38
    }
39
40
    /**
41
     * Processes this test, when one of its tokens is encountered.
42
     *
43
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
44
     * @param int                   $stackPtr  The position of the current token in the
45
     *                                         stack passed in $tokens.
46
     *
47
     * @return void
48
     */
49
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
50
    {
51
        if ($this->supportsBelow('5.4') === false) {
52
            return;
53
        }
54
55
        $tokens = $phpcsFile->getTokens();
56
57
        $open = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true);
58 View Code Duplication
        if ($open === false || isset($tokens[$open]['parenthesis_closer']) === false) {
59
            return;
60
        }
61
62
        $close = $tokens[$open]['parenthesis_closer'];
63
64
        $nestingLevel = 0;
65 View Code Duplication
        if ($close !== ($open + 1) && isset($tokens[$open + 1]['nested_parenthesis'])) {
66
            $nestingLevel = count($tokens[$open + 1]['nested_parenthesis']);
67
        }
68
69
        if ($this->isVariable($phpcsFile, ($open + 1), $close, $nestingLevel) === true) {
70
            return;
71
        }
72
73
        $phpcsFile->addError(
74
            'Only variables can be passed to empty() prior to PHP 5.5.',
75
            $stackPtr,
76
            'Found'
77
        );
78
    }
79
}
80