Completed
Push — master ( e4aaf3...60edd7 )
by Wim
05:51 queued 03:09
created

NewFunctionParametersSniff::process()   D

Complexity

Conditions 9
Paths 7

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 9
eloc 23
c 2
b 1
f 1
nc 7
nop 2
dl 0
loc 40
rs 4.909
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewFunctionParametersSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 */
9
10
/**
11
 * PHPCompatibility_Sniffs_PHP_newFunctionParametersSniff.
12
 *
13
 * @category  PHP
14
 * @package   PHPCompatibility
15
 * @author    Wim Godden <[email protected]>
16
 */
17
class PHPCompatibility_Sniffs_PHP_NewFunctionParametersSniff 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 new functions, 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 $newFunctionParameters = array(
37
                                        'dirname' => array(
38
                                            1 => array(
39
                                                'name' => 'depth',
40
                                                '5.6' => false,
41
                                                '7.0' => true
42
                                            ),
43
                                        ),
44
                                        'unserialize' => array(
45
                                            1 => array(
46
                                                'name' => 'options',
47
                                                '5.6' => false,
48
                                                '7.0' => true
49
                                            ),
50
                                        ),
51
                                        'session_start' => array(
52
                                            0 => array(
53
                                                'name' => 'options',
54
                                                '5.6' => false,
55
                                                '7.0' => true
56
                                            )
57
                                        )
58
                                    );
59
60
61
    /**
62
     * If true, an error will be thrown; otherwise a warning.
63
     *
64
     * @var bool
65
     */
66
    public $error = false;
67
    
68
    /**
69
     * 
70
     * @var array
71
     */
72
    private $newFunctionParametersNames;
73
74
75
    /**
76
     * Returns an array of tokens this test wants to listen for.
77
     *
78
     * @return array
79
     */
80 View Code Duplication
    public function register()
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...
81
    {
82
        // Everyone has had a chance to figure out what forbidden functions
83
        // they want to check for, so now we can cache out the list.
84
        $this->newFunctionParametersNames = array_keys($this->newFunctionParameters);
85
    
86
        if ($this->patternMatch === true) {
87
            foreach ($this->newFunctionParametersNames as $i => $name) {
88
                $this->newFunctionParametersNames[$i] = '/'.$name.'/i';
89
            }
90
        }
91
    
92
        return array(T_STRING);
93
    }//end register()
94
    
95
    /**
96
     * Processes this test, when one of its tokens is encountered.
97
     *
98
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
99
     * @param int                  $stackPtr  The position of the current token in
100
     *                                        the stack passed in $tokens.
101
     *
102
     * @return void
103
     */
104
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
105
    {
106
        $tokens = $phpcsFile->getTokens();
107
108
        $ignore = array(
109
                T_DOUBLE_COLON,
110
                T_OBJECT_OPERATOR,
111
                T_FUNCTION,
112
                T_CONST,
113
        );
114
115
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
116
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
117
            // Not a call to a PHP function.
118
            return;
119
        }
120
121
        $function = strtolower($tokens[$stackPtr]['content']);
122
123
        if (in_array($function, $this->newFunctionParametersNames) === false) {
124
            return;
125
        }
126
        
127
        if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['type'] == 'T_OPEN_PARENTHESIS') {
128
            $closeParenthesis = $tokens[$stackPtr + 1]['parenthesis_closer'];
129
130
            $nextComma = $stackPtr + 1;
131
            $cnt = 0;
132
            while ($nextComma = $phpcsFile->findNext(array(T_COMMA, T_CLOSE_PARENTHESIS), $nextComma + 1, $closeParenthesis + 1)) {
133
                if ($tokens[$nextComma]['type'] == 'T_CLOSE_PARENTHESIS' && $nextComma != $closeParenthesis) {
134
                    continue;
135
                }
136
                if (isset($this->newFunctionParameters[$function][$cnt])) {
137
                    $this->addError($phpcsFile, $nextComma, $function, $cnt);
138
                }
139
                $cnt++;
140
            }
141
            
142
        }
143
    }//end process()
144
145
146
    /**
147
     * Generates the error or wanrning for this sniff.
148
     *
149
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
150
     * @param int                  $stackPtr  The position of the function
151
     *                                        in the token array.
152
     * @param string               $function  The name of the function.
153
     * @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...
154
     *
155
     * @return void
156
     */
157 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...
158
    {
159
        $error = '';
160
161
        $this->error = false;
162
        foreach ($this->newFunctionParameters[$function][$parameterLocation] as $version => $present) {
163
            if ($version != 'name' && $this->supportsBelow($version)) {
164
                if ($present === false) {
165
                    $this->error = true;
166
                    $error .= 'in PHP version ' . $version . ' or earlier';
167
                }
168
            }
169
        }
170
        
171
        if (strlen($error) > 0) {
172
            $error = 'The function ' . $function . ' does not have a parameter ' . $this->newFunctionParameters[$function][$parameterLocation]['name'] . ' ' . $error;
173
174
            if ($this->error === true) {
175
                $phpcsFile->addError($error, $stackPtr);
176
            } else {
177
                $phpcsFile->addWarning($error, $stackPtr);
178
            }
179
        }
180
181
    }//end addError()
182
183
}//end class
184