Completed
Push — php-5.5/new-foreach-referenced... ( 6ee1d3 )
by Juliette
11:54 queued 09:54
created

ForeachExpressionReferencingSniff::process()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 21

Duplication

Lines 3
Ratio 7.32 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 41
rs 8.439
cc 6
eloc 21
nc 6
nop 2
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\ControlStructures\ForeachExpressionReferencingSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility\ControlStructures
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\ControlStructures;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\ControlStructures\ForeachExpressionReferencingSniff.
18
 *
19
 * Before PHP 5.5.0, referencing $value is only possible if the iterated array
20
 * can be referenced (i.e. if it is a variable).
21
 *
22
 * PHP version 5.5
23
 *
24
 * @category PHP
25
 * @package  PHPCompatibility\ControlStructures
26
 * @author   Juliette Reinders Folmer <[email protected]>
27
 */
28
class ForeachExpressionReferencingSniff extends Sniff
29
{
30
31
    /**
32
     * Returns an array of tokens this test wants to listen for.
33
     *
34
     * @return array
35
     */
36
    public function register()
37
    {
38
        return array(T_FOREACH);
39
    }
40
41
    /**
42
     * Processes this test, when one of its tokens is encountered.
43
     *
44
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
45
     * @param int                   $stackPtr  The position of the current token in the
46
     *                                         stack passed in $tokens.
47
     *
48
     * @return void
49
     */
50
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
51
    {
52
        if ($this->supportsBelow('5.4') === false) {
53
            return;
54
        }
55
56
        $tokens = $phpcsFile->getTokens();
57
58 View Code Duplication
        if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
59
            return;
60
        }
61
62
        $opener = $tokens[$stackPtr]['parenthesis_opener'];
63
        $closer = $tokens[$stackPtr]['parenthesis_closer'];
64
65
        $asToken = $phpcsFile->findNext(T_AS, ($opener + 1), $closer);
66
        if ($asToken === false) {
67
            return;
68
        }
69
70
        /*
71
         * Note: referencing $key is not allowed in any version, so this should only find referenced $values.
72
         * If it does find a referenced key, it would be a parse error anyway.
73
         */
74
        $hasReference = $phpcsFile->findNext(T_BITWISE_AND, ($asToken + 1), $closer);
75
        if ($hasReference === false) {
76
            return;
77
        }
78
79
        $ignore   = \PHP_CodeSniffer_Tokens::$emptyTokens;
80
        $ignore[] = T_VARIABLE;
81
82
        if ($phpcsFile->findNext($ignore, ($opener + 1), $asToken, true) !== false) {
83
            // Non-variable detected before the `as` keyword.
84
            $phpcsFile->addError(
85
                'Referencing $value is only possible if the iterated array is a variable in PHP 5.4 or earlier.',
86
                $hasReference,
87
                'Found'
88
            );
89
        }
90
    }
91
}
92