Completed
Push — master ( b52690...50932b )
by Wim
02:29
created

DeprecatedFunctionsSniff   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 730
Duplicated Lines 9.04 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 1 Features 1
Metric Value
wmc 18
c 9
b 1
f 1
lcom 1
cbo 2
dl 66
loc 730
rs 5.2093

3 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 21 47 5
A register() 15 15 3
D addError() 30 42 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_DeprecatedFunctionsSniff.
4
 *
5
 * PHP version 7.0
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 */
11
12
/**
13
 * PHPCompatibility_Sniffs_PHP_DeprecatedFunctionsSniff.
14
 *
15
 * @category  PHP
16
 * @package   PHPCompatibility
17
 * @author    Wim Godden <[email protected]>
18
 */
19
class PHPCompatibility_Sniffs_PHP_DeprecatedFunctionsSniff 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...
20
{
21
22
    /**
23
     * If true, forbidden functions will be considered regular expressions.
24
     *
25
     * @var bool
26
     */
27
    protected $patternMatch = false;
28
29
    /**
30
     * A list of forbidden functions with their alternatives.
31
     *
32
     * The array lists : version number with 0 (deprecated) or 1 (forbidden) and an alternative function.
33
     * If no alternative exists, it is NULL. IE, the function should just not be used.
34
     *
35
     * @var array(string => array(string => int|string|null))
36
     */
37
    protected $forbiddenFunctions = array(
38
                                        'call_user_method' => array(
39
                                            '5.3' => false,
40
                                            '5.4' => false,
41
                                            '5.5' => false,
42
                                            '5.6' => false,
43
                                            '7.0' => true,
44
                                            'alternative' => 'call_user_func'
45
                                        ),
46
                                        'call_user_method_array' => array(
47
                                            '5.3' => false,
48
                                            '5.4' => false,
49
                                            '5.5' => false,
50
                                            '5.6' => false,
51
                                            '7.0' => true,
52
                                            'alternative' => 'call_user_func_array'
53
                                        ),
54
                                        'define_syslog_variables' => array(
55
                                            '5.3' => false,
56
                                            '5.4' => true,
57
                                            '5.5' => true,
58
                                            '5.6' => true,
59
                                            'alternative' => null
60
                                        ),
61
                                        'dl' => array(
62
                                            '5.3' => false,
63
                                            '5.4' => false,
64
                                            '5.5' => false,
65
                                            '5.6' => false,
66
                                            'alternative' => null
67
                                        ),
68
                                        'ereg' => array(
69
                                            '5.3' => false,
70
                                            '5.4' => false,
71
                                            '5.5' => false,
72
                                            '5.6' => false,
73
                                            'alternative' => 'preg_match'
74
                                        ),
75
                                        'ereg_replace' => array(
76
                                            '5.3' => false,
77
                                            '5.4' => false,
78
                                            '5.5' => false,
79
                                            '5.6' => false,
80
                                            'alternative' => 'preg_replace'
81
                                        ),
82
                                        'eregi' => array(
83
                                            '5.3' => false,
84
                                            '5.4' => false,
85
                                            '5.5' => false,
86
                                            '5.6' => false,
87
                                            'alternative' => 'preg_match'
88
                                        ),
89
                                        'eregi_replace' => array(
90
                                            '5.3' => false,
91
                                            '5.4' => false,
92
                                            '5.5' => false,
93
                                            '5.6' => false,
94
                                            'alternative' => 'preg_replace'
95
                                        ),
96
                                        'imagepsbbox' => array(
97
                                            '7.0' => true,
98
                                            'alternative' => null
99
                                        ),
100
                                        'imagepsencodefont' => array(
101
                                            '7.0' => true,
102
                                            'alternative' => null
103
                                        ),
104
                                        'imagepsextendfont' => array(
105
                                            '7.0' => true,
106
                                            'alternative' => null
107
                                        ),
108
                                        'imagepsfreefont' => array(
109
                                            '7.0' => true,
110
                                            'alternative' => null
111
                                        ),
112
                                        'imagepsloadfont' => array(
113
                                            '7.0' => true,
114
                                            'alternative' => null
115
                                        ),
116
                                        'imagepsslantfont' => array(
117
                                            '7.0' => true,
118
                                            'alternative' => null
119
                                        ),
120
                                        'imagepstext' => array(
121
                                            '7.0' => true,
122
                                            'alternative' => null
123
                                        ),
124
                                        'import_request_variables' => array(
125
                                            '5.4' => true,
126
                                            '5.5' => true,
127
                                            '5.6' => false,
128
                                            'alternative' => null
129
                                        ),
130
                                        'ldap_sort' => array(
131
                                            '7.0' => false,
132
                                            'alternative' => null
133
                                        ),
134
                                        'mcrypt_generic_end' => array(
135
                                            '5.4' => false,
136
                                            '5.5' => false,
137
                                            '5.6' => false,
138
                                            '7.0' => true,
139
                                            'alternative' => 'mcrypt_generic_deinit'
140
                                        ),
141
                                        'mysql_db_query' => array(
142
                                            '5.3' => false,
143
                                            '5.4' => false,
144
                                            '5.5' => false,
145
                                            '5.6' => false,
146
                                            'alternative' => 'mysql_select_db and mysql_query'
147
                                        ),
148
                                        'mysql_escape_string' => array(
149
                                            '5.3' => false,
150
                                            '5.4' => false,
151
                                            '5.5' => false,
152
                                            '5.6' => false,
153
                                            'alternative' => 'mysql_real_escape_string'
154
                                        ),
155
                                        'mysql_list_dbs' => array(
156
                                            '5.4' => false,
157
                                            '5.5' => false,
158
                                            '5.6' => false,
159
                                            'alternative' => null
160
                                        ),
161
                                        'mysqli_bind_param' => array(
162
                                            '5.4' => true,
163
                                            '5.5' => true,
164
                                            '5.6' => true,
165
                                            'alternative' => 'mysqli_stmt_bind_param'
166
                                        ),
167
                                        'mysqli_bind_result' => array(
168
                                            '5.4' => true,
169
                                            '5.5' => true,
170
                                            '5.6' => true,
171
                                            'alternative' => 'mysqli_stmt_bind_result'
172
                                        ),
173
                                        'mysqli_client_encoding' => array(
174
                                            '5.4' => true,
175
                                            '5.5' => true,
176
                                            '5.6' => true,
177
                                            'alternative' => 'mysqli_character_set_name'
178
                                        ),
179
                                        'mysqli_fetch' => array(
180
                                            '5.4' => true,
181
                                            '5.5' => true,
182
                                            '5.6' => true,
183
                                            'alternative' => 'mysqli_stmt_fetch'
184
                                        ),
185
                                        'mysqli_param_count' => array(
186
                                            '5.4' => true,
187
                                            '5.5' => true,
188
                                            '5.6' => true,
189
                                            'alternative' => 'mysqli_stmt_param_count'
190
                                        ),
191
                                        'mysqli_get_metadata' => array(
192
                                            '5.4' => true,
193
                                            '5.5' => true,
194
                                            '5.6' => true,
195
                                            'alternative' => 'mysqli_stmt_result_metadata'
196
                                        ),
197
                                        'mysqli_send_long_data' => array(
198
                                            '5.4' => true,
199
                                            '5.5' => true,
200
                                            '5.6' => true,
201
                                            'alternative' => 'mysqli_stmt_send_long_data'
202
                                        ),
203
                                        'magic_quotes_runtime' => array(
204
                                            '5.3' => false,
205
                                            '5.4' => false,
206
                                            '5.5' => false,
207
                                            '5.6' => false,
208
                                            '7.0' => true,
209
                                            'alternative' => null
210
                                        ),
211
                                        'session_register' => array(
212
                                            '5.3' => false,
213
                                            '5.4' => true,
214
                                            '5.5' => true,
215
                                            '5.6' => true,
216
                                            'alternative' => '$_SESSION'
217
                                        ),
218
                                        'session_unregister' => array(
219
                                            '5.3' => false,
220
                                            '5.4' => true,
221
                                            '5.5' => true,
222
                                            '5.6' => true,
223
                                            'alternative' => '$_SESSION'
224
                                        ),
225
                                        'session_is_registered' => array(
226
                                            '5.3' => false,
227
                                            '5.4' => true,
228
                                            '5.5' => true,
229
                                            '5.6' => true,
230
                                            'alternative' => '$_SESSION'
231
                                        ),
232
                                        'set_magic_quotes_runtime' => array(
233
                                            '5.3' => false,
234
                                            '5.4' => false,
235
                                            '5.5' => false,
236
                                            '5.6' => false,
237
                                            '7.0' => true,
238
                                            'alternative' => null
239
                                        ),
240
                                        'set_socket_blocking' => array(
241
                                            '5.3' => false,
242
                                            '5.4' => false,
243
                                            '5.5' => false,
244
                                            '5.6' => false,
245
                                            '7.0' => true,
246
                                            'alternative' => 'stream_set_blocking'
247
                                        ),
248
                                        'split' => array(
249
                                            '5.3' => false,
250
                                            '5.4' => false,
251
                                            '5.5' => false,
252
                                            '5.6' => false,
253
                                            'alternative' => 'preg_split'
254
                                        ),
255
                                        'spliti' => array(
256
                                            '5.3' => false,
257
                                            '5.4' => false,
258
                                            '5.5' => false,
259
                                            '5.6' => false,
260
                                            'alternative' => 'preg_split'
261
                                        ),
262
                                        'sql_regcase' => array(
263
                                            '5.3' => false,
264
                                            '5.4' => false,
265
                                            '5.5' => false,
266
                                            '5.6' => false,
267
                                            'alternative' => null
268
                                        ),
269
                                        'php_logo_guid' => array(
270
                                            '5.5' => true,
271
                                            '5.6' => true,
272
                                            'alternative' => null
273
                                        ),
274
                                        'php_egg_logo_guid' => array(
275
                                            '5.5' => true,
276
                                            '5.6' => true,
277
                                            'alternative' => null
278
                                        ),
279
                                        'php_real_logo_guid' => array(
280
                                            '5.5' => true,
281
                                            '5.6' => true,
282
                                            'alternative' => null
283
                                        ),
284
                                        'zend_logo_guid' => array(
285
                                            '5.5' => true,
286
                                            '5.6' => true,
287
                                            'alternative' => null
288
                                        ),
289
                                        'datefmt_set_timezone_id' => array(
290
                                            '5.5' => false,
291
                                            '5.6' => false,
292
                                            '7.0' => true,
293
                                            'alternative' => 'datefmt_set_timezone'
294
                                        ),
295
                                        'mcrypt_ecb' => array(
296
                                            '5.5' => false,
297
                                            '5.6' => false,
298
                                            '7.0' => true,
299
                                            'alternative' => null
300
                                        ),
301
                                        'mcrypt_cbc' => array(
302
                                            '5.5' => false,
303
                                            '5.6' => false,
304
                                            '7.0' => true,
305
                                            'alternative' => null
306
                                        ),
307
                                        'mcrypt_cfb' => array(
308
                                            '5.5' => false,
309
                                            '5.6' => false,
310
                                            '7.0' => true,
311
                                            'alternative' => null
312
                                        ),
313
                                        'mcrypt_ofb' => array(
314
                                            '5.5' => false,
315
                                            '5.6' => false,
316
                                            '7.0' => true,
317
                                            'alternative' => null
318
                                        ),
319
                                        'ocibindbyname' => array(
320
                                            '5.4' => false,
321
                                            '5.5' => false,
322
                                            '5.6' => false,
323
                                            'alternative' => 'oci_bind_by_name'
324
                                        ),
325
                                        'ocicancel' => array(
326
                                            '5.4' => false,
327
                                            '5.5' => false,
328
                                            '5.6' => false,
329
                                            'alternative' => 'oci_cancel'
330
                                        ),
331
                                        'ocicloselob' => array(
332
                                            '5.4' => false,
333
                                            '5.5' => false,
334
                                            '5.6' => false,
335
                                            'alternative' => 'OCI-Lob::close'
336
                                        ),
337
                                        'ocicollappend' => array(
338
                                            '5.4' => false,
339
                                            '5.5' => false,
340
                                            '5.6' => false,
341
                                            'alternative' => 'OCI-Collection::append'
342
                                        ),
343
                                        'ocicollassign' => array(
344
                                            '5.4' => false,
345
                                            '5.5' => false,
346
                                            '5.6' => false,
347
                                            'alternative' => 'OCI-Collection::assign'
348
                                        ),
349
                                        'ocicollassignelem' => array(
350
                                            '5.4' => false,
351
                                            '5.5' => false,
352
                                            '5.6' => false,
353
                                            'alternative' => 'OCI-Collection::assignElem'
354
                                        ),
355
                                        'ocicollgetelem' => array(
356
                                            '5.4' => false,
357
                                            '5.5' => false,
358
                                            '5.6' => false,
359
                                            'alternative' => 'OCI-Collection::getElem'
360
                                        ),
361
                                        'ocicollmax' => array(
362
                                            '5.4' => false,
363
                                            '5.5' => false,
364
                                            '5.6' => false,
365
                                            'alternative' => 'OCI-Collection::max'
366
                                        ),
367
                                        'ocicollsize' => array(
368
                                            '5.4' => false,
369
                                            '5.5' => false,
370
                                            '5.6' => false,
371
                                            'alternative' => 'OCI-Collection::size'
372
                                        ),
373
                                        'ocicolltrim' => array(
374
                                            '5.4' => false,
375
                                            '5.5' => false,
376
                                            '5.6' => false,
377
                                            'alternative' => 'OCI-Collection::trim'
378
                                        ),
379
                                        'ocicolumnisnull' => array(
380
                                            '5.4' => false,
381
                                            '5.5' => false,
382
                                            '5.6' => false,
383
                                            'alternative' => 'oci_field_is_null'
384
                                        ),
385
                                        'ocicolumnname' => array(
386
                                            '5.4' => false,
387
                                            '5.5' => false,
388
                                            '5.6' => false,
389
                                            'alternative' => 'oci_field_name'
390
                                        ),
391
                                        'ocicolumnprecision' => array(
392
                                            '5.4' => false,
393
                                            '5.5' => false,
394
                                            '5.6' => false,
395
                                            'alternative' => 'oci_field_precision'
396
                                        ),
397
                                        'ocicolumnscale' => array(
398
                                            '5.4' => false,
399
                                            '5.5' => false,
400
                                            '5.6' => false,
401
                                            'alternative' => 'oci_field_scale'
402
                                        ),
403
                                        'ocicolumnsize' => array(
404
                                            '5.4' => false,
405
                                            '5.5' => false,
406
                                            '5.6' => false,
407
                                            'alternative' => 'oci_field_size'
408
                                        ),
409
                                        'ocicolumntype' => array(
410
                                            '5.4' => false,
411
                                            '5.5' => false,
412
                                            '5.6' => false,
413
                                            'alternative' => 'oci_field_type'
414
                                        ),
415
                                        'ocicolumntyperaw' => array(
416
                                            '5.4' => false,
417
                                            '5.5' => false,
418
                                            '5.6' => false,
419
                                            'alternative' => 'oci_field_type_raw'
420
                                        ),
421
                                        'ocicommit' => array(
422
                                            '5.4' => false,
423
                                            '5.5' => false,
424
                                            '5.6' => false,
425
                                            'alternative' => 'oci_commit'
426
                                        ),
427
                                        'ocidefinebyname' => array(
428
                                            '5.4' => false,
429
                                            '5.5' => false,
430
                                            '5.6' => false,
431
                                            'alternative' => 'oci_define_by_name'
432
                                        ),
433
                                        'ocierror' => array(
434
                                            '5.4' => false,
435
                                            '5.5' => false,
436
                                            '5.6' => false,
437
                                            'alternative' => 'oci_error'
438
                                        ),
439
                                        'ociexecute' => array(
440
                                            '5.4' => false,
441
                                            '5.5' => false,
442
                                            '5.6' => false,
443
                                            'alternative' => 'oci_execute'
444
                                        ),
445
                                        'ocifetch' => array(
446
                                            '5.4' => false,
447
                                            '5.5' => false,
448
                                            '5.6' => false,
449
                                            'alternative' => 'oci_fetch'
450
                                        ),
451
                                        'ocifetchinto' => array(
452
                                            '5.4' => false,
453
                                            '5.5' => false,
454
                                            '5.6' => false,
455
                                            'alternative' => null
456
                                        ),
457
                                        'ocifetchstatement' => array(
458
                                            '5.4' => false,
459
                                            '5.5' => false,
460
                                            '5.6' => false,
461
                                            'alternative' => 'oci_fetch_all'
462
                                        ),
463
                                        'ocifreecollection' => array(
464
                                            '5.4' => false,
465
                                            '5.5' => false,
466
                                            '5.6' => false,
467
                                            'alternative' => 'OCI-Collection::free'
468
                                        ),
469
                                        'ocifreecursor' => array(
470
                                            '5.4' => false,
471
                                            '5.5' => false,
472
                                            '5.6' => false,
473
                                            'alternative' => 'oci_free_statement'
474
                                        ),
475
                                        'ocifreedesc' => array(
476
                                            '5.4' => false,
477
                                            '5.5' => false,
478
                                            '5.6' => false,
479
                                            'alternative' => 'OCI-Lob::free'
480
                                        ),
481
                                        'ocifreestatement' => array(
482
                                            '5.4' => false,
483
                                            '5.5' => false,
484
                                            '5.6' => false,
485
                                            'alternative' => 'oci_free_statement'
486
                                        ),
487
                                        'ociinternaldebug' => array(
488
                                            '5.4' => false,
489
                                            '5.5' => false,
490
                                            '5.6' => false,
491
                                            'alternative' => 'oci_internal_debug'
492
                                        ),
493
                                        'ociloadlob' => array(
494
                                            '5.4' => false,
495
                                            '5.5' => false,
496
                                            '5.6' => false,
497
                                            'alternative' => 'OCI-Lob::load'
498
                                        ),
499
                                        'ocilogoff' => array(
500
                                            '5.4' => false,
501
                                            '5.5' => false,
502
                                            '5.6' => false,
503
                                            'alternative' => 'oci_close'
504
                                        ),
505
                                        'ocilogon' => array(
506
                                            '5.4' => false,
507
                                            '5.5' => false,
508
                                            '5.6' => false,
509
                                            'alternative' => 'oci_connect'
510
                                        ),
511
                                        'ocinewcollection' => array(
512
                                            '5.4' => false,
513
                                            '5.5' => false,
514
                                            '5.6' => false,
515
                                            'alternative' => 'oci_new_collection'
516
                                        ),
517
                                        'ocinewcursor' => array(
518
                                            '5.4' => false,
519
                                            '5.5' => false,
520
                                            '5.6' => false,
521
                                            'alternative' => 'oci_new_cursor'
522
                                        ),
523
                                        'ocinewdescriptor' => array(
524
                                            '5.4' => false,
525
                                            '5.5' => false,
526
                                            '5.6' => false,
527
                                            'alternative' => 'oci_new_descriptor'
528
                                        ),
529
                                        'ocinlogon' => array(
530
                                            '5.4' => false,
531
                                            '5.5' => false,
532
                                            '5.6' => false,
533
                                            'alternative' => 'oci_new_connect'
534
                                        ),
535
                                        'ocinumcols' => array(
536
                                            '5.4' => false,
537
                                            '5.5' => false,
538
                                            '5.6' => false,
539
                                            'alternative' => 'oci_num_fields'
540
                                        ),
541
                                        'ociparse' => array(
542
                                            '5.4' => false,
543
                                            '5.5' => false,
544
                                            '5.6' => false,
545
                                            'alternative' => 'oci_parse'
546
                                        ),
547
                                        'ociplogon' => array(
548
                                            '5.4' => false,
549
                                            '5.5' => false,
550
                                            '5.6' => false,
551
                                            'alternative' => 'oci_pconnect'
552
                                        ),
553
                                        'ociresult' => array(
554
                                            '5.4' => false,
555
                                            '5.5' => false,
556
                                            '5.6' => false,
557
                                            'alternative' => 'oci_result'
558
                                        ),
559
                                        'ocirollback' => array(
560
                                            '5.4' => false,
561
                                            '5.5' => false,
562
                                            '5.6' => false,
563
                                            'alternative' => 'oci_rollback'
564
                                        ),
565
                                        'ocirowcount' => array(
566
                                            '5.4' => false,
567
                                            '5.5' => false,
568
                                            '5.6' => false,
569
                                            'alternative' => 'oci_num_rows'
570
                                        ),
571
                                        'ocisavelob' => array(
572
                                            '5.4' => false,
573
                                            '5.5' => false,
574
                                            '5.6' => false,
575
                                            'alternative' => 'OCI-Lob::save'
576
                                        ),
577
                                        'ocisavelobfile' => array(
578
                                            '5.4' => false,
579
                                            '5.5' => false,
580
                                            '5.6' => false,
581
                                            'alternative' => 'OCI-Lob::import'
582
                                        ),
583
                                        'ociserverversion' => array(
584
                                            '5.4' => false,
585
                                            '5.5' => false,
586
                                            '5.6' => false,
587
                                            'alternative' => 'oci_server_version'
588
                                        ),
589
                                        'ocisetprefetch' => array(
590
                                            '5.4' => false,
591
                                            '5.5' => false,
592
                                            '5.6' => false,
593
                                            'alternative' => 'oci_set_prefetch'
594
                                        ),
595
                                        'ocistatementtype' => array(
596
                                            '5.4' => false,
597
                                            '5.5' => false,
598
                                            '5.6' => false,
599
                                            'alternative' => 'oci_statement_type'
600
                                        ),
601
                                        'ociwritelobtofile' => array(
602
                                            '5.4' => false,
603
                                            '5.5' => false,
604
                                            '5.6' => false,
605
                                            'alternative' => 'OCI-Lob::export'
606
                                        ),
607
                                        'ociwritetemporarylob' => array(
608
                                            '5.4' => false,
609
                                            '5.5' => false,
610
                                            '5.6' => false,
611
                                            'alternative' => 'OCI-Lob::writeTemporary'
612
                                        ),
613
                                    );
614
615
    /**
616
     * Returns an array of tokens this test wants to listen for.
617
     *
618
     * @return array
619
     */
620 View Code Duplication
    public function register()
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...
621
    {
622
        // Everyone has had a chance to figure out what forbidden functions
623
        // they want to check for, so now we can cache out the list.
624
        $this->forbiddenFunctionNames = array_keys($this->forbiddenFunctions);
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
625
626
        if ($this->patternMatch === true) {
627
            foreach ($this->forbiddenFunctionNames as $i => $name) {
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
628
                $this->forbiddenFunctionNames[$i] = '/'.$name.'/i';
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
629
            }
630
        }
631
632
        return array(T_STRING);
633
634
    }//end register()
635
636
637
    /**
638
     * Processes this test, when one of its tokens is encountered.
639
     *
640
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
641
     * @param int                  $stackPtr  The position of the current token in
642
     *                                        the stack passed in $tokens.
643
     *
644
     * @return void
645
     */
646
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
647
    {
648
        $tokens = $phpcsFile->getTokens();
649
650
        $ignore = array(
651
                T_DOUBLE_COLON,
652
                T_OBJECT_OPERATOR,
653
                T_FUNCTION,
654
                T_CONST,
655
                T_USE,
656
                T_NS_SEPARATOR,
657
        );
658
659
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
660
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
661
            // Not a call to a PHP function.
662
            return;
663
        }
664
665
        $function = strtolower($tokens[$stackPtr]['content']);
666
        $pattern  = null;
667
668 View Code Duplication
        if ($this->patternMatch === true) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
669
            $count   = 0;
670
            $pattern = preg_replace(
671
                    $this->forbiddenFunctionNames,
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
672
                    $this->forbiddenFunctionNames,
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
673
                    $function,
674
                    1,
675
                    $count
676
            );
677
678
            if ($count === 0) {
679
                return;
680
            }
681
682
            // Remove the pattern delimiters and modifier.
683
            $pattern = substr($pattern, 1, -2);
684
        } else {
685
            if (in_array($function, $this->forbiddenFunctionNames) === false) {
0 ignored issues
show
Bug introduced by
The property forbiddenFunctionNames does not seem to exist. Did you mean forbiddenFunctions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
686
                return;
687
            }
688
        }
