NewIniDirectivesSniff   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 603
Duplicated Lines 6.97 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 1
dl 42
loc 603
rs 9.997

8 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B process() 39 39 5
A getItemArray() 0 4 1
A getNonVersionArrayKeys() 0 4 1
A getErrorInfo() 3 16 4
A getErrorMsgTemplate() 0 4 1
A filterErrorMsg() 0 8 2
A filterErrorData() 0 8 2

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\NewIniDirectivesSniff.
4
 *
5
 * @category  PHP
6
 * @package   PHPCompatibility
7
 * @author    Wim Godden <[email protected]>
8
 * @copyright 2013 Cu.be Solutions bvba
9
 */
10
11
namespace PHPCompatibility\Sniffs\PHP;
12
13
use PHPCompatibility\AbstractNewFeatureSniff;
14
15
/**
16
 * \PHPCompatibility\Sniffs\PHP\NewIniDirectivesSniff.
17
 *
18
 * Discourages the use of new INI directives through ini_set() or ini_get().
19
 *
20
 * @category  PHP
21
 * @package   PHPCompatibility
22
 * @author    Wim Godden <[email protected]>
23
 * @copyright 2013 Cu.be Solutions bvba
24
 */
25
class NewIniDirectivesSniff extends AbstractNewFeatureSniff
26
{
27
    /**
28
     * A list of new INI directives
29
     *
30
     * The array lists : version number with false (not present) or true (present).
31
     * If's sufficient to list the first version where the ini directive appears.
32
     *
33
     * @var array(string)
34
     */
35
    protected $newIniDirectives = array(
36
        'auto_globals_jit' => array(
37
            '4.4' => false,
38
            '5.0' => true,
39
        ),
40
        'com.code_page' => array(
41
            '4.4' => false,
42
            '5.0' => true,
43
        ),
44
        'date.default_latitude' => array(
45
            '4.4' => false,
46
            '5.0' => true,
47
        ),
48
        'date.default_longitude' => array(
49
            '4.4' => false,
50
            '5.0' => true,
51
        ),
52
        'date.sunrise_zenith' => array(
53
            '4.4' => false,
54
            '5.0' => true,
55
        ),
56
        'date.sunset_zenith' => array(
57
            '4.4' => false,
58
            '5.0' => true,
59
        ),
60
        'ibase.default_charset' => array(
61
            '4.4' => false,
62
            '5.0' => true,
63
        ),
64
        'ibase.default_db' => array(
65
            '4.4' => false,
66
            '5.0' => true,
67
        ),
68
        'mail.force_extra_parameters' => array(
69
            '4.4' => false,
70
            '5.0' => true,
71
        ),
72
        'mime_magic.debug' => array(
73
            '4.4' => false,
74
            '5.0' => true,
75
        ),
76
        'mysqli.max_links' => array(
77
            '4.4' => false,
78
            '5.0' => true,
79
        ),
80
        'mysqli.default_port' => array(
81
            '4.4' => false,
82
            '5.0' => true,
83
        ),
84
        'mysqli.default_socket' => array(
85
            '4.4' => false,
86
            '5.0' => true,
87
        ),
88
        'mysqli.default_host' => array(
89
            '4.4' => false,
90
            '5.0' => true,
91
        ),
92
        'mysqli.default_user' => array(
93
            '4.4' => false,
94
            '5.0' => true,
95
        ),
96
        'mysqli.default_pw' => array(
97
            '4.4' => false,
98
            '5.0' => true,
99
        ),
100
        'report_zend_debug' => array(
101
            '4.4' => false,
102
            '5.0' => true,
103
        ),
104
        'session.hash_bits_per_character' => array(
105
            '4.4' => false,
106
            '5.0' => true,
107
        ),
108
        'session.hash_function' => array(
109
            '4.4' => false,
110
            '5.0' => true,
111
        ),
112
        'soap.wsdl_cache_dir' => array(
113
            '4.4' => false,
114
            '5.0' => true,
115
        ),
116
        'soap.wsdl_cache_enabled' => array(
117
            '4.4' => false,
118
            '5.0' => true,
119
        ),
120
        'soap.wsdl_cache_ttl' => array(
121
            '4.4' => false,
122
            '5.0' => true,
123
        ),
124
        'sqlite.assoc_case' => array(
125
            '4.4' => false,
126
            '5.0' => true,
127
        ),
128
        'tidy.clean_output' => array(
129
            '4.4' => false,
130
            '5.0' => true,
131
        ),
132
        'tidy.default_config' => array(
133
            '4.4' => false,
134
            '5.0' => true,
135
        ),
136
        'zend.ze1_compatibility_mode' => array(
137
            '4.4' => false,
138
            '5.0' => true,
139
        ),
140
141
        'date.timezone' => array(
142
            '5.0' => false,
143
            '5.1' => true,
144
        ),
145
        'detect_unicode' => array(
146
            '5.0' => false,
147
            '5.1' => true,
148
        ),
149
        'fbsql.batchsize' => array(
150
            '5.0'         => false,
151
            '5.1'         => true,
152
            'alternative' => 'fbsql.batchSize',
153
        ),
154
        'realpath_cache_size' => array(
155
            '5.0' => false,
156
            '5.1' => true,
157
        ),
158
        'realpath_cache_ttl' => array(
159
            '5.0' => false,
160
            '5.1' => true,
161
        ),
162
163
        'mbstring.strict_detection' => array(
164
            '5.1.1' => false,
165
            '5.1.2' => true,
166
        ),
167
        'mssql.charset' => array(
168
            '5.1.1' => false,
169
            '5.1.2' => true,
170
        ),
171
172
        'gd.jpeg_ignore_warning' => array(
173
            '5.1.2' => false,
174
            '5.1.3' => true,
175
        ),
176
177
        'fbsql.show_timestamp_decimals' => array(
178
            '5.1.4' => false,
179
            '5.1.5' => true,
180
        ),
181
        'soap.wsdl_cache' => array(
182
            '5.1.4' => false,
183
            '5.1.5' => true,
184
        ),
185
        'soap.wsdl_cache_limit' => array(
186
            '5.1.4' => false,
187
            '5.1.5' => true,
188
        ),
189
190
        'allow_url_include' => array(
191
            '5.1' => false,
192
            '5.2' => true,
193
        ),
194
        'filter.default' => array(
195
            '5.1' => false,
196
            '5.2' => true,
197
        ),
198
        'filter.default_flags' => array(
199
            '5.1' => false,
200
            '5.2' => true,
201
        ),
202
        'pcre.backtrack_limit' => array(
203
            '5.1' => false,
204
            '5.2' => true,
205
        ),
206
        'pcre.recursion_limit' => array(
207
            '5.1' => false,
208
            '5.2' => true,
209
        ),
210
        'session.cookie_httponly' => array(
211
            '5.1' => false,
212
            '5.2' => true,
213
        ),
214
215
        'cgi.check_shebang_line' => array(
216
            '5.2.0' => false,
217
            '5.2.1' => true,
218
        ),
219
220
        'max_input_nesting_level' => array(
221
            '5.2.2' => false,
222
            '5.2.3' => true,
223
        ),
224
225
        'mysqli.allow_local_infile' => array(
226
            '5.2.3' => false,
227
            '5.2.4' => true,
228
        ),
229
230
        'max_file_uploads' => array(
231
            '5.2.11' => false,
232
            '5.2.12' => true,
233
        ),
234
235
        'cgi.discard_path' => array(
236
            '5.2' => false,
237
            '5.3' => true,
238
        ),
239
        'exit_on_timeout' => array(
240
            '5.2' => false,
241
            '5.3' => true,
242
        ),
243
        'intl.default_locale' => array(
244
            '5.2' => false,
245
            '5.3' => true,
246
        ),
247
        'intl.error_level' => array(
248
            '5.2' => false,
249
            '5.3' => true,
250
        ),
251
        'mail.add_x_header' => array(
252
            '5.2' => false,
253
            '5.3' => true,
254
        ),
255
        'mail.log' => array(
256
            '5.2' => false,
257
            '5.3' => true,
258
        ),
259
        'mbstring.http_output_conv_mimetype' => array(
260
            '5.2' => false,
261
            '5.3' => true,
262
        ),
263
        'mysqli.allow_persistent' => array(
264
            '5.2' => false,
265
            '5.3' => true,
266
        ),
267
        'mysqli.cache_size' => array(
268
            '5.2' => false,
269
            '5.3' => true,
270
        ),
271
        'mysqli.max_persistent' => array(
272
            '5.2' => false,
273
            '5.3' => true,
274
        ),
275
        'mysqlnd.collect_memory_statistics' => array(
276
            '5.2' => false,
277
            '5.3' => true,
278
        ),
279
        'mysqlnd.collect_statistics' => array(
280
            '5.2' => false,
281
            '5.3' => true,
282
        ),
283
        'mysqlnd.debug' => array(
284
            '5.2' => false,
285
            '5.3' => true,
286
        ),
287
        'mysqlnd.net_read_buffer_size' => array(
288
            '5.2' => false,
289
            '5.3' => true,
290
        ),
291
        'odbc.default_cursortype' => array(
292
            '5.2' => false,
293
            '5.3' => true,
294
        ),
295
        'request_order' => array(
296
            '5.2' => false,
297
            '5.3' => true,
298
        ),
299
        'user_ini.cache_ttl' => array(
300
            '5.2' => false,
301
            '5.3' => true,
302
        ),
303
        'user_ini.filename' => array(
304
            '5.2' => false,
305
            '5.3' => true,
306
        ),
307
        'zend.enable_gc' => array(
308
            '5.2' => false,
309
            '5.3' => true,
310
        ),
311
312
        'curl.cainfo' => array(
313
            '5.3.6' => false,
314
            '5.3.7' => true,
315
        ),
316
317
        'max_input_vars' => array(
318
            '5.3.8' => false,
319
            '5.3.9' => true,
320
        ),
321
322
        'sqlite3.extension_dir' => array(
323
            '5.3.10' => false,
324
            '5.3.11' => true,
325
        ),
326
327
        'cli.pager' => array(
328
            '5.3' => false,
329
            '5.4' => true,
330
        ),
331
        'cli.prompt' => array(
332
            '5.3' => false,
333
            '5.4' => true,
334
        ),
335
        'cli_server.color' => array(
336
            '5.3' => false,
337
            '5.4' => true,
338
        ),
339
        'enable_post_data_reading' => array(
340
            '5.3' => false,
341
            '5.4' => true,
342
        ),
343
        'mysqlnd.mempool_default_size' => array(
344
            '5.3' => false,
345
            '5.4' => true,
346
        ),
347
        'mysqlnd.net_cmd_buffer_size' => array(
348
            '5.3' => false,
349
            '5.4' => true,
350
        ),
351
        'mysqlnd.net_read_timeout' => array(
352
            '5.3' => false,
353
            '5.4' => true,
354
        ),
355
        'phar.cache_list' => array(
356
            '5.3' => false,
357
            '5.4' => true,
358
        ),
359
        'session.upload_progress.enabled' => array(
360
            '5.3' => false,
361
            '5.4' => true,
362
        ),
363
        'session.upload_progress.cleanup' => array(
364
            '5.3' => false,
365
            '5.4' => true,
366
        ),
367
        'session.upload_progress.name' => array(
368
            '5.3' => false,
369
            '5.4' => true,
370
        ),
371
        'session.upload_progress.freq' => array(
372
            '5.3' => false,
373
            '5.4' => true,
374
        ),
375
        'session.upload_progress.min_freq' => array(
376
            '5.3' => false,
377
            '5.4' => true,
378
        ),
379
        'session.upload_progress.prefix' => array(
380
            '5.3' => false,
381
            '5.4' => true,
382
        ),
383
        'windows_show_crt_warning' => array(
384
            '5.3' => false,
385
            '5.4' => true,
386
        ),
387
        'zend.detect_unicode' => array(
388
            '5.3'         => false,
389
            '5.4'         => true,
390
            'alternative' => 'detect_unicode',
391
        ),
392
        'zend.multibyte' => array(
393
            '5.3' => false,
394
            '5.4' => true,
395
        ),
396
        'zend.script_encoding' => array(
397
            '5.3' => false,
398
            '5.4' => true,
399
        ),
400
        'zend.signal_check' => array(
401
            '5.3' => false,
402
            '5.4' => true,
403
        ),
404
        'mysqlnd.log_mask' => array(
405
            '5.3' => false,
406
            '5.4' => true,
407
        ),
408
409
        'intl.use_exceptions' => array(
410
            '5.4' => false,
411
            '5.5' => true,
412
        ),
413
        'mysqlnd.sha256_server_public_key' => array(
414
            '5.4' => false,
415
            '5.5' => true,
416
        ),
417
        'mysqlnd.trace_alloc' => array(
418
            '5.4' => false,
419
            '5.5' => true,
420
        ),
421
        'sys_temp_dir' => array(
422
            '5.4' => false,
423
            '5.5' => true,
424
        ),
425
        'xsl.security_prefs' => array(
426
            '5.4' => false,
427
            '5.5' => true,
428
        ),
429
430
        'session.use_strict_mode' => array(
431
            '5.5.1' => false,
432
            '5.5.2' => true,
433
        ),
434
435
        'mysqli.rollback_on_cached_plink' => array(
436
            '5.5' => false,
437
            '5.6' => true,
438
        ),
439
440
        'assert.exception' => array(
441
            '5.6' => false,
442
            '7.0' => true,
443
        ),
444
        'pcre.jit' => array(
445
            '5.6' => false,
446
            '7.0' => true,
447
        ),
448
        'session.lazy_write' => array(
449
            '5.6' => false,
450
            '7.0' => true,
451
        ),
452
        'zend.assertions' => array(
453
            '5.6' => false,
454
            '7.0' => true,
455
        ),
456
457
        'session.sid_length' => array(
458
            '7.0' => false,
459
            '7.1' => true,
460
        ),
461
        'session.sid_bits_per_character' => array(
462
            '7.0' => false,
463
            '7.1' => true,
464
        ),
465
    );
466
467
    /**
468
     * Returns an array of tokens this test wants to listen for.
469
     *
470
     * @return array
471
     */
472
    public function register()
473
    {
474
        return array(T_STRING);
475
476
    }//end register()
477
478
    /**
479
     * Processes this test, when one of its tokens is encountered.
480
     *
481
     * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned.
482
     * @param int                   $stackPtr  The position of the current token in the
483
     *                                         stack passed in $tokens.
484
     *
485
     * @return void
486
     */
487 View Code Duplication
    public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr)
