Completed
Pull Request — master (#58)
by
unknown
03:20
created

EqualOperatorSniff::findToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
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 $isFixing
22
     */
23
    public $isFixing = 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
     * @return void
45
     */
46
    private function findToken(): void
47
    {
48
        if ($this->token['code'] === T_IS_EQUAL) {
49
            $isFixing = $this->file->addFixableWarning(
50
                self::WARNING_EQUAL_OPERATOR_FOUND,
51
                $this->stackPos,
52
                self::CODE_EQUAL_OPERATOR_FOUND
53
            );
54
55
            if ($isFixing) {
56
                $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...
57
            }
58
        }
59
    }
60
61
    /**
62
     * Processes the token.
63
     *
64
     * @return void
65
     */
66
    protected function processToken(): void
67
    {
68
        $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...
69
    }
70
71
    /**
72
     * Registers the tokens that this sniff wants to listen for.
73
     *
74
     * An example return value for a sniff that wants to listen for whitespace
75
     * and any comments would be:
76
     *
77
     * <code>
78
     *    return array(
79
     *            T_WHITESPACE,
80
     *            T_DOC_COMMENT,
81
     *            T_COMMENT,
82
     *           );
83
     * </code>
84
     *
85
     * @return int[]
86
     * @see    Tokens.php
87
     */
88
    public function register(): array
89
    {
90
        return [T_IS_EQUAL];
91
    }
92
93
    /**
94
     * Replace token with the T_IS_IDENTIAL token.
95
     *
96
     * @param File $phpcsFile
97
     * @param int $stackPtr
98
     *
99
     * @return void
100
     */
101
    private function replaceToken(File $phpcsFile, int $stackPtr): void
102
    {
103
        $phpcsFile->fixer->beginChangeset();
104
        $phpcsFile->fixer->replaceToken($stackPtr, '===');
105
        $phpcsFile->fixer->endChangeset();
106
    }
107
}
108