Completed
Push — php70 ( b6aa8a...86ce4b )
by Wim
02:44
created

DeprecatedFunctionsSniff   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 733
Duplicated Lines 6.28 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 15
Bugs 4 Features 3
Metric Value
c 15
b 4
f 3
dl 46
loc 733
rs 6.7878
wmc 18
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 15 15 3
B process() 21 47 5
D addError() 10 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
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
    public $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
                                        'mcrypt_generic_end' => array(
131
                                            '5.4' => false,
132
                                            '5.5' => false,
133
                                            '5.6' => false,
134
                                            '7.0' => true,
135
                                            'alternative' => 'mcrypt_generic_deinit'
136
                                        ),
137
                                        'mysql_db_query' => array(
138
                                            '5.3' => false,
139
                                            '5.4' => false,
140
                                            '5.5' => false,
141
                                            '5.6' => false,
142
                                            'alternative' => 'mysql_select_db and mysql_query'
143
                                        ),
144
                                        'mysql_escape_string' => array(
145
                                            '5.3' => false,
146
                                            '5.4' => false,
147
                                            '5.5' => false,
148
                                            '5.6' => false,
149
                                            'alternative' => 'mysql_real_escape_string'
150
                                        ),
151
                                        'mysql_list_dbs' => array(
152
                                            '5.4' => false,
153
                                            '5.5' => false,
154
                                            '5.6' => false,
155
                                            'alternative' => null
156
                                        ),
157
                                        'mysqli_bind_param' => array(
158
                                            '5.4' => true,
159
                                            '5.5' => true,
160
                                            '5.6' => true,
161
                                            'alternative' => 'mysqli_stmt_bind_param'
162
                                        ),
163
                                        'mysqli_bind_result' => array(
164
                                            '5.4' => true,
165
                                            '5.5' => true,
166
                                            '5.6' => true,
167
                                            'alternative' => 'mysqli_stmt_bind_result'
168
                                        ),
169
                                        'mysqli_client_encoding' => array(
170
                                            '5.4' => true,
171
                                            '5.5' => true,
172
                                            '5.6' => true,
173
                                            'alternative' => 'mysqli_character_set_name'
174
                                        ),
175
                                        'mysqli_fetch' => array(
176
                                            '5.4' => true,
177
                                            '5.5' => true,
178
                                            '5.6' => true,
179
                                            'alternative' => 'mysqli_stmt_fetch'
180
                                        ),
181
                                        'mysqli_param_count' => array(
182
                                            '5.4' => true,
183
                                            '5.5' => true,
184
                                            '5.6' => true,
185
                                            'alternative' => 'mysqli_stmt_param_count'
186
                                        ),
187
                                        'mysqli_get_metadata' => array(
188
                                            '5.4' => true,
189
                                            '5.5' => true,
190
                                            '5.6' => true,
191
                                            'alternative' => 'mysqli_stmt_result_metadata'
192
                                        ),
193
                                        'mysqli_send_long_data' => array(
194
                                            '5.4' => true,
195
                                            '5.5' => true,
196
                                            '5.6' => true,
197
                                            'alternative' => 'mysqli_stmt_send_long_data'
198
                                        ),
199
                                        'magic_quotes_runtime' => array(
200
                                            '5.3' => false,
201
                                            '5.4' => false,
202
                                            '5.5' => false,
203
                                            '5.6' => false,
204
                                            '7.0' => true,
205
                                            'alternative' => null
206
                                        ),
207
                                        'session_register' => array(
208
                                            '5.3' => false,
209
                                            '5.4' => true,
210
                                            '5.5' => true,
211
                                            '5.6' => true,
212
                                            'alternative' => '$_SESSION'
213
                                        ),
214
                                        'session_unregister' => array(
215
                                            '5.3' => false,
216
                                            '5.4' => true,
217
                                            '5.5' => true,
218
                                            '5.6' => true,
219
                                            'alternative' => '$_SESSION'
220
                                        ),
221
                                        'session_is_registered' => array(
222
                                            '5.3' => false,
223
                                            '5.4' => true,
224
                                            '5.5' => true,
225
                                            '5.6' => true,
226
                                            'alternative' => '$_SESSION'
227
                                        ),
228
                                        'set_magic_quotes_runtime' => array(
229
                                            '5.3' => false,
230
                                            '5.4' => false,
231
                                            '5.5' => false,
232
                                            '5.6' => false,
233
                                            '7.0' => true,
234
                                            'alternative' => null
235
                                        ),