488
    {
489
        $tokens = $phpcsFile->getTokens();
490
491
        $ignore = array(
492
            T_DOUBLE_COLON,
493
            T_OBJECT_OPERATOR,
494
            T_FUNCTION,
495
            T_CONST,
496
        );
497
498
        $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
499
        if (in_array($tokens[$prevToken]['code'], $ignore) === true) {
500
            // Not a call to a PHP function.
501
            return;
502
        }
503
504
        $functionLc = strtolower($tokens[$stackPtr]['content']);
505
        if (isset($this->iniFunctions[$functionLc]) === false) {
506
            return;
507
        }
508
509
        $iniToken = $this->getFunctionCallParameter($phpcsFile, $stackPtr, $this->iniFunctions[$functionLc]);
510
        if ($iniToken === false) {
511
            return;
512
        }
513
514
        $filteredToken = $this->stripQuotes($iniToken['raw']);
515
        if (isset($this->newIniDirectives[$filteredToken]) === false) {
516
            return;
517
        }
518
519
        $itemInfo = array(
520
            'name'       => $filteredToken,
521
            'functionLc' => $functionLc,
522
        );
523
        $this->handleFeature($phpcsFile, $iniToken['end'], $itemInfo);
524
525
    }//end process()
526
527
528
    /**
529
     * Get the relevant sub-array for a specific item from a multi-dimensional array.
530
     *
531
     * @param array $itemInfo Base information about the item.
532
     *
533
     * @return array Version and other information about the item.
534
     */
