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

RemovedGlobalVariablesSniff::process()   C

Complexity

Conditions 13
Paths 37

Size

Total Lines 41
Code Lines 27

Duplication

Lines 35
Ratio 85.37 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 1
b 0
f 0
nc 37
nop 2
dl 35
loc 41
rs 5.1234

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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