Completed
Push — feature/new-pack-formats-sniff ( 86e997 )
by Juliette
07:35 queued 05:56
created

PackFormatSniff::bowOutEarly()   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\FunctionParameters\PackFormatSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility\FunctionParameters
7
 * @author   Juliette Reinders Folmer <[email protected]>
8
 */
9
10
namespace PHPCompatibility\Sniffs\FunctionParameters;
11
12
use PHPCompatibility\AbstractFunctionCallParameterSniff;
13
14
/**
15
 * \PHPCompatibility\Sniffs\FunctionParameters\PackFormatSniff.
16
 *
17
 * Detect: Changes in the allowed values for $format passed to pack().
18
 *
19
 * @category PHP
20
 * @package  PHPCompatibility\FunctionParameters
21
 * @author   Juliette Reinders Folmer <[email protected]>
22
 */
23
class PackFormatSniff extends AbstractFunctionCallParameterSniff
24
{
25
26
    /**
27
     * Functions to check for.
28
     *
29
     * @var array
30
     */
31
    protected $targetFunctions = array(
32
        'pack' => true,
33
    );
34
35
    /**
36
     * List of new format character codes added to pack().
37
     *
38
     * @var array Regex pattern => Version array.
39
     */
40
    protected $newFormats = array(
41
        '`([Z])`'    => array(
42
            '5.4' => false,
43
            '5.5' => true,
44
        ),
45
        '`([qQJP])`' => array(
46
            '5.6.2' => false,
47
            '5.6.3' => true,
48
        ),
49
        '`([eEgG])`' => array(
50
            '7.0.14' => false,
51
            '7.0.15' => true, // And 7.1.1.
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        ),
53
    );
54
55
56
    /**
57
     * Do a version check to determine if this sniff needs to run at all.
58
     *
59
     * @return bool
60
     */
61
    protected function bowOutEarly()
62
    {
63
        return ($this->supportsBelow('7.1') === false);
64
    }
65
66
67
    /**
68
     * Process the parameters of a matched function.
69
     *
70
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
71
     * @param int                   $stackPtr     The position of the current token in the stack.
72
     * @param string                $functionName The token content (function name) which was matched.
73
     * @param array                 $parameters   Array with information about the parameters.
74
     *
75
     * @return int|void Integer stack pointer to skip forward or void to continue
76
     *                  normal file processing.
77
     */
78
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
79
    {
80
        if (isset($parameters[1]) === false) {
81
            return;
82
        }
83
84
        $tokens      = $phpcsFile->getTokens();
85
        $targetParam = $parameters[1];
86
87
        for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
88
            if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING
89
                && $tokens[$i]['code'] !== T_DOUBLE_QUOTED_STRING
90
            ) {
91
                continue;
92
            }
93
94
            $content = $tokens[$i]['content'];
95
            if ($tokens[$i]['code'] === T_DOUBLE_QUOTED_STRING) {
96
                $content = $this->stripVariables($content);
97
            }
98
99
            foreach ($this->newFormats as $pattern => $versionArray) {
100
                if (preg_match($pattern, $content, $matches) !== 1) {
101
                    continue;
102
                }
103
104
                foreach ($versionArray as $version => $present) {
105
                    if ($present === false && $this->supportsBelow($version) === true) {
106
                        $phpcsFile->addError(
107
                            'Passing the $format(s) "%s" to pack() is not supported in PHP %s or lower. Found %s',
108
                            $targetParam['start'],
109
                            'NewFormatFound',
110
                            array(
111
                                $matches[1],
112
                                $version,
113
                                $targetParam['raw'],
114
                            )
115
                        );
116
                        continue 2;
117
                    }
118
                }
119
            }
120
        }
121
    }
122
}//end class
123