Completed
Pull Request — master (#657)
by Juliette
03:22 queued 01:07
created

NewListInForeachSniff   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 3 31 5

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\Lists\NewListInForeachSniff.
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\Lists;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\Lists\NewListInForeachSniff.
18
 *
19
 * Detect unpacking nested arrays with list() in a foreach().
20
 *
21
 * PHP version 5.5
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Juliette Reinders Folmer <[email protected]>
26
 */
27
class NewListInForeachSniff 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_FOREACH);
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 View Code Duplication
        if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
58
            return;
59
        }
60
61
        $opener = $tokens[$stackPtr]['parenthesis_opener'];
62
        $closer = $tokens[$stackPtr]['parenthesis_closer'];
63
64
        $asToken = $phpcsFile->findNext(T_AS, ($opener + 1), $closer);
65
        if ($asToken === false) {
66
            return;
67
        }
68
69
        $hasList = $phpcsFile->findNext(array(T_LIST, T_OPEN_SHORT_ARRAY), ($asToken + 1), $closer);
70
        if ($hasList === false) {
71
            return;
72
        }
73
74
        $phpcsFile->addError(
75
            'Unpacking nested arrays with list() in a foreach is not supported in PHP 5.4 or earlier.',
76
            $hasList,
77
            'Found'
78
        );
79
    }
80
}
81