Skip to content

Commit 597e3f7

Browse files
author
Vincent Langlet
committed
✨ Add warning about == and !=
1 parent 05e0dc4 commit 597e3f7

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/**
4+
* Throws warning if == or != are use
5+
*/
6+
class Symfony3Custom_Sniffs_Formatting_StrictComparisonSniff implements PHP_CodeSniffer_Sniff
7+
{
8+
/**
9+
* Types to replace: key is operator to replace, value is operator to replace with.
10+
*
11+
* @var array
12+
*/
13+
public $operators = array(
14+
'T_IS_EQUAL' => '===',
15+
'T_IS_NOT_EQUAL' => '!==',
16+
);
17+
18+
/**
19+
* A list of tokenizers this sniff supports.
20+
*
21+
* @return array
22+
*/
23+
public function register()
24+
{
25+
return array(
26+
T_IS_EQUAL,
27+
T_IS_NOT_EQUAL,
28+
);
29+
}
30+
31+
/**
32+
* Processes this test, when one of its tokens is encountered.
33+
*
34+
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document.
35+
* @param int $stackPtr The position of the current token in
36+
* the stack passed in $tokens.
37+
*
38+
* @return void
39+
*/
40+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
41+
{
42+
$tokens = $phpcsFile->getTokens();
43+
44+
// This warning is fixable, but it's too dangerous to add automatically fixer
45+
$phpcsFile->addWarning(
46+
'The %s comparator is not recommended, use %s instead',
47+
$stackPtr,
48+
'',
49+
array(
50+
$tokens[$stackPtr]['content'],
51+
$this->operators[$tokens[$stackPtr]['type']],
52+
)
53+
);
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
if ($true == true) {
4+
echo 'True';
5+
} elseif ($true != true) {
6+
echo 'False';
7+
} elseif (false === $true && true !== $false) {
8+
echo 'False';
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for the StrictComparison sniff.
5+
*
6+
* A sniff unit test checks a .inc file for expected violations of a single
7+
* coding standard. Expected errors and warnings are stored in this class.
8+
*/
9+
class Symfony3Custom_Tests_Formatting_StrictComparisonUnitTest extends AbstractSniffUnitTest
10+
{
11+
/**
12+
* Returns the lines where errors should occur.
13+
*
14+
* @return array <int line number> => <int number of errors>
15+
*/
16+
public function getErrorList()
17+
{
18+
return array();
19+
}
20+
21+
/**
22+
* Returns the lines where warnings should occur.
23+
*
24+
* @return array <int line number> => <int number of warnings>
25+
*/
26+
public function getWarningList()
27+
{
28+
return array(
29+
3 => 1,
30+
5 => 1,
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)