Completed
Pull Request — master (#110)
by Wim
03:06
created

RemovedGlobalVariablesSniff   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 43.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 35
loc 81
rs 10
wmc 14
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
C process() 35 41 13

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_RemovedGlobalVariablesSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff.
12
 *
13
 * Discourages the use of removed global variables. Suggests alternative extensions if available
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Wim Godden <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_RemovedGlobalVariablesSniff extends PHPCompatibility_Sniff
20
{
21
22
    /**
23
     * A list of removed global variables with their alternative, if any
24
     * Array codes : 0 = removed/unavailable, -1 = deprecated, 1 = active
25
     *
26
     * @var array(string|null)
27
     */
28
    protected $removedGlobalVariables = array(
29
        'HTTP_RAW_POST_DATA' => array(
30
            '5.5' => 1,
31
            '5.6' => -1,
32
            '7.0' => 0,
33
            'alternative' => 'php://input'
34
        ),
35
    );
36
37
    /**
38
     * Returns an array of tokens this test wants to listen for.
39
     *
40
     * @return array
41
     */
42
    public function register()
43
    {
44
        return array(T_VARIABLE);
45
46
    }//end register()
47
48
    /**
49
     * Processes this test, when one of its tokens is encountered.
50
     *
51
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
52
     * @param int                  $stackPtr  The position of the current token in the
53
     *                                        stack passed in $tokens.
54
     *
55
     * @return void
56
     */
57
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
58
    {
59
        $tokens = $phpcsFile->getTokens();
60
        
61 View Code Duplication
        foreach ($this->removedGlobalVariables as $variable => $versionList) {
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...
62
            if (strtolower($tokens[$stackPtr]['content']) == '$' . strtolower($variable)) {
63
                $error = '';
64
                $isErrored = false;
65
                foreach ($versionList as $version => $status) {
66
                    if ($version != 'alternative') {
67
                        if ($status == -1 || $status == 0) {
68
                            if ($this->supportsAbove($version)) {
69
                                switch ($status) {
70
                                    case -1:
71
                                        $error .= 'deprecated since PHP ' . $version . ' and ';
72
                                        break;
73
                                    case 0:
74
                                        $isErrored = true;
75
                                        $error .= 'removed since PHP ' . $version . ' and ';
76
                                        break 2;
77
                                }
78
                            }
79
                        }
80
                    }
81
                }
82
                if (strlen($error) > 0) {
83
                    $error = "Global variable '" . $variable . "' is " . $error;
84
                    $error = substr($error, 0, strlen($error) - 5);
85
                    if (!is_null($versionList['alternative'])) {
86
                        $error .= ' - use ' . $versionList['alternative'] . ' instead.';
87
                    }
88
                    if ($isErrored === true) {
89
                        $phpcsFile->addError($error, $stackPtr);
90
                    } else {
91
                        $phpcsFile->addWarning($error, $stackPtr);
92
                    }
93
                }
94
            }
95
        }
96
97
    }//end process()
98
99
}//end class
100