Completed
Push — php-7.1/248-new-short-list-syn... ( 312df7 )
by Juliette
02:06
created

ShortListSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 27 4
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\Lists\ShortListSniff.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility\Lists
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\Lists;
13
14
use PHPCompatibility\Sniff;
15
use PHPCompatibility\PHPCSHelper;
16
17
/**
18
 * \PHPCompatibility\Sniffs\Lists\ShortListSniff.
19
 *
20
 * "The shorthand array syntax ([]) may now be used to destructure arrays for
21
 * assignments (including within foreach), as an alternative to the existing
22
 * list() syntax, which is still supported."
23
 *
24
 * PHP version 7.1
25
 *
26
 * @category PHP
27
 * @package  PHPCompatibility\Lists
28
 * @author   Juliette Reinders Folmer <[email protected]>
29
 */
30
class ShortListSniff extends Sniff
31
{
32
33
    /**
34
     * Returns an array of tokens this test wants to listen for.
35
     *
36
     * @return array
37
     */
38
    public function register()
39
    {
40
        return array(T_OPEN_SHORT_ARRAY);
41
    }
42
43
    /**
44
     * Processes this test, when one of its tokens is encountered.
45
     *
46
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
47
     * @param int                   $stackPtr  The position of the current token in the
48
     *                                         stack passed in $tokens.
49
     *
50
     * @return void
51
     */
52
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53
    {
54
        if ($this->supportsBelow('7.0') === false) {
55
            return;
56
        }
57
58
        if ($this->isShortList($phpcsFile, $stackPtr) === false) {
59
            return;
60
        }
61
62
        $tokens = $phpcsFile->getTokens();
63
        $closer = $tokens[$stackPtr]['bracket_closer'];
64
65
        $hasVariable = $phpcsFile->findNext(T_VARIABLE, ($stackPtr + 1), $closer);
66
        if ($hasVariable === false) {
67
            // List syntax is only valid if there are variables in it.
68
            return;
69
        }
70
71
        $phpcsFile->addError(
72
            'The shorthand list syntax "[]" to destructure arrays is not available in PHP 7.0 or earlier.',
73
            $stackPtr,
74
            'Found'
75
        );
76
77
        return ($closer + 1);
78
    }
79
}
80