Completed
Push — develop ( bd026a...0f4807 )
by Wim
02:04 queued 11s
created

ChangedObStartEraseFlagsSniff   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bowOutEarly() 0 4 1
B processParameters() 0 27 8
1
<?php
2
/**
3
 * PHPCompatibility, an external standard for PHP_CodeSniffer.
4
 *
5
 * @package   PHPCompatibility
6
 * @copyright 2012-2020 PHPCompatibility Contributors
7
 * @license   https://opensource.org/licenses/LGPL-3.0 LGPL3
8
 * @link      https://github.com/PHPCompatibility/PHPCompatibility
9
 */
10
11
namespace PHPCompatibility\Sniffs\ParameterValues;
12
13
use PHPCompatibility\AbstractFunctionCallParameterSniff;
14
use PHP_CodeSniffer_File as File;
15
16
/**
17
 * Detect incompatible use of the third parameter for `ob_start()`.
18
 *
19
 * > The third parameter of ob_start() changed from a boolean parameter called erase
20
 * > (which, if set to FALSE, would prevent the output buffer from being deleted until
21
 * > the script finished executing) to an integer parameter called flags.
22
 * > Unfortunately, this results in an API compatibility break for code written prior to
23
 * > PHP 5.4.0 that uses the third parameter.
24
 *
25
 * PHP version 5.4
26
 *
27
 * @link https://www.php.net/manual/en/function.ob-start.php#refsect1-function.ob-start-changelog
28
 *
29
 * @since 10.0.0
30
 */
31
class ChangedObStartEraseFlagsSniff extends AbstractFunctionCallParameterSniff
32
{
33
34
    /**
35
     * Functions to check for.
36
     *
37
     * @since 10.0.0
38
     *
39
     * @var array
40
     */
41
    protected $targetFunctions = array(
42
        'ob_start' => true,
43
    );
44
45
    /**
46
     * Do a version check to determine if this sniff needs to run at all.
47
     *
48
     * @since 10.0.0
49
     *
50
     * @return bool
51
     */
52
    protected function bowOutEarly()
53
    {
54
        return false;
55
    }
56
57
    /**
58
     * Process the parameters of a matched function.
59
     *
60
     * @since 10.0.0
61
     *
62
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
63
     * @param int                   $stackPtr     The position of the current token in the stack.
64
     * @param string                $functionName The token content (function name) which was matched.
65
     * @param array                 $parameters   Array with information about the parameters.
66
     *
67
     * @return int|void Integer stack pointer to skip forward or void to continue
68
     *                  normal file processing.
69
     */
70
    public function processParameters(File $phpcsFile, $stackPtr, $functionName, $parameters)
71
    {
72
        if (isset($parameters[3]) === false) {
73
            return;
74
        }
75
76
        $targetParam  = $parameters[3];
77
        $cleanValueLc = \strtolower($targetParam['clean']);
78
79
        $error = 'The third parameter of ob_start() changed from the boolean $erase to the integer $flags in PHP 5.4. Found: %s';
80
        $data  = array($targetParam['clean']);
81
82
        if ($cleanValueLc === 'true' || $cleanValueLc === 'false') {
83
            if ($this->supportsAbove('5.4') === true) {
84
                $phpcsFile->addError($error, $targetParam['start'], 'BooleanFound', $data);
85
            }
86
87
            return;
88
        }
89
90
        if ((preg_match('`PHP_OUTPUT_HANDLER_(CLEANABLE|FLUSHABLE|REMOVABLE|STDFLAGS)`', $targetParam['clean']) === 1
91
            || $this->isNumber($phpcsFile, $targetParam['start'], $targetParam['end'], true) !== false)
92
            && $this->supportsBelow('5.3') === true
93
        ) {
94
            $phpcsFile->addError($error, $targetParam['start'], 'IntegerFound', $data);
95
        }
96
    }
97
}
98