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

getItemArray()   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 1
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
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
    extends PHPCompatibility_AbstractNewFeatureSniff
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
24
{
25
26
    /**
27
     * A list of new classes, not present in older versions.
28
     *
29
     * The array lists : version number with false (not present) or true (present).
30
     * If's sufficient to list the first version where the class appears.
31
     *
32
     * @var array(string => array(string => int|string|null))
33
     */
34
    protected $newClasses = array(
35
                                        'DateTime' => array(
36
                                            '5.1' => false,
37
                                            '5.2' => true
38
                                        ),
39
                                        'DateTimeZone' => array(
40
                                            '5.1' => false,
41
                                            '5.2' => true
42
                                        ),
43
                                        'RegexIterator' => array(
44
                                            '5.1' => false,
45
                                            '5.2' => true
46
                                        ),
47
                                        'RecursiveRegexIterator' => array(
48
                                            '5.1' => false,
49
                                            '5.2' => true
50
                                        ),
51
52
                                        'DateInterval' => array(
53
                                            '5.2' => false,
54
                                            '5.3' => true
55
                                        ),
56
                                        'DatePeriod' => array(
57
                                            '5.2' => false,
58
                                            '5.3' => true
59
                                        ),
60
                                        'Phar' => array(
61
                                            '5.2' => false,
62
                                            '5.3' => true
63
                                        ),
64
                                        'PharData' => array(
65
                                            '5.2' => false,
66
                                            '5.3' => true
67
                                        ),
68
                                        'PharException' => array(
69
                                            '5.2' => false,
70
                                            '5.3' => true
71
                                        ),
72
                                        'PharFileInfo' => array(
73
                                            '5.2' => false,
74
                                            '5.3' => true
75
                                        ),
76
                                        'FilesystemIterator' => array(
77
                                            '5.2' => false,
78
                                            '5.3' => true
79
                                        ),
80
                                        'GlobIterator' => array(
81
                                            '5.2' => false,
82
                                            '5.3' => true
83
                                        ),
84
                                        'MultipleIterator' => array(
85
                                            '5.2' => false,
86
                                            '5.3' => true
87
                                        ),
88
                                        'RecursiveTreeIterator' => array(
89
                                            '5.2' => false,
90
                                            '5.3' => true
91
                                        ),
92
                                        'SplDoublyLinkedList' => array(
93
                                            '5.2' => false,
94
                                            '5.3' => true
95
                                        ),
96
                                        'SplFixedArray' => array(
97
                                            '5.2' => false,
98
                                            '5.3' => true
99
                                        ),
100
                                        'SplHeap' => array(
101
                                            '5.2' => false,
102
                                            '5.3' => true
103
                                        ),
104
                                        'SplMaxHeap' => array(
105
                                            '5.2' => false,
106
                                            '5.3' => true
107
                                        ),
108
                                        'SplMinHeap' => array(
109
                                            '5.2' => false,
110
                                            '5.3' => true
111
                                        ),
112
                                        'SplPriorityQueue' => array(
113
                                            '5.2' => false,
114
                                            '5.3' => true
115
                                        ),
116
                                        'SplQueue' => array(
117
                                            '5.2' => false,
118
                                            '5.3' => true
119
                                        ),
120
                                        'SplStack' => array(
121
                                            '5.2' => false,
122
                                            '5.3' => true
123
                                        ),
124
125
                                        'CallbackFilterIterator' => array(
126
                                            '5.3' => false,
127
                                            '5.4' => true
128
                                        ),
129
                                        'RecursiveCallbackFilterIterator' => array(
130
                                            '5.3' => false,
131
                                            '5.4' => true
132
                                        ),
133
                                        'ReflectionZendExtension' => array(
134
                                            '5.3' => false,
135
                                            '5.4' => true
136
                                        ),
137
                                        'SessionHandler' => array(
138
                                            '5.3' => false,
139
                                            '5.4' => true
140
                                        ),
141
                                        'SNMP' => array(
142
                                            '5.3' => false,
143
                                            '5.4' => true
144
                                        ),
145
                                        'Transliterator' => array(
146
                                            '5.3' => false,
147
                                            '5.4' => true
148
                                        ),
149
                                        'Spoofchecker' => array(
150
                                            '5.3' => false,
151
                                            '5.4' => true
152
                                        ),
153
154
                                        'CURLFile' => array(
155
                                            '5.4' => false,
156
                                            '5.5' => true
157
                                        ),
158
                                        'DateTimeImmutable' => array(
159
                                            '5.4' => false,
160
                                            '5.5' => true
161
                                        ),
162
                                        'IntlCalendar' => array(
163
                                            '5.4' => false,
164
                                            '5.5' => true
165
                                        ),
166
                                        'IntlGregorianCalendar' => array(
167
                                            '5.4' => false,
168
                                            '5.5' => true
169
                                        ),
170
                                        'IntlTimeZone' => array(
171
                                            '5.4' => false,
172
                                            '5.5' => true
173
                                        ),
174
                                        'IntlBreakIterator' => array(
175
                                            '5.4' => false,
176
                                            '5.5' => true
177
                                        ),
178
                                        'IntlRuleBasedBreakIterator' => array(
179
                                            '5.4' => false,
180
                                            '5.5' => true
181
                                        ),
182
                                        'IntlCodePointBreakIterator' => array(
183
                                            '5.4' => false,
184
                                            '5.5' => true
185
                                        ),
186
187
                                    );
188
189
190
    /**
191
     * Returns an array of tokens this test wants to listen for.
192
     *
193
     * @return array
194
     */
195
    public function register()
196
    {
197
        // Handle case-insensitivity of class names.
198
        $this->newClasses = $this->arrayKeysToLowercase($this->newClasses);
199
200
        return array(
201
                T_NEW,
202
                T_CLASS,
203
                T_DOUBLE_COLON,
204
               );
205
206
    }//end register()
207
208
209
    /**
210
     * Processes this test, when one of its tokens is encountered.
211
     *
212
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
213
     * @param int                  $stackPtr  The position of the current token in
214
     *                                        the stack passed in $tokens.
215
     *
216
     * @return void
217
     */
218
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
219
    {
220
        $tokens      = $phpcsFile->getTokens();
221
        $FQClassName = '';
222
223
        if ($tokens[$stackPtr]['type'] === 'T_NEW') {
224
            $FQClassName = $this->getFQClassNameFromNewToken($phpcsFile, $stackPtr);
225
        }
226
        else if ($tokens[$stackPtr]['type'] === 'T_CLASS') {
227
            $FQClassName = $this->getFQExtendedClassName($phpcsFile, $stackPtr);
228
        }
229
        else if ($tokens[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
230
            $FQClassName = $this->getFQClassNameFromDoubleColonToken($phpcsFile, $stackPtr);
231
        }
232
233
        if ($FQClassName === '') {
234
            return;
235
        }
236
237
        if ($this->isNamespaced($FQClassName) === true) {
238
            return;
239
        }
240
241
        $className   = substr($FQClassName, 1); // Remove global namespace indicator.
242
        $classNameLc = strtolower($className);
243
244
        if (isset($this->newClasses[$classNameLc]) === false) {
245
            return;
246
        }
247
248
        $itemInfo = array(
249
            'name'   => $className,
250
            'nameLc' => $classNameLc,
251
        );
252
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
253
254
    }//end process()
255
256
257
    /**
258
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
259
     *
260
     * @param array $itemInfo Base information about the item.
261
     *
262
     * @return array Version and other information about the item.
263
     */
264
    public function getItemArray(array $itemInfo)
265
    {
266
        return $this->newClasses[$itemInfo['nameLc']];
267
    }
268
269
270
    /**
271
     * Get the error message template for this sniff.
272
     *
273
     * @return string
274
     */
275
    protected function getErrorMsgTemplate()
276
    {
277
        return 'The built-in class '.parent::getErrorMsgTemplate();
278
    }
279
280
281
}//end class
282