535
    public function getItemArray(array $itemInfo)
536
    {
537
        return $this->newIniDirectives[$itemInfo['name']];
538
    }
539
540
541
    /**
542
     * Get an array of the non-PHP-version array keys used in a sub-array.
543
     *
544
     * @return array
545
     */
546
    protected function getNonVersionArrayKeys()
547
    {
548
        return array('alternative');
549
    }
550
551
552
    /**
553
     * Retrieve the relevant detail (version) information for use in an error message.
554
     *
555
     * @param array $itemArray Version and other information about the item.
556
     * @param array $itemInfo  Base information about the item.
557
     *
558
     * @return array
559
     */
560
    public function getErrorInfo(array $itemArray, array $itemInfo)
561
    {
562
        $errorInfo = parent::getErrorInfo($itemArray, $itemInfo);
563
        $errorInfo['alternative'] = '';
564
565
        if (isset($itemArray['alternative']) === true) {
566
            $errorInfo['alternative'] = $itemArray['alternative'];
567
        }
568
569
        // Lower error level to warning if the function used was ini_get.
570 View Code Duplication
        if ($errorInfo['error'] === true && $itemInfo['functionLc'] === 'ini_get') {
571
            $errorInfo['error'] = false;
572
        }
573
574
        return $errorInfo;
575
    }