689
690
        $this->addError($phpcsFile, $stackPtr, $function, $pattern);
691
692
    }//end process()
693
694
    /**
695
     * Generates the error or warning for this sniff.
696
     *
697
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
698
     * @param int                  $stackPtr  The position of the forbidden function
699
     *                                        in the token array.
700
     * @param string               $function  The name of the forbidden function.
701
     * @param string               $pattern   The pattern used for the match.
702
     *
703
     * @return void
704
     */
705
    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
706
    {
707
        if ($pattern === null) {
708
            $pattern = $function;
709
        }
710
711
        $error = '';
712
713
        $isError = false;
714
        $previousVersionStatus = null;
715 View Code Duplication
        foreach ($this->forbiddenFunctions[$pattern] as $version => $forbidden) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
716
            if ($this->supportsAbove($version)) {
717
                if ($version != 'alternative') {
718
                    if ($previousVersionStatus !== $forbidden) {
719
                        $previousVersionStatus = $forbidden;
720
                        if ($forbidden === true) {
721
                            $isError = true;
722
                            $error .= 'forbidden';
723
                        } else {
724
                            $error .= 'discouraged';
725
                        }
726
                        $error .=  ' from PHP version ' . $version . ' and ';
727
                    }
728
                }
729
            }
730
        }
731 View Code Duplication
        if (strlen($error) > 0) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
732
            $error = 'The use of function ' . $function . ' is ' . $error;
733
            $error = substr($error, 0, strlen($error) - 5);
734
735
            if ($this->forbiddenFunctions[$pattern]['alternative'] !== null) {
736
                $error .= '; use ' . $this->forbiddenFunctions[$pattern]['alternative'] . ' instead';
737
            }
738
739
            if ($isError === true) {
740
                $phpcsFile->addError($error, $stackPtr);
741
            } else {
742
                $phpcsFile->addWarning($error, $stackPtr);
743
            }
744
        }
745
746
    }//end addError()
747
748
}//end class
749