Completed
Pull Request — master (#291)
by Juliette
02:26
created

getErrorMsgTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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