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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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: