Passed
Pull Request — master (#58)
by
unknown
03:39
created

EqualOperatorSniff::replaceToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
     * Code when an equal operator was found.
20
     *
21
     * @var CODE_EQUAL_OPERATOR_FOUND string
22
     */
23
    public const CODE_EQUAL_OPERATOR_FOUND = 'EqualOperatorFound';
24
25
    /**
26
     * Warning when an equal operator was found.
27
     *
28
     * @var WARNING_EQUAL_OPERATOR_FOUND string
29
     */
30
    public const WARNING_EQUAL_OPERATOR_FOUND = '
31
        An "Equal"-operator (==) was found. You should use the "Identical"-operator (===).
32
    ';
33
34
    /**
35
     * Find the T_IS_EQUAL token and add a warning.
36
     *
37
     * @param File $phpcsFile
38
     * @param int $stackPtr
39
     *
40
     * @return void
41
     */
42
    private function findToken(File $phpcsFile, int $stackPtr): void
43
    {
44
        $token = $phpcsFile->getTokens()[$stackPtr];
45
46
        if ($token['code'] === T_IS_EQUAL) {
47
            $isFixing = $phpcsFile->addFixableWarning(
48
                self::WARNING_EQUAL_OPERATOR_FOUND,
49
                $stackPtr,
50
                self::CODE_EQUAL_OPERATOR_FOUND
51
            );
52
53
            if ($isFixing) {
54
                $this->replaceToken($phpcsFile, $stackPtr);
55
            }
56
        }
57
    }
58
59
    /**
60
     * Processes the token.
61
     *
62
     * @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...
63
     * @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...
64
     *
65
     * @return void
66
     */
67
    protected function processToken(): void
68
    {
69
        $this->findToken($this->file, $this->stackPos);
0 ignored issues
show
Bug introduced by
It seems like $this->file can be null; however, findToken() 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...
70
    }
71
72
    /**
73
     * Registers the tokens that this sniff wants to listen for.
74
     *
75
     * An example return value for a sniff that wants to listen for whitespace
76
     * and any comments would be:
77
     *
78
     * <code>
79
     *    return array(
80
     *            T_WHITESPACE,
81
     *            T_DOC_COMMENT,
82
     *            T_COMMENT,
83
     *           );
84
     * </code>
85
     *
86
     * @return int[]
87
     * @see    Tokens.php
88
     */
89
    public function register(): array
90
    {
91
        return [T_IS_EQUAL];
92
    }
93
94
    /**
95
     * Replace token with the T_IS_IDENTIAL token.
96
     *
97
     * @param File $phpcsFile
98
     * @param int $stackPtr
99
     *
100
     * @return void
101
     */
102
    private function replaceToken(File $phpcsFile, int $stackPtr): void
103
    {
104
        $phpcsFile->fixer->beginChangeset();
105
        $phpcsFile->fixer->replaceToken($stackPtr, '===');
106
        $phpcsFile->fixer->endChangeset();
107
    }
108
}
109