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

PHPCompatibility_Sniffs_PHP_NewClassesSniff::addError()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 12
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A PHPCompatibility_Sniffs_PHP_NewClassesSniff::getErrorMsgTemplate() 0 4 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 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 => 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
        // Handle case-insensitivity of class names.
197
        $this->newClasses = $this->arrayKeysToLowercase($this->newClasses);
198
199
        return array(
200
                T_NEW,
201
                T_CLASS,
202
                T_DOUBLE_COLON,
203
               );
204
205
    }//end register()
206
207
208
    /**
209
     * Processes this test, when one of its tokens is encountered.
210
     *
211
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
212
     * @param int                  $stackPtr  The position of the current token in
213
     *                                        the stack passed in $tokens.
214
     *
215
     * @return void
216
     */
217
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
218
    {
219
        $tokens      = $phpcsFile->getTokens();
220
        $FQClassName = '';
221
222
        if ($tokens[$stackPtr]['type'] === 'T_NEW') {
223
            $FQClassName = $this->getFQClassNameFromNewToken($phpcsFile, $stackPtr);
224
        }
225
        else if ($tokens[$stackPtr]['type'] === 'T_CLASS') {
226
            $FQClassName = $this->getFQExtendedClassName($phpcsFile, $stackPtr);
227
        }
228
        else if ($tokens[$stackPtr]['type'] === 'T_DOUBLE_COLON') {
229
            $FQClassName = $this->getFQClassNameFromDoubleColonToken($phpcsFile, $stackPtr);
230
        }
231
232
        if ($FQClassName === '') {
233
            return;
234
        }
235
236
        if ($this->isNamespaced($FQClassName) === true) {
237
            return;
238
        }
239
240
        $className   = substr($FQClassName, 1); // Remove global namespace indicator.
241
        $classNameLc = strtolower($className);
242
243
        if (isset($this->newClasses[$classNameLc]) === false) {
244
            return;
245
        }
246
247
        $itemInfo = array(
248
            'name'   => $className,
249
            'nameLc' => $classNameLc,
250
        );
251
        $this->handleFeature($phpcsFile, $stackPtr, $itemInfo);
252
253
    }//end process()
254
255
256
    /**
257
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
258
     *
259
     * @param array $itemInfo Base information about the item.
260
     *
261
     * @return array Version and other information about the item.
262
     */
263
    public function getItemArray(array $itemInfo)
264
    {
265
        return $this->newClasses[$itemInfo['nameLc']];
266
    }
267
268
269
    /**
270
     * Get the error message template for this sniff.
271
     *
272
     * @return string
273
     */
274
    protected function getErrorMsgTemplate()
275
    {
276
        return 'The built-in class '.parent::getErrorMsgTemplate();
277
    }
278
279
280
}//end class
281