Completed
Push — php70 ( 958375...6836d5 )
by Wim
02:59
created

RemovedFunctionParametersSniff::addError()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 25
Code Lines 14

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 14
c 1
b 0
f 1
nc 12
nop 4
dl 25
loc 25
rs 6.7272
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff.
12
 *
13
 * @category  PHP
14
 * @package   PHPCompatibility
15
 * @author    Wim Godden <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_RemovedFunctionParametersSniff extends PHPCompatibility_Sniff
18
{
19
20
    /**
21
     * If true, forbidden functions will be considered regular expressions.
22
     *
23
     * @var bool
24
     */
25
    protected $patternMatch = false;
26
    
27
    /**
28
     * A list of Removed function parameters, not present in older versions.
29
     *
30
     * The array lists : version number with false (not present) or true (present).
31
     * The index is the location of the parameter in the parameter list, starting at 0 !
32
     * If's sufficient to list the first version where the function appears.
33
     *
34
     * @var array
35
     */
36
    public $removedFunctionParameters = array(
37
                                        'mktime' => array(
38
                                            6 => array(
39
                                                'name' => 'is_dst',
40
                                                '5.1' => true,
41
                                                '7.0' => false
42
                                            ),
43
                                        ),
44
                                        'gmmktime' => array(
45
                                            6 => array(
46
                                                'name' => 'is_dst',
47
                                                '7.0' => false
48
                                            ),
49
                                        ),
50
                                    );
51
52
53
    /**
54
     * If true, an error will be thrown; otherwise a warning.
55
     *
56
     * @var bool
57
     */
58
    public $error = false;
59
    
60
    /**
61
     * 
62
     * @var array
63
     */
64
    private $removedFunctionParametersNames;
65
66
67
    /**
68
     * Returns an array of tokens this test wants to listen for.
69
     *
70
     * @return array
71
     */
72
    public function register()
73
    {
74
        // Everyone has had a chance to figure out what forbidden functions
75
        // they want to check for, so now we can cache out the list.
76
        $this->removedFunctionParametersNames = array_keys($this->removedFunctionParameters);
77
    
78
        if ($this->patternMatch === true) {
79
            foreach ($this->removedFunctionParametersNames as $i => $name) {
80
                $this->removedFunctionParametersNames[$i] = '/'.$name.'/i';
81
            }
82
        }
83
    
84
        return array(T_STRING);
85
    }//end register()
86
    
87
    /**
88
     * Processes this test, when one of its tokens is encountered.
89
     *
90
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
91
     * @param int                  $stackPtr  The position of the current token in
92
     *                                        the stack passed in $tokens.
93
     *
94
     * @return void
95
     */
96 View Code Duplication
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
97
    {
98
        $tokens = $phpcsFile->getTokens();
99
100
        $ignore = array(
101
                T_DOUBLE_COLON,
102
                T_OBJECT_OPERATOR,
103
                T_FUNCTION,
104
                T_CONST,
105
        );
106
107
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
108
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
109
            // Not a call to a PHP function.
110
            return;
111
        }
112
113
        $function = strtolower($tokens[$stackPtr]['content']);
114
115
        if (in_array($function, $this->removedFunctionParametersNames) === false) {
116
            return;
117
        }
118
        
119
        if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['type'] == 'T_OPEN_PARENTHESIS') {
120
            $closeParenthesis = $tokens[$stackPtr + 1]['parenthesis_closer'];
121
122
            $nextComma = $stackPtr + 1;
123
            $cnt = 0;
124
            while ($nextComma = $phpcsFile->findNext(array(T_COMMA, T_CLOSE_PARENTHESIS), $nextComma + 1, $closeParenthesis + 1)) {
125
                if (isset($this->removedFunctionParameters[$function][$cnt])) {
126
                    $this->addError($phpcsFile, $nextComma, $function, $cnt);
127
                }
128
                $cnt++;
129
            }
130
            
131
        }
132
    }//end process()
133
134
135
    /**
136
     * Generates the error or wanrning for this sniff.
137
     *
138
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
139
     * @param int                  $stackPtr  The position of the function
140
     *                                        in the token array.
141
     * @param string               $function  The name of the function.
142
     * @param string               $pattern   The pattern used for the match.
0 ignored issues
show
Bug introduced by
There is no parameter named $pattern. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
143
     *
144
     * @return void
145
     */
146 View Code Duplication
    protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
147
    {
148
        $error = '';
149
150
        $this->error = false;
151
        foreach ($this->removedFunctionParameters[$function][$parameterLocation] as $version => $present) {
152
            if ($version != 'name' && $this->supportsAbove($version)) {
153
                if ($present === false) {
154
                    $this->error = true;
155
                    $error .= 'in PHP version ' . $version . ' or later';
156
                }
157
            }
158
        }
159
        
160
        if (strlen($error) > 0) {
161
            $error = 'The function ' . $function . ' does not have a parameter ' . $this->removedFunctionParameters[$function][$parameterLocation]['name'] . ' ' . $error;
162
163
            if ($this->error === true) {
164
                $phpcsFile->addError($error, $stackPtr);
165
            } else {
166
                $phpcsFile->addWarning($error, $stackPtr);
167
            }
168
        }
169
170
    }//end addError()
171
172
}//end class
173