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 |
|
|
|
|
45
|
|
|
* @param int $stackPtr |
|
|
|
|
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); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Processes the token. |
68
|
|
|
* |
69
|
|
|
* @param File $phpcsFile |
|
|
|
|
70
|
|
|
* @param int $stackPtr |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected function processToken(): void |
75
|
|
|
{ |
76
|
|
|
$this->findToken($this->file, $this->stackPos); |
|
|
|
|
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
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.