Completed
Pull Request — master (#232)
by Juliette
02:38
created

addError()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 14
nc 12
nop 3
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewClassesSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2013 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_NewClassesSniff.
15
 *
16
 * @category  PHP
17
 * @package   PHPCompatibility
18
 * @author    Wim Godden <[email protected]>
19
 * @version   1.0.0
20
 * @copyright 2013 Cu.be Solutions bvba
21
 */
22
class PHPCompatibility_Sniffs_PHP_NewClassesSniff extends PHPCompatibility_Sniff
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...
23
{
24
25
    /**
26
     * A list of new classes, not present in older versions.
27
     *
28
     * The array lists : version number with false (not present) or true (present).
29
     * If's sufficient to list the first version where the class appears.
30
     *
31
     * @var array(string => array(string => int|string|null))
32
     */
33
    protected $newClasses = array(
34
                                        'DateTime' => array(
35
                                            '5.1' => false,
36
                                            '5.2' => true
37
                                        ),
38
                                        'DateTimeZone' => array(
39
                                            '5.1' => false,
40
                                            '5.2' => true
41
                                        ),
42
                                        'RegexIterator' => array(
43
                                            '5.1' => false,
44
                                            '5.2' => true
45
                                        ),
46
                                        'RecursiveRegexIterator' => array(
47
                                            '5.1' => false,
48
                                            '5.2' => true
49
                                        ),
50
51
                                        'DateInterval' => array(
52
                                            '5.2' => false,
53
                                            '5.3' => true
54
                                        ),
55
                                        'DatePeriod' => array(
56
                                            '5.2' => false,
57
                                            '5.3' => true
58
                                        ),
59
                                        'Phar' => array(
60
                                            '5.2' => false,
61
                                            '5.3' => true
62
                                        ),
63
                                        'PharData' => array(
64
                                            '5.2' => false,
65
                                            '5.3' => true
66
                                        ),
67
                                        'PharException' => array(
68
                                            '5.2' => false,
69
                                            '5.3' => true
70
                                        ),
71
                                        'PharFileInfo' => array(
72
                                            '5.2' => false,
73
                                            '5.3' => true
74
                                        ),
75
                                        'FilesystemIterator' => array(
76
                                            '5.2' => false,
77
                                            '5.3' => true
78
                                        ),
79
                                        'GlobIterator' => array(
80
                                            '5.2' => false,
81
                                            '5.3' => true
82
                                        ),
83
                                        'MultipleIterator' => array(
84
                                            '5.2' => false,
85
                                            '5.3' => true
86
                                        ),
87
                                        'RecursiveTreeIterator' => array(
88
                                            '5.2' => false,
89
                                            '5.3' => true
90
                                        ),
91
                                        'SplDoublyLinkedList' => array(
92
                                            '5.2' => false,
93
                                            '5.3' => true
94
                                        ),
95
                                        'SplFixedArray' => array(
96
                                            '5.2' => false,
97
                                            '5.3' => true
98
                                        ),
99
                                        'SplHeap' => array(
100
                                            '5.2' => false,
101
                                            '5.3' => true
102
                                        ),
103
                                        'SplMaxHeap' => array(
104
                                            '5.2' => false,
105
                                            '5.3' => true
106
                                        ),
107
                                        'SplMinHeap' => array(
108
                                            '5.2' => false,
109
                                            '5.3' => true
110
                                        ),
111
                                        'SplPriorityQueue' => array(
112
                                            '5.2' => false,
113
                                            '5.3' => true
114
                                        ),
115
                                        'SplQueue' => array(
116
                                            '5.2' => false,
117
                                            '5.3' => true
118
                                        ),
119
                                        'SplStack' => array(
120
                                            '5.2' => false,
121
                                            '5.3' => true
122
                                        ),
123
124
                                        'CallbackFilterIterator' => array(
125
                                            '5.3' => false,
126
                                            '5.4' => true
127
                                        ),
128
                                        'RecursiveCallbackFilterIterator' => array(
129
                                            '5.3' => false,
130
                                            '5.4' => true
131
                                        ),
132
                                        'ReflectionZendExtension' => array(
133
                                            '5.3' => false,
134
                                            '5.4' => true
135
                                        ),
136
                                        'SessionHandler' => array(
137
                                            '5.3' => false,
138
                                            '5.4' => true
139
                                        ),
140
                                        'SNMP' => array(
141
                                            '5.3' => false,
142
                                            '5.4' => true
143
                                        ),
144
                                        'Transliterator' => array(
145
                                            '5.3' => false,
146
                                            '5.4' => true
147
                                        ),
148
                                        'Spoofchecker' => array(
149
                                            '5.3' => false,
150
                                            '5.4' => true
151
                                        ),
152
153
                                        'CURLFile' => array(
154
                                            '5.4' => false,
155
                                            '5.5' => true
156
                                        ),
157
                                        'DateTimeImmutable' => array(
158
                                            '5.4' => false,
159
                                            '5.5' => true
160
                                        ),
161
                                        'IntlCalendar' => array(
162
                                            '5.4' => false,
163
                                            '5.5' => true
164
                                        ),
165
                                        'IntlGregorianCalendar' => array(
166
                                            '5.4' => false,
167
                                            '5.5' => true
168
                                        ),
169
                                        'IntlTimeZone' => array(
170
                                            '5.4' => false,
171
                                            '5.5' => true
172
                                        ),
173
                                        'IntlBreakIterator' => array(
174
                                            '5.4' => false,
175
                                            '5.5' => true
176
                                        ),
177
                                        'IntlRuleBasedBreakIterator' => array(
178
                                            '5.4' => false,
179
                                            '5.5' => true
180
                                        ),
181
                                        'IntlCodePointBreakIterator' => array(
182
                                            '5.4' => false,
183
                                            '5.5' => true
184
                                        ),
185
186
                                    );
187
188
189
    /**
190
     * Returns an array of tokens this test wants to listen for.
191
     *
192
     * @return array
193
     */
194
    public function register()
195
    {
196
        return array(
197
                T_NEW,
198
                T_CLASS,
199
                T_DOUBLE_COLON,
200
               );
201
202
    }//end register()
203
204
205
    /**
206
     * Processes this test, when one of its tokens is encountered.
207
     *
208
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
209
     * @param int                  $stackPtr  The position of the current token in
210
     *                                        the stack passed in $tokens.
211
     *
212
     * @return void
213
     */
214
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
215
    {
216
        $tokens      = $phpcsFile->getTokens();
217
        $FQClassName = '';
218
219
        if ($tokens[$stackPtr]['type'] === 'T_NEW') {
220
            $FQClassName = $this->getFQClassNameFromNewToken($phpcsFile, $stackPtr);
221
        }
222
        else if ($tokens[$stackPtr]['type'] === 'T_CLASS') {
223
            $FQClassName = $this->getFQExtendedClassName($phpcsFile, $stackPtr);
224
        }
225
        else if ($tokens[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
226
            $FQClassName = $this->getFQClassNameFromDoubleColonToken($phpcsFile, $stackPtr);
227
        }
228
229
        if ($FQClassName === '') {
230
            return;
231
        }
232
233
        if ($this->isNamespaced($FQClassName) === true) {
234
            return;
235
        }
236
237
        $className = substr($FQClassName, 1); // Remove global namespace indicator.
238
239
        if (array_key_exists($className, $this->newClasses) === false) {
240
            return;
241
        }
242
243
        $this->addError($phpcsFile, $stackPtr, $className);
244
245
    }//end process()
246
247
248
    /**
249
     * Generates the error or warning for this sniff.
250
     *
251
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
252
     * @param int                  $stackPtr  The position of the function
253
     *                                        in the token array.
254
     * @param string               $className The name of the class.
255
     *
256
     * @return void
257
     */
258
    protected function addError($phpcsFile, $stackPtr, $className)
259
    {
260
        $error = '';
261
262
        $isError = false;
263
        foreach ($this->newClasses[$className] as $version => $present) {
264
            if ($this->supportsBelow($version)) {
265
                if ($present === false) {
266
                    $isError = true;
267
                    $error .= 'not present in PHP version ' . $version . ' or earlier';
268
                }
269
            }
270
        }
271
        if (strlen($error) > 0) {
272
            $error = 'The built-in class ' . $className . ' is ' . $error;
273
274
            if ($isError === true) {
275
                $phpcsFile->addError($error, $stackPtr);
276
            } else {
277
                $phpcsFile->addWarning($error, $stackPtr);
278
            }
279
        }
280
281
    }//end addError()
282
283
}//end class
284