Completed
Pull Request — master (#291)
by Juliette
02:26
created

getErrorMsgTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
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
    extends PHPCompatibility_AbstractRemovedFeatureSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
19
{
20
    /**
21
     * A list of removed function parameters, which were present in older versions.
22
     *
23
     * The array lists : version number with false (deprecated) and true (removed).
24
     * The index is the location of the parameter in the parameter list, starting at 0 !
25
     * If's sufficient to list the first version where the function parameter was deprecated/removed.
26
     *
27
     * @var array
28
     */
29
    protected $removedFunctionParameters = array(
30
                                        'gmmktime' => array(
31
                                            6 => array(
32
                                                'name' => 'is_dst',
33
                                                '5.1' => false, // deprecated
34
                                                '7.0' => true,
35
                                            ),
36
                                        ),
37
                                        'ldap_first_attribute' => array(
38
                                            2 => array(
39
                                                'name' => 'ber_identifier',
40
                                                '5.2.4' => true,
41
                                            ),
42
                                        ),
43
                                        'ldap_next_attribute' => array(
44
                                            2 => array(
45
                                                'name' => 'ber_identifier',
46
                                                '5.2.4' => true,
47
                                            ),
48
                                        ),
49
                                        'mktime' => array(
50
                                            6 => array(
51
                                                'name' => 'is_dst',
52
                                                '5.1' => false, // deprecated
53
                                                '7.0' => true,
54
                                            ),
55
                                        ),
56
                                    );
57
58
59
    /**
60
     * Returns an array of tokens this test wants to listen for.
61
     *
62
     * @return array
63
     */
64
    public function register()
65
    {
66
        // Handle case-insensitivity of function names.
67
        $this->removedFunctionParameters = $this->arrayKeysToLowercase($this->removedFunctionParameters);
68
69
        return array(T_STRING);
70
    }//end register()
71
72
    /**
73
     * Processes this test, when one of its tokens is encountered.
74
     *
75
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
76
     * @param int                  $stackPtr  The position of the current token in
77
     *                                        the stack passed in $tokens.
78
     *
79
     * @return void
80
     */
81 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...
82
    {
83
        $tokens = $phpcsFile->getTokens();
84
85
        $ignore = array(
86
                T_DOUBLE_COLON,
87
                T_OBJECT_OPERATOR,
88
                T_FUNCTION,
89
                T_CONST,
90
        );
91
92
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
93
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
94
            // Not a call to a PHP function.
95
            return;
96
        }
97
98
        $function   = $tokens[$stackPtr]['content'];
99
        $functionLc = strtolower($function);
100
101
        if (isset($this->removedFunctionParameters[$functionLc]) === false) {
102
            return;
103
        }
104
105
        $parameterCount = $this->getFunctionCallParameterCount($phpcsFile, $stackPtr);
106
        if ($parameterCount === 0) {
107
            return;
108
        }
109
110
        // If the parameter count returned > 0, we know there will be valid open parenthesis.
111
        $openParenthesis = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $stackPtr + 1, null, true, null, true);
112
        $parameterOffsetFound = $parameterCount - 1;
113
114
        foreach($this->removedFunctionParameters[$functionLc] as $offset => $parameterDetails) {
115
            if ($offset <= $parameterOffsetFound) {
116
                $itemInfo = array(
117
                    'name'   => $function,
118
                    'nameLc' => $functionLc,
119
                    'offset' => $offset,
120
                );
121
                $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 111 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...
122
            }
123
        }
124
125
    }//end process()
126
127
128
    /**
129
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
130
     *
131
     * @param array $itemInfo Base information about the item.
132
     *
133
     * @return array Version and other information about the item.
134
     */
135
    public function getItemArray(array $itemInfo)
136
    {
137
        return $this->removedFunctionParameters[$itemInfo['nameLc']][$itemInfo['offset']];
138
    }
139
140
141
    /**
142
     * Get an array of the non-PHP-version array keys used in a sub-array.
143
     *
144
     * @return array
145
     */
146
    protected function getNonVersionArrayKeys()
147
    {
148
        return array('name');
149
    }
150
151
152
    /**
153
     * Retrieve the relevant detail (version) information for use in an error message.
154
     *
155
     * @param array $itemArray Version and other information about the item.
156
     * @param array $itemInfo  Base information about the item.
157
     *
158
     * @return array
159
     */
160
    public function getErrorInfo(array $itemArray, array $itemInfo)
161
    {
162
        $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
163
        $errorInfo['paramName'] = $itemArray['name'];
164
165
        return $errorInfo;
166
    }
167
168
169
    /**
170
     * Get the item name to be used for the creation of the error code.
171
     *
172
     * @param array $itemInfo  Base information about the item.
173
     * @param array $errorInfo Detail information about an item.
174
     *
175
     * @return string
176
     */
177
    protected function getItemName(array $itemInfo, array $errorInfo)
178
    {
179
        return $itemInfo['name'].'_'.$errorInfo['paramName'];
180
    }
181
182
183
    /**
184
     * Get the error message template for this sniff.
185
     *
186
     * @return string
187
     */
188
    protected function getErrorMsgTemplate()
189
    {
190
        return 'The "%s" parameter for function %s() is ';
191
    }
192
193
194
    /**
195
     * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
196
     *
197
     * @param array $data      The error data array which was created.
198
     * @param array $itemInfo  Base information about the item this error message applied to.
199
     * @param array $errorInfo Detail information about an item this error message applied to.
200
     *
201
     * @return array
202
     */
203
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
204
    {
205
        array_shift($data);
206
        array_unshift($data, $errorInfo['paramName'], $itemInfo['name']);
207
        return $data;
208
    }
209
210
211
}//end class
212