Completed
Pull Request — master (#416)
by Juliette
02:21
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_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_AbstractNewFeatureSniff
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 => bool))
32
     */
33
    protected $newClasses = array(
34
                                        'libXMLError' => array(
35
                                            '5.0' => false,
36
                                            '5.1' => true
37
                                        ),
38
39
                                        'DateTime' => array(
40
                                            '5.1' => false,
41
                                            '5.2' => true
42
                                        ),
43
                                        'DateTimeZone' => array(
44
                                            '5.1' => false,
45
                                            '5.2' => true
46
                                        ),
47
                                        'RegexIterator' => array(
48
                                            '5.1' => false,
49
                                            '5.2' => true
50
                                        ),
51
                                        'RecursiveRegexIterator' => array(
52
                                            '5.1' => false,
53
                                            '5.2' => true
54
                                        ),
55
56
                                        'DateInterval' => array(
57
                                            '5.2' => false,
58
                                            '5.3' => true
59
                                        ),
60
                                        'DatePeriod' => array(
61
                                            '5.2' => false,
62
                                            '5.3' => true
63
                                        ),
64
                                        'Phar' => array(
65
                                            '5.2' => false,
66
                                            '5.3' => true
67
                                        ),
68
                                        'PharData' => array(
69
                                            '5.2' => false,
70
                                            '5.3' => true
71
                                        ),
72
                                        'PharException' => array(
73
                                            '5.2' => false,
74
                                            '5.3' => true
75
                                        ),
76
                                        'PharFileInfo' => array(
77
                                            '5.2' => false,
78
                                            '5.3' => true
79
                                        ),
80
                                        'FilesystemIterator' => array(
81
                                            '5.2' => false,
82
                                            '5.3' => true
83
                                        ),
84
                                        'GlobIterator' => array(
85
                                            '5.2' => false,
86
                                            '5.3' => true
87
                                        ),
88
                                        'MultipleIterator' => array(
89
                                            '5.2' => false,
90
                                            '5.3' => true
91
                                        ),
92
                                        'RecursiveTreeIterator' => array(
93
                                            '5.2' => false,
94
                                            '5.3' => true
95
                                        ),
96
                                        'SplDoublyLinkedList' => array(
97
                                            '5.2' => false,
98
                                            '5.3' => true
99
                                        ),
100
                                        'SplFixedArray' => array(
101
                                            '5.2' => false,
102
                                            '5.3' => true
103
                                        ),
104
                                        'SplHeap' => array(
105
                                            '5.2' => false,
106
                                            '5.3' => true
107
                                        ),
108
                                        'SplMaxHeap' => array(
109
                                            '5.2' => false,
110
                                            '5.3' => true
111
                                        ),
112
                                        'SplMinHeap' => array(
113
                                            '5.2' => false,
114
                                            '5.3' => true
115
                                        ),
116
                                        'SplPriorityQueue' => array(
117
                                            '5.2' => false,
118
                                            '5.3' => true
119
                                        ),
120
                                        'SplQueue' => array(
121
                                            '5.2' => false,
122
                                            '5.3' => true
123
                                        ),
124
                                        'SplStack' => array(
125
                                            '5.2' => false,
126
                                            '5.3' => true
127
                                        ),
128
129
                                        'CallbackFilterIterator' => array(
130
                                            '5.3' => false,
131
                                            '5.4' => true
132
                                        ),
133
                                        'RecursiveCallbackFilterIterator' => array(
134
                                            '5.3' => false,
135
                                            '5.4' => true
136
                                        ),
137
                                        'ReflectionZendExtension' => array(
138
                                            '5.3' => false,
139
                                            '5.4' => true
140
                                        ),
141
                                        'SessionHandler' => array(
142
                                            '5.3' => false,
143
                                            '5.4' => true
144
                                        ),
145
                                        'SNMP' => array(
146
                                            '5.3' => false,
147
                                            '5.4' => true
148
                                        ),
149
                                        'Transliterator' => array(
150
                                            '5.3' => false,
151
                                            '5.4' => true
152
                                        ),
153
                                        'Spoofchecker' => array(
154
                                            '5.3' => false,
155
                                            '5.4' => true
156
                                        ),
157
158
                                        'CURLFile' => array(
159
                                            '5.4' => false,
160
                                            '5.5' => true
161
                                        ),
162
                                        'DateTimeImmutable' => array(
163
                                            '5.4' => false,
164
                                            '5.5' => true
165
                                        ),
166
                                        'IntlCalendar' => array(
167
                                            '5.4' => false,
168
                                            '5.5' => true
169
                                        ),
170
                                        'IntlGregorianCalendar' => array(
171
                                            '5.4' => false,
172
                                            '5.5' => true
173
                                        ),
174
                                        'IntlTimeZone' => array(
175
                                            '5.4' => false,
176
                                            '5.5' => true
177
                                        ),
178
                                        'IntlBreakIterator' => array(
179
                                            '5.4' => false,
180
                                            '5.5' => true
181
                                        ),
182
                                        'IntlRuleBasedBreakIterator' => array(
183
                                            '5.4' => false,
184
                                            '5.5' => true
185
                                        ),
186
                                        'IntlCodePointBreakIterator' => array(
187
                                            '5.4' => false,
188
                                            '5.5' => true
189
                                        ),
190
191
                                    );
