Completed
Push — php70 ( 4ff9ba...992172 )
by Wim
02:29
created

NewFunctionParametersSniff   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 150
rs 10
wmc 17
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 14 3
C process() 0 37 7
C addError() 0 25 7
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
                                    );
45
46
47
    /**
48
     * If true, an error will be thrown; otherwise a warning.
49
     *
50
     * @var bool
51
     */
52
    public $error = false;
53
    
54
    /**
55
     * 
56
     * @var array
57
     */
58
    private $newFunctionParametersNames;
59
60
61
    /**
62
     * Returns an array of tokens this test wants to listen for.
63
     *
64
     * @return array
65
     */
66
    public function register()
67
    {
68
        // Everyone has had a chance to figure out what forbidden functions
69
        // they want to check for, so now we can cache out the list.
70
        $this->newFunctionParametersNames = array_keys($this->newFunctionParameters);
71
    
72
        if ($this->patternMatch === true) {
73
            foreach ($this->newFunctionParametersNames as $i => $name) {
74
                $this->newFunctionParametersNames[$i] = '/'.$name.'/i';
75
            }
76
        }
77
    
78
        return array(T_STRING);
79
    }//end register()
80
    
81
    /**
82
     * Processes this test, when one of its tokens is encountered.
83
     *
84
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
85
     * @param int                  $stackPtr  The position of the current token in
86
     *                                        the stack passed in $tokens.
87
     *
88
     * @return void
89
     */
90
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
91
    {
92
        $tokens = $phpcsFile->getTokens();
93
94
        $ignore = array(
95
                T_DOUBLE_COLON,
96
                T_OBJECT_OPERATOR,
97
                T_FUNCTION,
98
                T_CONST,
99
        );
100
101
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
102
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
103
            // Not a call to a PHP function.
104
            return;
105
        }
106
107
        $function = strtolower($tokens[$stackPtr]['content']);
108
109
        if (in_array($function, $this->newFunctionParametersNames) === false) {
110
            return;
111
        }
112
        
113
        if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['type'] == 'T_OPEN_PARENTHESIS') {
114
            $closeParenthesis = $tokens[$stackPtr + 1]['parenthesis_closer'];
115
116
            $nextComma = $stackPtr + 1;
117
            $cnt = 0;
118
            while ($nextComma = $phpcsFile->findNext(array(T_COMMA, T_CLOSE_PARENTHESIS), $nextComma + 1, $closeParenthesis + 1)) {
119
                if (isset($this->newFunctionParameters[$function][$cnt])) {
120
                    $this->addError($phpcsFile, $nextComma, $function, $cnt);
121
                }
122
                $cnt++;
123
            }
124
            
125
        }
126
    }//end process()
127
128
129
    /**
130
     * Generates the error or wanrning for this sniff.
131
     *
132
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
133
     * @param int                  $stackPtr  The position of the function
134
     *                                        in the token array.
135
     * @param string               $function  The name of the function.
136
     * @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...
137
     *
138
     * @return void
139
     */
140
    protected function addError($phpcsFile, $stackPtr, $function, $parameterLocation)
141
    {
142
        $error = '';
143
144
        $this->error = false;
145
        foreach ($this->newFunctionParameters[$function][$parameterLocation] as $version => $present) {
146
            if ($version != 'name' && $this->supportsBelow($version)) {
147
                if ($present === false) {
148
                    $this->error = true;
149
                    $error .= 'in PHP version ' . $version . ' or earlier';
150
                }
151
            }
152
        }
153
        
154
        if (strlen($error) > 0) {
155
            $error = 'The function ' . $function . ' does not have a parameter ' . $this->newFunctionParameters[$function][$parameterLocation]['name'] . ' ' . $error;
156
157
            if ($this->error === true) {
158
                $phpcsFile->addError($error, $stackPtr);
159
            } else {
160
                $phpcsFile->addWarning($error, $stackPtr);
161
            }
162
        }
163
164
    }//end addError()
165
166
}//end class
167