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

ForeachExpressionReferencingSniff   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 8.82 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 6
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 6 45 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\ControlStructures\ForeachExpressionReferencingSniff.
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\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
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
        $nestingLevel = 0;
80 View Code Duplication
        if ($asToken !== ($opener + 1) && isset($tokens[$opener + 1]['nested_parenthesis'])) {
81
            $nestingLevel = count($tokens[$opener + 1]['nested_parenthesis']);
82
        }
83
84
        if ($this->isVariable($phpcsFile, ($opener + 1), $asToken, $nestingLevel) === true) {
85
            return;
86
        }
87
88
        // Non-variable detected before the `as` keyword.
89
        $phpcsFile->addError(
90
            'Referencing $value is only possible if the iterated array is a variable in PHP 5.4 or earlier.',
91
            $hasReference,
92
            'Found'
93
        );
94
    }
95
}
96