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

FopenModeSniff::bowOutEarly()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\FunctionParameters\FopenModeSniff.
4
 *
5
 * @category PHP
6
 * @package  PHPCompatibility
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\FopenModeSniff.
16
 *
17
 * Detect: Changes in allowed values for the fopen() $mode parameter.
18
 *
19
 * @category PHP
20
 * @package  PHPCompatibility
21
 * @author   Juliette Reinders Folmer <[email protected]>
22
 */
23
class FopenModeSniff extends AbstractFunctionCallParameterSniff
24
{
25
26
    /**
27
     * Functions to check for.
28
     *
29
     * @var array
30
     */
31
    protected $targetFunctions = array(
32
        'fopen' => true,
33
    );
34
35
36
    /**
37
     * Do a version check to determine if this sniff needs to run at all.
38
     *
39
     * @return bool
40
     */
41
    protected function bowOutEarly()
42
    {
43
        // Version used here should be (above) the highest version from the `$newModes` array,
44
        // i.e. the last PHP version in which a new mode was introduced.
45
        return ($this->supportsBelow('7.1') === false);
46
    }
47
48
49
    /**
50
     * Process the parameters of a matched function.
51
     *
52
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
53
     * @param int                   $stackPtr     The position of the current token in the stack.
54
     * @param string                $functionName The token content (function name) which was matched.
55
     * @param array                 $parameters   Array with information about the parameters.
56
     *
57
     * @return int|void Integer stack pointer to skip forward or void to continue
58
     *                  normal file processing.
59
     */
60
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
61
    {
62
        if (isset($parameters[2]) === false) {
63
            return;
64
        }
65
66
        $tokens      = $phpcsFile->getTokens();
67
        $targetParam = $parameters[2];
68
        $errors      = array();
69
70
        for ($i = $targetParam['start']; $i <= $targetParam['end']; $i++) {
71
            if ($tokens[$i]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
72
                continue;
73
            }
74
75
            if (strpos($tokens[$i]['content'], 'c+') !== false && $this->supportsBelow('5.2.5')) {
76
                $errors['cplusFound'] = array(
77
                    'c+',
78
                    '5.2.5',
79
                    $targetParam['raw'],
80
                );
81
            } elseif (strpos($tokens[$i]['content'], 'c') !== false && $this->supportsBelow('5.2.5')) {
82
                $errors['cFound'] = array(
83
                    'c',
84
                    '5.2.5',
85
                    $targetParam['raw'],
86
                );
87
            }
88
89
            if (strpos($tokens[$i]['content'], 'e') !== false && $this->supportsBelow('7.0.15')) {
90
                $errors['eFound'] = array(
91
                    'e',
92
                    '7.0.15',
93
                    $targetParam['raw'],
94
                );
95
            }
96
        }
97
98
        if (empty($errors) === true) {
99
            return;
100
        }
101
102
        foreach ($errors as $errorCode => $errorData) {
103
            $phpcsFile->addError(
104
                'Passing "%s" as the $mode to fopen() is not supported in PHP %s or lower. Found %s',
105
                $targetParam['start'],
106
                $errorCode,
107
                $errorData
108
            );
109
        }
110
    }
111
}//end class
112