236
                                        'set_socket_blocking' => array(
237
                                            '5.3' => false,
238
                                            '5.4' => false,
239
                                            '5.5' => false,
240
                                            '5.6' => false,
241
                                            '7.0' => true,
242
                                            'alternative' => 'stream_set_blocking'
243
                                        ),
244
                                        'split' => array(
245
                                            '5.3' => false,
246
                                            '5.4' => false,
247
                                            '5.5' => false,
248
                                            '5.6' => false,
249
                                            'alternative' => 'preg_split'
250
                                        ),
251
                                        'spliti' => array(
252
                                            '5.3' => false,
253
                                            '5.4' => false,
254
                                            '5.5' => false,
255
                                            '5.6' => false,
256
                                            'alternative' => 'preg_split'
257
                                        ),
258
                                        'sql_regcase' => array(
259
                                            '5.3' => false,
260
                                            '5.4' => false,
261
                                            '5.5' => false,
262
                                            '5.6' => false,
263
                                            'alternative' => null
264
                                        ),
265
                                        'php_logo_guid' => array(
266
                                            '5.5' => true,
267
                                            '5.6' => true,
268
                                            'alternative' => null
269
                                        ),
270
                                        'php_egg_logo_guid' => array(
271
                                            '5.5' => true,
272
                                            '5.6' => true,
273
                                            'alternative' => null
274
                                        ),
275
                                        'php_real_logo_guid' => array(
276
                                            '5.5' => true,
277
                                            '5.6' => true,
278
                                            'alternative' => null
279
                                        ),
280
                                        'zend_logo_guid' => array(
281
                                            '5.5' => true,
282
                                            '5.6' => true,
283
                                            'alternative' => null
284
                                        ),
285
                                        'datefmt_set_timezone_id' => array(
286
                                            '5.5' => false,
287
                                            '5.6' => false,
288
                                            '7.0' => true,
289
                                            'alternative' => 'datefmt_set_timezone'
290
                                        ),
291
                                        'mcrypt_ecb' => array(
292
                                            '5.5' => false,
293
                                            '5.6' => false,
294
                                            '7.0' => true,
295
                                            'alternative' => null
296
                                        ),
297
                                        'mcrypt_cbc' => array(
298
                                            '5.5' => false,
299
                                            '5.6' => false,
300
                                            '7.0' => true,
301
                                            'alternative' => null
302
                                        ),
303
                                        'mcrypt_cfb' => array(
304
                                            '5.5' => false,
305
                                            '5.6' => false,
306
                                            '7.0' => true,
307
                                            'alternative' => null
308
                                        ),
309
                                        'mcrypt_ofb' => array(
310
                                            '5.5' => false,
311
                                            '5.6' => false,
312
                                            '7.0' => true,
313
                                            'alternative' => null
314
                                        ),
315
                                        'ocibindbyname' => array(
316
                                            '5.4' => false,
317
                                            '5.5' => false,
318
                                            '5.6' => false,
319
                                            'alternative' => 'oci_bind_by_name'
320
                                        ),
321
                                        'ocicancel' => array(
322
                                            '5.4' => false,
323
                                            '5.5' => false,
324
                                            '5.6' => false,
325
                                            'alternative' => 'oci_cancel'
326
                                        ),
327
                                        'ocicloselob' => array(
328
                                            '5.4' => false,
329
                                            '5.5' => false,
330
                                            '5.6' => false,
331
                                            'alternative' => 'OCI-Lob::close'
332
                                        ),
333
                                        'ocicollappend' => array(
334
                                            '5.4' => false,
335
                                            '5.5' => false,
336
                                            '5.6' => false,
337
                                            'alternative' => 'OCI-Collection::append'
338
                                        ),
339
                                        'ocicollassign' => array(
340
                                            '5.4' => false,
341
                                            '5.5' => false,
342
                                            '5.6' => false,
343
                                            'alternative' => 'OCI-Collection::assign'
344
                                        ),
345
                                        'ocicollassignelem' => array(
346
                                            '5.4' => false,
347
                                            '5.5' => false,
348
                                            '5.6' => false,
349
                                            'alternative' => 'OCI-Collection::assignElem'
350
                                        ),
351
                                        'ocicollgetelem' => array(
352
                                            '5.4' => false,
353
                                            '5.5' => false,
354
                                            '5.6' => false,
355
                                            'alternative' => 'OCI-Collection::getElem'
356
                                        ),
