Completed
Push — master ( a32632...c637b7 )
by Juliette
9s
created

RemovedFunctionParametersSniff::addError()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 39
Code Lines 25

Duplication

Lines 20
Ratio 51.28 %

Importance

Changes 0
Metric Value
dl 20
loc 39
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 25
nc 15
nop 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A RemovedFunctionParametersSniff::getNonVersionArrayKeys() 0 4 1
A RemovedFunctionParametersSniff::getErrorInfo() 0 7 1
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_AbstractRemovedFeatureSniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
18
{
19
    /**
20
     * A list of removed function parameters, which were present in older versions.
21
     *
22
     * The array lists : version number with false (deprecated) and true (removed).
23
     * The index is the location of the parameter in the parameter list, starting at 0 !
24
     * If's sufficient to list the first version where the function parameter was deprecated/removed.
25
     *
26
     * @var array
27
     */
28
    protected $removedFunctionParameters = array(
29
                                        'gmmktime' => array(
30
                                            6 => array(
31
                                                'name' => 'is_dst',
32
                                                '5.1' => false, // deprecated
33
                                                '7.0' => true,
34
                                            ),
35
                                        ),
36
                                        'ldap_first_attribute' => array(
37
                                            2 => array(
38
                                                'name' => 'ber_identifier',
39
                                                '5.2.4' => true,
40
                                            ),
41
                                        ),
42
                                        'ldap_next_attribute' => array(
43
                                            2 => array(
44
                                                'name' => 'ber_identifier',
45
                                                '5.2.4' => true,
46
                                            ),
47
                                        ),
48
                                        'mktime' => array(
49
                                            6 => array(
50
                                                'name' => 'is_dst',
51
                                                '5.1' => false, // deprecated
52
                                                '7.0' => true,
53
                                            ),
54
                                        ),
55
                                    );
56
57
58
    /**
59
     * Returns an array of tokens this test wants to listen for.
60
     *
61
     * @return array
62
     */
63
    public function register()
64
    {
65
        // Handle case-insensitivity of function names.
66
        $this->removedFunctionParameters = $this->arrayKeysToLowercase($this->removedFunctionParameters);
67
68
        return array(T_STRING);
69
    }//end register()
70
71
    /**
72
     * Processes this test, when one of its tokens is encountered.
73
     *
74
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
75
     * @param int                  $stackPtr  The position of the current token in
76
     *                                        the stack passed in $tokens.
77
     *
78
     * @return void
79
     */
80 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...
81
    {
82
        $tokens = $phpcsFile->getTokens();
83
84
        $ignore = array(
85
                T_DOUBLE_COLON,
86
                T_OBJECT_OPERATOR,
87
                T_FUNCTION,
88
                T_CONST,
89
        );
90
91
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
92
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
93
            // Not a call to a PHP function.
94
            return;
95
        }
96
97
        $function   = $tokens[$stackPtr]['content'];
98
        $functionLc = strtolower($function);
99
100
        if (isset($this->removedFunctionParameters[$functionLc]) === false) {
101
            return;
102
        }
103
104
        $parameterCount = $this->getFunctionCallParameterCount($phpcsFile, $stackPtr);
105
        if ($parameterCount === 0) {
106
            return;
107
        }
108
109
        // If the parameter count returned > 0, we know there will be valid open parenthesis.
110
        $openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
111
        $parameterOffsetFound = $parameterCount - 1;
112
113
        foreach($this->removedFunctionParameters[$functionLc] as $offset => $parameterDetails) {
114
            if ($offset <= $parameterOffsetFound) {
115
                $itemInfo = array(
116
                    'name'   => $function,
117
                    'nameLc' => $functionLc,
118
                    'offset' => $offset,
119
                );
120
                $this->handleFeature($phpcsFile, $openParenthesis, $itemInfo);
0 ignored issues
show
Security Bug introduced by
It seems like $openParenthesis defined by $phpcsFile->findNext(\PH...null, true, null, true) on line 110 can also be of type false; however, PHPCompatibility_Abstrac...nSniff::handleFeature() does only seem to accept integer, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
121
            }
122
        }
123
124
    }//end process()
125
126
127
    /**
128
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
129
     *
130
     * @param array $itemInfo Base information about the item.
131
     *
132
     * @return array Version and other information about the item.
133
     */
134
    public function getItemArray(array $itemInfo)
135
    {
136
        return $this->removedFunctionParameters[$itemInfo['nameLc']][$itemInfo['offset']];
137
    }
138
139
140
    /**
141
     * Get an array of the non-PHP-version array keys used in a sub-array.
142
     *
143
     * @return array
144
     */
145
    protected function getNonVersionArrayKeys()
146
    {
147
        return array('name');
148
    }
149
150
151
    /**
152
     * Retrieve the relevant detail (version) information for use in an error message.
153
     *
154
     * @param array $itemArray Version and other information about the item.
155
     * @param array $itemInfo  Base information about the item.
156
     *
157
     * @return array
158
     */
159
    public function getErrorInfo(array $itemArray, array $itemInfo)
160
    {
161
        $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
162
        $errorInfo['paramName'] = $itemArray['name'];
163
164
        return $errorInfo;
165
    }
166
167
168
    /**
169
     * Get the item name to be used for the creation of the error code.
170
     *
171
     * @param array $itemInfo  Base information about the item.
172
     * @param array $errorInfo Detail information about an item.
173
     *
174
     * @return string
175
     */
176
    protected function getItemName(array $itemInfo, array $errorInfo)
177
    {
178
        return $itemInfo['name'].'_'.$errorInfo['paramName'];
179
    }
180
181
182
    /**
183
     * Get the error message template for this sniff.
184
     *
185
     * @return string
186
     */
187
    protected function getErrorMsgTemplate()
188
    {
189
        return 'The "%s" parameter for function %s() is ';
190
    }
191
192
193
    /**
194
     * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
195
     *
196
     * @param array $data      The error data array which was created.
197
     * @param array $itemInfo  Base information about the item this error message applied to.
198
     * @param array $errorInfo Detail information about an item this error message applied to.
199
     *
200
     * @return array
201
     */
202
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
203
    {
204
        array_shift($data);
205
        array_unshift($data, $errorInfo['paramName'], $itemInfo['name']);
206
        return $data;
207
    }
208
209
210
}//end class
211