576
577
578
    /**
579
     * Get the error message template for this sniff.
580
     *
581
     * @return string
582
     */
583
    protected function getErrorMsgTemplate()
584
    {
585
        return "INI directive '%s' is not present in PHP version %s or earlier";
586
    }
587
588
589
    /**
590
     * Allow for concrete child classes to filter the error message before it's passed to PHPCS.
591
     *
592
     * @param string $error     The error message which was created.
593
     * @param array  $itemInfo  Base information about the item this error message applied to.
594
     * @param array  $errorInfo Detail information about an item this error message applied to.
595
     *
596
     * @return string
597
     */
598
    protected function filterErrorMsg($error, array $itemInfo, array $errorInfo)
599
    {
600
        if ($errorInfo['alternative'] !== '') {
601
            $error .= ". This directive was previously called '%s'.";
602
        }
603
604
        return $error;
605
    }
606
607
608
    /**
609
     * Allow for concrete child classes to filter the error data before it's passed to PHPCS.
610
     *
611
     * @param array $data      The error data array which was created.
612
     * @param array $itemInfo  Base information about the item this error message applied to.
613
     * @param array $errorInfo Detail information about an item this error message applied to.
614
     *
615
     * @return array
616
     */
617
    protected function filterErrorData(array $data, array $itemInfo, array $errorInfo)
618
    {
619
        if ($errorInfo['alternative'] !== '') {
620
            $data[] = $errorInfo['alternative'];
621
        }
622
623
        return $data;
624
    }
625
626
627
}//end class
628