Completed
Pull Request — master (#659)
by Juliette
03:24 queued 01:23
created

GetClassNullSniff   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bowOutEarly() 0 4 1
A processParameters() 0 16 3
1
<?php
2
/**
3
 * \PHPCompatibility\Sniffs\FunctionParameters\GetClassNullSniff.
4
 *
5
 * PHP version 7.2
6
 *
7
 * @category PHP
8
 * @package  PHPCompatibility
9
 * @author   Juliette Reinders Folmer <[email protected]>
10
 */
11
12
namespace PHPCompatibility\Sniffs\FunctionParameters;
13
14
use PHPCompatibility\AbstractFunctionCallParameterSniff;
15
16
/**
17
 * \PHPCompatibility\Sniffs\FunctionParameters\GetClassNullSniff.
18
 *
19
 * Detect: Passing `null` to get_class() is no longer allowed as of PHP 7.2.
20
 * This will now result in an E_WARNING being thrown.
21
 *
22
 * PHP version 7.2
23
 *
24
 * @category PHP
25
 * @package  PHPCompatibility
26
 * @author   Juliette Reinders Folmer <[email protected]>
27
 */
28
class GetClassNullSniff extends AbstractFunctionCallParameterSniff
29
{
30
31
    /**
32
     * Functions to check for.
33
     *
34
     * @var array
35
     */
36
    protected $targetFunctions = array(
37
        'get_class' => true,
38
    );
39
40
41
    /**
42
     * Do a version check to determine if this sniff needs to run at all.
43
     *
44
     * @return bool
45
     */
46
    protected function bowOutEarly()
47
    {
48
        return ($this->supportsAbove('7.2') === false);
49
    }
50
51
52
    /**
53
     * Process the parameters of a matched function.
54
     *
55
     * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned.
56
     * @param int                   $stackPtr     The position of the current token in the stack.
57
     * @param string                $functionName The token content (function name) which was matched.
58
     * @param array                 $parameters   Array with information about the parameters.
59
     *
60
     * @return int|void Integer stack pointer to skip forward or void to continue
61
     *                  normal file processing.
62
     */
63
    public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters)
64
    {
65
        if (isset($parameters[1]) === false) {
66
            return;
67
        }
68
69
        if ($parameters[1]['raw'] !== 'null') {
70
            return;
71
        }
72
73
        $phpcsFile->addError(
74
            'Passing "null" as the $object to get_class() is not allowed since PHP 7.2.',
75
            $parameters[1]['start'],
76
            'Found'
77
        );
78
    }
79
}//end class
80