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

getErrorInfo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 3
Ratio 18.75 %

Importance

Changes 0
Metric Value
dl 3
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
/**
3
 * PHPCompatibility_Sniffs_PHP_NewIniDirectivesSniff.
4
 *
5
 * PHP version 5.5
6
 *
7
 * @category  PHP
8
 * @package   PHPCompatibility
9
 * @author    Wim Godden <[email protected]>
10
 * @copyright 2013 Cu.be Solutions bvba
11
 */
12
13
/**
14
 * PHPCompatibility_Sniffs_PHP_NewIniDirectivesSniff.
15
 *
16
 * Discourages the use of new INI directives through ini_set() or ini_get().
17
 *
18
 * @category  PHP
19
 * @package   PHPCompatibility
20
 * @author    Wim Godden <[email protected]>
21
 * @copyright 2013 Cu.be Solutions bvba
22
 */
23
class PHPCompatibility_Sniffs_PHP_NewIniDirectivesSniff extends PHPCompatibility_AbstractNewFeatureSniff
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

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