Completed
Push — master ( 8571e4...1f0247 )
by Juliette
9s
created

ConstantArraysUsingDefineSniff   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
c 4
b 1
f 0
lcom 0
cbo 2
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
B process() 0 36 6
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
/**
13
 * PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff.
14
 *
15
 * Constant arrays using define in PHP 7.0
16
 *
17
 * PHP version 7.0
18
 *
19
 * @category  PHP
20
 * @package   PHPCompatibility
21
 * @author    Wim Godden <[email protected]>
22
 */
23
class PHPCompatibility_Sniffs_PHP_ConstantArraysUsingDefineSniff extends PHPCompatibility_Sniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
26
    /**
27
     * Returns an array of tokens this test wants to listen for.
28
     *
29
     * @return array
30
     */
31
    public function register()
32
    {
33
        return array(T_STRING);
34
    }
35
36
    /**
37
     * Processes this test, when one of its tokens is encountered.
38
     *
39
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
40
     * @param int                  $stackPtr  The position of the current token in the
41
     *                                        stack passed in $tokens.
42
     *
43
     * @return void
44
     */
45
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
46
    {
47
        if ($this->supportsBelow('5.6') !== true) {
48
            return;
49
        }
50
51
        $tokens = $phpcsFile->getTokens();
52
53
        $ignore = array(
54
            T_DOUBLE_COLON,
55
            T_OBJECT_OPERATOR,
56
            T_FUNCTION,
57
            T_CONST,
58
        );
59
60
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
61
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
62
            // Not a call to a PHP function.
63
            return;
64
        }
65
66
        $function = strtolower($tokens[$stackPtr]['content']);
67
        if ($function !== 'define') {
68
            return;
69
        }
70
71
        $secondParam = $this->getFunctionCallParameter($phpcsFile, $stackPtr, 2);
72
        if (isset($secondParam['start'], $secondParam['end']) === false) {
73
            return;
74
        }
75
76
        $array = $phpcsFile->findNext(array(T_ARRAY, T_OPEN_SHORT_ARRAY), $secondParam['start'], ($secondParam['end'] + 1));
77
        if ($array !== false) {
78
            $phpcsFile->addError('Constant arrays using define are not allowed in PHP 5.6 or earlier', $array);
79
        }
80
    }
81
}
82