Completed
Push — master ( 515011...5ea7bb )
by Wim
05:08 queued 02:12
created

PHPCompatibility_Sniffs_PHP_LongArraysSniff   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 5.43 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
C process() 5 55 10

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_PHP_LongArraysSniff.
4
 *
5
 * PHP version 5.3
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Ben Selby <[email protected]>
10
 * @copyright 2012 Ben Selby
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_LongArraysSniff.
15
 *
16
 * Marks the use of HTTP_*_VARS as deprecated
17
 *
18
 * PHP version 5.3
19
 *
20
 * @category  PHP
21
 * @package   PHPCompatibility
22
 * @author    Ben Selby <[email protected]>
23
 * @copyright 2012 Ben Selby
24
 */
25
class PHPCompatibility_Sniffs_PHP_LongArraysSniff 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...
26
{
27
    /**
28
     * Array of HTTP_*_VARS that are now deprecated
29
     *
30
     * @var array
31
     */
32
    protected $deprecated = array(
33
        'HTTP_POST_VARS',
34
        'HTTP_GET_VARS',
35
        'HTTP_ENV_VARS',
36
        'HTTP_SERVER_VARS',
37
        'HTTP_COOKIE_VARS',
38
        'HTTP_SESSION_VARS',
39
        'HTTP_POST_FILES'
40
    );
41
42
    /**
43
     * Returns an array of tokens this test wants to listen for.
44
     *
45
     * @return array
46
     */
47
    public function register()
48
    {
49
        return array(T_VARIABLE);
50
    }
51
52
    /**
53
     * Processes this test, when one of its tokens is encountered.
54
     *
55
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
56
     * @param int                  $stackPtr  The position of the current token in the
57
     *                                        stack passed in $tokens.
58
     *
59
     * @return void
60
     */
61
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
62
    {
63
        if ($this->supportsAbove('5.3') === false) {
64
            return;
65
        }
66
67
        $tokens = $phpcsFile->getTokens();
68
69
        // Check if the variable name is in our blacklist.
70
        if (in_array(substr($tokens[$stackPtr]['content'], 1), $this->deprecated, true) === false) {
71
            return;
72
        }
73
74
        if ($this->inClassScope($phpcsFile, $stackPtr, false) === true) {
75
            /*
76
             * Check for class property definitions.
77
             */
78
            $properties = array();
79
            try {
80
                $properties = $phpcsFile->getMemberProperties($stackPtr);
81
            } catch ( PHP_CodeSniffer_Exception $e) {
82
                // If it's not an expected exception, throw it.
83
                if ($e->getMessage() !== '$stackPtr is not a class member var') {
84
                    throw $e;
85
                }
86
            }
87
88
            if (isset($properties['scope'])) {
89
                // Ok, so this was a class property declaration, not our concern.
90
                return;
91
            }
92
93
            /*
94
             * Check for static usage of class properties shadowing the long arrays.
95
             */
96
            $prevToken = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true);
97
            if ($tokens[$prevToken]['code'] === T_DOUBLE_COLON) {
98
                return;
99
            }
100
        }
101
102
        // Still here, so throw an error/warning.
103
        $error   = "The use of long predefined variables has been deprecated in 5.3%s; Found '%s'";
104
        $isError = $this->supportsAbove('5.4');
105
        $data    = array(
106
            ($isError ? ' and removed in 5.4' : ''),
107
            $tokens[$stackPtr]['content']
108
        );
109
110 View Code Duplication
        if ($isError === true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            $phpcsFile->addError($error, $stackPtr, 'Found', $data);
112
        } else {
113
            $phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
114
        }
115
    }
116
}
117