Failed Conditions
Push — feature/scrutinizer-send-pass-... ( 5c2dd9 )
by Juliette
02:12
created

ConstantArraysUsingDefineSniff   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D process() 0 43 10
A register() 0 3 1
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\PHP\ConstantArraysUsingDefineSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Wim Godden <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\PHP;
13
14
use PHPCompatibility\Sniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\PHP\ConstantArraysUsingDefineSniff.
18
 *
19
 * Constant arrays using define in PHP 7.0
20
 *
21
 * PHP version 7.0
22
 *
23
 * @category PHP
24
 * @package  PHPCompatibility
25
 * @author   Wim Godden <[email protected]>
26
 */
27
class ConstantArraysUsingDefineSniff 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_STRING);
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)
0 ignored issues
show
Bug introduced by
The type PHP_CodeSniffer_File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
    {
51
        if ($this->supportsBelow('5.6') !== true) {
52
            return;
53
        }
54
55
        $tokens = $phpcsFile->getTokens();
56
57
        $ignore = array(
58
            T_DOUBLE_COLON,
59
            T_OBJECT_OPERATOR,
60
            T_FUNCTION,
61
            T_CONST,
62
        );
63
64
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
65
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
66
            // Not a call to a PHP function.
67
            return;
68
        }
69
70
        $functionLc = strtolower($tokens[$stackPtr]['content']);
71
        if ($functionLc !== 'define') {
72
            return;
73
        }
74
75
        $secondParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 2);
76
        if (isset($secondParam['start'], $secondParam['end']) === false) {
77
            return;
78
        }
79
80
        $targetNestingLevel = 0;
81
        if (isset($tokens[$secondParam['start']]['nested_parenthesis'])) {
82
            $targetNestingLevel = count($tokens[$secondParam['start']]['nested_parenthesis']);
83
        }
84
85
        $array = $phpcsFile->findNext(array(T_ARRAY, T_OPEN_SHORT_ARRAY), $secondParam['start'], ($secondParam['end'] + 1));
86
        if ($array !== false) {
87
            if ((isset($tokens[$array]['nested_parenthesis']) === false && $targetNestingLevel === 0) || count($tokens[$array]['nested_parenthesis']) === $targetNestingLevel) {
88
                $phpcsFile->addError(
89
                    'Constant arrays using define are not allowed in PHP 5.6 or earlier',
90
                    $array,
91
                    'Found'
92
                );
93
            }
94
        }
95
    }
96
}
97