192
193
194
    /**
195
     * Returns an array of tokens this test wants to listen for.
196
     *
197
     * @return array
198
     */
199
    public function register()
200
    {
201
        // Handle case-insensitivity of class names.
202
        $this->newClasses = $this->arrayKeysToLowercase($this->newClasses);
203
204
        $targets = array(
205
            T_NEW,
206
            T_CLASS,
207
            T_DOUBLE_COLON,
208
            T_FUNCTION,
209
            T_CLOSURE,
210
        );
211
212
        if (defined('T_ANON_CLASS')) {
213
            $targets[] = constant('T_ANON_CLASS');
214
        }
215
216
        return $targets;
217
218
    }//end register()
219
220
221
    /**
222
     * Processes this test, when one of its tokens is encountered.
223
     *
224
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
225
     * @param int                  $stackPtr  The position of the current token in
226
     *                                        the stack passed in $tokens.
227
     *
228
     * @return void
229
     */
230 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...
231
    {
232
        $tokens = $phpcsFile->getTokens();
233
234
        switch($tokens[$stackPtr]['type']) {
235
            case 'T_FUNCTION':
236
            case 'T_CLOSURE':
237
                $this->processFunctionToken($phpcsFile, $stackPtr);
238
                break;
239
240
            default:
241
                $this->processSingularToken($phpcsFile, $stackPtr);
242
                break;
243
        }
244
245
    }//end process()
246
247
248
    /**
249
     * Processes this test for when a token resulting in a singular class name is encountered.
250
     *
251
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
252
     * @param int                  $stackPtr  The position of the current token in
253
     *                                        the stack passed in $tokens.
254
     *
255
     * @return void
256
     */
257
    private function processSingularToken(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
258
    {
259
        $tokens      = $phpcsFile->getTokens();
260
        $FQClassName = '';
261
262
        if ($tokens[$stackPtr]['type'] === 'T_NEW') {
263
            $FQClassName = $this->getFQClassNameFromNewToken($phpcsFile, $stackPtr);
264
        }
265
        else if ($tokens[$stackPtr]['type'] === 'T_CLASS' || $tokens[$stackPtr]['type'] === 'T_ANON_CLASS') {
266
            $FQClassName = $this->getFQExtendedClassName($phpcsFile, $stackPtr);
267
        }
268
        else if ($tokens[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
269
            $FQClassName = $this->getFQClassNameFromDoubleColonToken($phpcsFile, $stackPtr);
270
        }
271
272
        if ($FQClassName === '') {
273
            return;
274
        }
275
276
        if ($this->isNamespaced($FQClassName) === true) {
277
            return;
278
        }
279
280
        $className   = substr($FQClassName, 1); // Remove global namespace indicator.
281
        $classNameLc = strtolower($className);
282
283
        if (isset($this->newClasses[$classNameLc]) === false) {
284
            return;
285
        }
286
287
        $itemInfo = array(
288
            'name'   => $className,
289
            'nameLc' => $classNameLc,
290
        );
291
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
292
293
    }//end processSingularToken()
294
295
296
    /**
297
     * Processes this test for when a function token is encountered.
298
     *
299
     * - Detect new classes when used as a type hint.
300
     *
301
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
302
     * @param int                  $stackPtr  The position of the current token in
303
     *                                        the stack passed in $tokens.
304
     *
305
     * @return void
306
     */
307 View Code Duplication
    private function processFunctionToken(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...
308
    {
309
        // Retrieve typehints stripped of global NS indicator and/or nullable indicator.
310
        $typeHints = $this->getTypeHintsFromFunctionDeclaration($phpcsFile, $stackPtr);
311
        if (empty($typeHints) || is_array($typeHints) === false) {
312
            return;
313
        }
314
315
        foreach ($typeHints as $hint) {
316
317
            $typeHintLc = strtolower($hint);
318
319
            if (isset($this->newClasses[$typeHintLc]) === true) {
320
                $itemInfo = array(
321
                    'name'   => $hint,
322
                    'nameLc' => $typeHintLc,
323
                );
324
                $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
325
            }
326
        }
327
    }
328
329
330
    /**
331
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
332
     *
333
     * @param array $itemInfo Base information about the item.
334
     *
335
     * @return array Version and other information about the item.
336
     */
337
    public function getItemArray(array $itemInfo)
338
    {
339
        return $this->newClasses[$itemInfo['nameLc']];
340
    }
341
342
343
    /**
344
     * Get the error message template for this sniff.
345
     *
346
     * @return string
347
     */
348
    protected function getErrorMsgTemplate()
349
    {
350
        return 'The built-in class '.parent::getErrorMsgTemplate();
351
    }
352
353
354
}//end class
355