357
                                        'ocicollmax' => array(
358
                                            '5.4' => false,
359
                                            '5.5' => false,
360
                                            '5.6' => false,
361
                                            'alternative' => 'OCI-Collection::max'
362
                                        ),
363
                                        'ocicollsize' => array(
364
                                            '5.4' => false,
365
                                            '5.5' => false,
366
                                            '5.6' => false,
367
                                            'alternative' => 'OCI-Collection::size'
368
                                        ),
369
                                        'ocicolltrim' => array(
370
                                            '5.4' => false,
371
                                            '5.5' => false,
372
                                            '5.6' => false,
373
                                            'alternative' => 'OCI-Collection::trim'
374
                                        ),
375
                                        'ocicolumnisnull' => array(
376
                                            '5.4' => false,
377
                                            '5.5' => false,
378
                                            '5.6' => false,
379
                                            'alternative' => 'oci_field_is_null'
380
                                        ),
381
                                        'ocicolumnname' => array(
382
                                            '5.4' => false,
383
                                            '5.5' => false,
384
                                            '5.6' => false,
385
                                            'alternative' => 'oci_field_name'
386
                                        ),
387
                                        'ocicolumnprecision' => array(
388
                                            '5.4' => false,
389
                                            '5.5' => false,
390
                                            '5.6' => false,
391
                                            'alternative' => 'oci_field_precision'
392
                                        ),
393
                                        'ocicolumnscale' => array(
394
                                            '5.4' => false,
395
                                            '5.5' => false,
396
                                            '5.6' => false,
397
                                            'alternative' => 'oci_field_scale'
398
                                        ),
399
                                        'ocicolumnsize' => array(
400
                                            '5.4' => false,
401
                                            '5.5' => false,
402
                                            '5.6' => false,
403
                                            'alternative' => 'oci_field_size'
404
                                        ),
405
                                        'ocicolumntype' => array(
406
                                            '5.4' => false,
407
                                            '5.5' => false,
408
                                            '5.6' => false,
409
                                            'alternative' => 'oci_field_type'
410
                                        ),
411
                                        'ocicolumntyperaw' => array(
412
                                            '5.4' => false,
413
                                            '5.5' => false,
414
                                            '5.6' => false,
415
                                            'alternative' => 'oci_field_type_raw'
416
                                        ),
417
                                        'ocicommit' => array(
418
                                            '5.4' => false,
419
                                            '5.5' => false,
420
                                            '5.6' => false,
421
                                            'alternative' => 'oci_commit'
422
                                        ),
423
                                        'ocidefinebyname' => array(
424
                                            '5.4' => false,
425
                                            '5.5' => false,
426
                                            '5.6' => false,
427
                                            'alternative' => 'oci_define_by_name'
428
                                        ),
429
                                        'ocierror' => array(
430
                                            '5.4' => false,
431
                                            '5.5' => false,
432
                                            '5.6' => false,
433
                                            'alternative' => 'oci_error'
434
                                        ),
435
                                        'ociexecute' => array(
436
                                            '5.4' => false,
437
                                            '5.5' => false,
438
                                            '5.6' => false,
439
                                            'alternative' => 'oci_execute'
440
                                        ),
441
                                        'ocifetch' => array(
442
                                            '5.4' => false,
443
                                            '5.5' => false,
444
                                            '5.6' => false,
445
                                            'alternative' => 'oci_fetch'
446
                                        ),
447
                                        'ocifetchinto' => array(
448
                                            '5.4' => false,
449
                                            '5.5' => false,
450
                                            '5.6' => false,
451
                                            'alternative' => null
452
                                        ),
453
                                        'ocifetchstatement' => array(
454
                                            '5.4' => false,
455
                                            '5.5' => false,
456
                                            '5.6' => false,
457
                                            'alternative' => 'oci_fetch_all'
458
                                        ),
459
                                        'ocifreecollection' => array(
460
                                            '5.4' => false,
461
                                            '5.5' => false,
462
                                            '5.6' => false,
463
                                            'alternative' => 'OCI-Collection::free'
464
                                        ),
465
                                        'ocifreecursor' => array(
466
                                            '5.4' => false,
467
                                            '5.5' => false,
468
                                            '5.6' => false,
469
                                            'alternative' => 'oci_free_statement'
470
                                        ),
471
                                        'ocifreedesc' => array(
472
                                            '5.4' => false,
473
                                            '5.5' => false,
474
                                            '5.6' => false,
475
                                            'alternative' => 'OCI-Lob::free'
476
                                        ),
