Completed
Pull Request — master (#58)
by
unknown
40:48 queued 23:30
created

EqualOperatorSniff::processToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs\Formatting;
6
7
use BestIt\Sniffs\AbstractSniff;
8
use PHP_CodeSniffer\Files\File;
9
10
/**
11
 * Class EqualOperatorSniff.
12
 *
13
 * @author Mika Bertels <[email protected]>
14
 * @package BestIt\Sniffs\Formatting
15
 */
16
class EqualOperatorSniff extends AbstractSniff
17
{
18
    /**
19
     * Is this sniff allowed to fix?
20
     *
21
     * @var bool $isFixable
22
     */
23
    private $isFixable = false;
24
25
    /**
26
     * Code when an equal operator was found.
27
     *
28
     * @var string CODE_EQUAL_OPERATOR_FOUND
29
     */
30
    public const CODE_EQUAL_OPERATOR_FOUND = 'EqualOperatorFound';
31
32
    /**
33
     * Warning when an equal operator was found.
34
     *
35
     * @var string WARNING_EQUAL_OPERATOR_FOUND
36
     */
37
    private const WARNING_EQUAL_OPERATOR_FOUND = '
38
        Please check if you could use the "Identical" operator (===).
39
    ';
40
41
    /**
42
     * Find the T_IS_EQUAL token and add a warning.
43
     *
44
     * @param File $phpcsFile
0 ignored issues
show
Bug introduced by
There is no parameter named $phpcsFile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     * @param int $stackPtr
0 ignored issues
show
Bug introduced by
There is no parameter named $stackPtr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     *
47
     * @return void
48
     */
49
    private function findToken(): void
50
    {
51
        $token = $this->token;
52
53
        if ($token['code'] === T_IS_EQUAL) {
54
            $isFixing = $this->file->addFixableWarning(
55
                self::WARNING_EQUAL_OPERATOR_FOUND,
56
                $this->stackPos,
57
                self::CODE_EQUAL_OPERATOR_FOUND
58
            );
59
60
            if ($isFixing) {
61
                $this->replaceToken($this->file, $this->stackPos);
0 ignored issues
show
Bug introduced by
It seems like $this->file can be null; however, replaceToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
            }
63
        }
64
    }
65
66
    /**
67
     * Processes the token.
68
     *
69
     * @param File $phpcsFile
0 ignored issues
show
Bug introduced by
There is no parameter named $phpcsFile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     * @param int $stackPtr
0 ignored issues
show
Bug introduced by
There is no parameter named $stackPtr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
71
     *
72
     * @return void
73
     */
74
    protected function processToken(): void
75
    {
76
        $this->findToken($this->file, $this->stackPos);
0 ignored issues
show
Unused Code introduced by
The call to EqualOperatorSniff::findToken() has too many arguments starting with $this->file.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
77
    }
78
79
    /**
80
     * Registers the tokens that this sniff wants to listen for.
81
     *
82
     * An example return value for a sniff that wants to listen for whitespace
83
     * and any comments would be:
84
     *
85
     * <code>
86
     *    return array(
87
     *            T_WHITESPACE,
88
     *            T_DOC_COMMENT,
89
     *            T_COMMENT,
90
     *           );
91
     * </code>
92
     *
93
     * @return int[]
94
     * @see    Tokens.php
95
     */
96
    public function register(): array
97
    {
98
        return [T_IS_EQUAL];
99
    }
100
101
    /**
102
     * Replace token with the T_IS_IDENTIAL token.
103
     *
104
     * @param File $phpcsFile
105
     * @param int $stackPtr
106
     *
107
     * @return void
108
     */
109
    private function replaceToken(File $phpcsFile, int $stackPtr): void
110
    {
111
        $phpcsFile->fixer->beginChangeset();
112
        $phpcsFile->fixer->replaceToken($stackPtr, '===');
113
        $phpcsFile->fixer->endChangeset();
114
    }
115
}
116