Completed
Push — master ( 14c836...906b18 )
by Björn
06:09 queued 03:52
created

EqualOperatorSniff   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BestIt\Sniffs\Comparisons;
6
7
use BestIt\Sniffs\AbstractSniff;
8
use const T_IS_EQUAL;
9
10
/**
11
 * Class EqualOperatorSniff.
12
 *
13
 * @author Mika Bertels <[email protected]>
14
 * @package BestIt\Sniffs\Comparisons
15
 */
16
class EqualOperatorSniff extends AbstractSniff
17
{
18
    /**
19
     * You SHOULD use the "Identical" operator (===).
20
     */
21
    public const CODE_EQUAL_OPERATOR_FOUND = 'EqualOperatorFound';
22
23
    /**
24
     * Warning when an equal operator was found.
25
     *
26
     * @var string WARNING_EQUAL_OPERATOR_FOUND
27
     */
28
    private const MESSAGE_EQUAL_OPERATOR_FOUND = 'Please check if you could use the "Identical" operator (===).';
29
30
    /**
31
     * Is this sniff allowed to fix?
32
     *
33
     * @var bool $isFixing
34
     */
35
    public bool $isFixable = false;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
36
    
37
    /**
38
     * Processes the token.
39
     *
40
     * @return void
41
     */
42
    protected function processToken(): void
43
    {
44
        $file = $this->getFile();
45
        $stackPos = $this->getStackPos();
46
47
        $isFixing = $file->addFixableWarning(
48
            self::MESSAGE_EQUAL_OPERATOR_FOUND,
49
            $stackPos,
50
            static::CODE_EQUAL_OPERATOR_FOUND
51
        );
52
53
        $file->recordMetric($stackPos, 'Found not wanted T_IS_EQUAL', 'yes');
54
55
        if ($isFixing && $this->isFixable) {
56
            $this->replaceToken();
57
        }
58
    }
59
60
    /**
61
     * Registers the tokens that this sniff wants to listen for.
62
     *
63
     * An example return value for a sniff that wants to listen for whitespace
64
     * and any comments would be:
65
     *
66
     * <code>
67
     *    return array(
68
     *            T_WHITESPACE,
69
     *            T_DOC_COMMENT,
70
     *            T_COMMENT,
71
     *           );
72
     * </code>
73
     *
74
     * @see    Tokens.php
75
     *
76
     * @return int[]
77
     */
78
    public function register(): array
79
    {
80
        return [T_IS_EQUAL];
81
    }
82
83
    /**
84
     * Replace token with the T_IS_IDENTIAL token.
85
     *
86
     * @return void
87
     */
88
    private function replaceToken(): void
89
    {
90
        $file = $this->getFile();
91
92
        $file->fixer->beginChangeset();
93
        $file->fixer->replaceToken($this->getStackPos(), '===');
94
        $file->fixer->endChangeset();
95
    }
96
}
97