477
                                        'ocifreestatement' => array(
478
                                            '5.4' => false,
479
                                            '5.5' => false,
480
                                            '5.6' => false,
481
                                            'alternative' => 'oci_free_statement'
482
                                        ),
483
                                        'ociinternaldebug' => array(
484
                                            '5.4' => false,
485
                                            '5.5' => false,
486
                                            '5.6' => false,
487
                                            'alternative' => 'oci_internal_debug'
488
                                        ),
489
                                        'ociloadlob' => array(
490
                                            '5.4' => false,
491
                                            '5.5' => false,
492
                                            '5.6' => false,
493
                                            'alternative' => 'OCI-Lob::load'
494
                                        ),
495
                                        'ocilogoff' => array(
496
                                            '5.4' => false,
497
                                            '5.5' => false,
498
                                            '5.6' => false,
499
                                            'alternative' => 'oci_close'
500
                                        ),
501
                                        'ocilogon' => array(
502
                                            '5.4' => false,
503
                                            '5.5' => false,
504
                                            '5.6' => false,
505
                                            'alternative' => 'oci_connect'
506
                                        ),
507
                                        'ocinewcollection' => array(
508
                                            '5.4' => false,
509
                                            '5.5' => false,
510
                                            '5.6' => false,
511
                                            'alternative' => 'oci_new_collection'
512
                                        ),
513
                                        'ocinewcursor' => array(
514
                                            '5.4' => false,
515
                                            '5.5' => false,
516
                                            '5.6' => false,
517
                                            'alternative' => 'oci_new_cursor'
518
                                        ),
519
                                        'ocinewdescriptor' => array(
520
                                            '5.4' => false,
521
                                            '5.5' => false,
522
                                            '5.6' => false,
523
                                            'alternative' => 'oci_new_descriptor'
524
                                        ),
525
                                        'ocinlogon' => array(
526
                                            '5.4' => false,
527
                                            '5.5' => false,
528
                                            '5.6' => false,
529
                                            'alternative' => 'oci_new_connect'
530
                                        ),
531
                                        'ocinumcols' => array(
532
                                            '5.4' => false,
533
                                            '5.5' => false,
534
                                            '5.6' => false,
535
                                            'alternative' => 'oci_num_fields'
536
                                        ),
537
                                        'ociparse' => array(
538
                                            '5.4' => false,
539
                                            '5.5' => false,
540
                                            '5.6' => false,
541
                                            'alternative' => 'oci_parse'
542
                                        ),
543
                                        'ociplogon' => array(
544
                                            '5.4' => false,
545
                                            '5.5' => false,
546
                                            '5.6' => false,
547
                                            'alternative' => 'oci_pconnect'
548
                                        ),
549
                                        'ociresult' => array(
550
                                            '5.4' => false,
551
                                            '5.5' => false,
552
                                            '5.6' => false,
553
                                            'alternative' => 'oci_result'
554
                                        ),
555
                                        'ocirollback' => array(
556
                                            '5.4' => false,
557
                                            '5.5' => false,
558
                                            '5.6' => false,
559
                                            'alternative' => 'oci_rollback'
560
                                        ),
561
                                        'ocirowcount' => array(
562
                                            '5.4' => false,
563
                                            '5.5' => false,
564
                                            '5.6' => false,
565
                                            'alternative' => 'oci_num_rows'
566
                                        ),
567
                                        'ocisavelob' => array(
568
                                            '5.4' => false,
569
                                            '5.5' => false,
570
                                            '5.6' => false,
571
                                            'alternative' => 'OCI-Lob::save'
572
                                        ),
573
                                        'ocisavelobfile' => array(
574
                                            '5.4' => false,
575
                                            '5.5' => false,
576
                                            '5.6' => false,
577
                                            'alternative' => 'OCI-Lob::import'
578
                                        ),
579
                                        'ociserverversion' => array(
580
                                            '5.4' => false,
581
                                            '5.5' => false,
582
                                            '5.6' => false,
583
                                            'alternative' => 'oci_server_version'
584
                                        ),
585
                                        'ocisetprefetch' => array(
586
                                            '5.4' => false,
587
                                            '5.5' => false,
588
                                            '5.6' => false,
589
                                            'alternative' => 'oci_set_prefetch'
590
                                        ),
591
                                        'ocistatementtype' => array(
592
                                            '5.4' => false,
593
                                            '5.5' => false,
594
                                            '5.6' => false,
595
                                            'alternative' => 'oci_statement_type'
596
                                        ),
597
                                        'ociwritelobtofile' => array(
598
                                            '5.4' => false,
599
                                            '5.5' => false,
600
                                            '5.6' => false,
601
                                            'alternative' => 'OCI-Lob::export'
602
                                        ),
603
                                        'ociwritetemporarylob' => array(
604
                                            '5.4' => false,
605
                                            '5.5' => false,
606
                                            '5.6' => false,
607
                                            'alternative' => 'OCI-Lob::writeTemporary'
608
                                        ),
609
                                    );
610
611
    /**
612
     * If true, an error will be thrown; otherwise a warning.
613
     *
614
     * @var bool
615
     */
616
    public $error = false;
617
618
    /**
619
     * Returns an array of tokens this test wants to listen for.
620
     *
621
     * @return array
622
     */
623 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...
624
    {
625
        // Everyone has had a chance to figure out what forbidden functions
626
        // they want to check for, so now we can cache out the list.
627
        $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...
628
    
629
        if ($this->patternMatch === true) {
630
            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...
631
                $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...
632
            }
633
        }
634
    
635
        return array(T_STRING);
636
    
637
    }//end register()
638
    
639
    
640
    /**
641
     * Processes this test, when one of its tokens is encountered.
642
     *
643
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
644
     * @param int                  $stackPtr  The position of the current token in
645
     *                                        the stack passed in $tokens.
646
     *
647
     * @return void
648
     */
649
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
650
    {
651
        $tokens = $phpcsFile->getTokens();
652
653
        $ignore = array(
654
                T_DOUBLE_COLON,
655
                T_OBJECT_OPERATOR,
656
                T_FUNCTION,
657
                T_CONST,
658
                T_USE,
659
                T_NS_SEPARATOR,
660
        );
661
662
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
663
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
664
            // Not a call to a PHP function.
665
            return;
666
        }
667
668
        $function = strtolower($tokens[$stackPtr]['content']);
669
        $pattern  = null;
670
671 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...
672
            $count   = 0;
673
            $pattern = preg_replace(
674
                    $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...
675
                    $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...
676
                    $function,
677
                    1,
678
                    $count
679
            );
680
681
            if ($count === 0) {
682
                return;
683
            }
684
685
            // Remove the pattern delimiters and modifier.
686
            $pattern = substr($pattern, 1, -2);
687
        } else {
688
            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...
689
                return;
690
            }
691
        }
692
693
        $this->addError($phpcsFile, $stackPtr, $function, $pattern);
694
695
    }//end process()
696
697
    /**
698
     * Generates the error or wanrning for this sniff.
699
     *
700
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
701
     * @param int                  $stackPtr  The position of the forbidden function
702
     *                                        in the token array.
703
     * @param string               $function  The name of the forbidden function.
704
     * @param string               $pattern   The pattern used for the match.
705
     *
706
     * @return void
707
     */
708
    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
709
    {
710
        if ($pattern === null) {
711
            $pattern = $function;
712
        }
713
714
        $error = '';
715
716
        $this->error = false;
717
        $previousVersionStatus = null;
718
        foreach ($this->forbiddenFunctions[$pattern] as $version => $forbidden) {
719
            if ($this->supportsAbove($version)) {
720
                if ($version != 'alternative') {
721 View Code Duplication
                    if ($previousVersionStatus !== $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...
722
                        $previousVersionStatus = $forbidden;
723
                        if ($forbidden === true) {
724
                            $this->error = true;
725
                            $error .= 'forbidden';
726
                        } else {
727
                            $error .= 'discouraged';
728
                        }
729
                        $error .=  ' from PHP version ' . $version . ' and ';
730
                    }
731
                }
732
            }
733
        }
734
        if (strlen($error) > 0) {
735
            $error = 'The use of function ' . $function . ' is ' . $error;
736
            $error = substr($error, 0, strlen($error) - 5);
737
738
            if ($this->forbiddenFunctions[$pattern]['alternative'] !== null) {
739
                $error .= '; use ' . $this->forbiddenFunctions[$pattern]['alternative'] . ' instead';
740
            }
741
742
            if ($this->error === true) {
743
                $phpcsFile->addError($error, $stackPtr);
744
            } else {
745
                $phpcsFile->addWarning($error, $stackPtr);
746
            }
747
        }
748
749
    }//end addError()
750
751
}//end class
752