Skip to content

Commit f16591d

Browse files
committed
Disallow assignments in if, elseif and do-while conditions
1 parent 8321a69 commit f16591d

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace SlevomatCodingStandard\Sniffs\ControlStructures;
4+
5+
class AssignmentInConditionSniff implements \PHP_CodeSniffer_Sniff
6+
{
7+
8+
const CODE_ASSIGNMENT_IN_CONDITION = 'assignmentInCondition';
9+
10+
/**
11+
* @return integer[]
12+
*/
13+
public function register()
14+
{
15+
return [
16+
T_IF,
17+
T_ELSEIF,
18+
T_DO,
19+
];
20+
}
21+
22+
/**
23+
* @param \PHP_CodeSniffer_File $phpcsFile
24+
* @param integer $conditionStartPointer
25+
*/
26+
public function process(\PHP_CodeSniffer_File $phpcsFile, $conditionStartPointer)
27+
{
28+
$tokens = $phpcsFile->getTokens();
29+
$token = $tokens[$conditionStartPointer];
30+
if ($token['code'] === T_DO) {
31+
$whilePointer = $phpcsFile->findNext(T_WHILE, $token['scope_closer'] + 1);
32+
$whileToken = $tokens[$whilePointer];
33+
$parenthesisOpener = $whileToken['parenthesis_opener'];
34+
$parenthesisCloser = $whileToken['parenthesis_closer'];
35+
$type = 'do-while';
36+
} else {
37+
$parenthesisOpener = $token['parenthesis_opener'];
38+
$parenthesisCloser = $token['parenthesis_closer'];
39+
$type = $token['code'] === T_IF ? 'if' : 'elseif';
40+
}
41+
$this->processCondition($phpcsFile, $parenthesisOpener, $parenthesisCloser, $type);
42+
}
43+
44+
/**
45+
* @param \PHP_CodeSniffer_File $phpcsFile
46+
* @param integer $parenthesisOpener
47+
* @param integer $parenthesisCloser
48+
* @param string $conditionType
49+
*/
50+
private function processCondition(
51+
\PHP_CodeSniffer_File $phpcsFile,
52+
$parenthesisOpener,
53+
$parenthesisCloser,
54+
$conditionType
55+
)
56+
{
57+
$equalsTokenPointer = $phpcsFile->findNext(T_EQUAL, $parenthesisOpener + 1, $parenthesisCloser);
58+
if ($equalsTokenPointer !== false) {
59+
$phpcsFile->addError(
60+
sprintf('Assignment in %s condition is not allowed', $conditionType),
61+
$equalsTokenPointer,
62+
self::CODE_ASSIGNMENT_IN_CONDITION
63+
);
64+
}
65+
}
66+
67+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace SlevomatCodingStandard\Sniffs\ControlStructures;
4+
5+
class AssignmentInConditionSniffTest extends \SlevomatCodingStandard\Sniffs\TestCase
6+
{
7+
8+
public function testCorrectFile()
9+
{
10+
$resultFile = $this->checkFile(__DIR__ . '/data/noAssignmentsInConditions.php');
11+
$this->assertNoSniffErrorInFile($resultFile);
12+
}
13+
14+
public function dataIncorrectFile()
15+
{
16+
$lineNumbers = [];
17+
foreach (range(3, 6) as $lineNumber) {
18+
$lineNumbers[$lineNumber] = [$lineNumber];
19+
}
20+
21+
return $lineNumbers;
22+
}
23+
24+
/**
25+
* @dataProvider dataIncorrectFile
26+
* @param integer $lineNumber
27+
*/
28+
public function testIncorrectFile($lineNumber)
29+
{
30+
$resultFile = $this->checkFile(__DIR__ . '/data/allAssignmentsInConditions.php');
31+
$this->assertSniffError($resultFile, $lineNumber, AssignmentInConditionSniff::CODE_ASSIGNMENT_IN_CONDITION);
32+
}
33+
34+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
if ($foo = 5) { }
4+
if ($foo) {} elseif ($foo = 5) {}
5+
do { } while ($foo = 5);
6+
if (($line = fgets($fp)) !== null) { }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
if ($foo == 5) {
4+
5+
} elseif ($foo == 10) {
6+
7+
}
8+
9+
if ($foo === 5) {
10+
11+
} elseif ($foo === 10) {
12+
13+
}
14+
15+
if ($foo != 5) {
16+
17+
} elseif ($foo != 10) {
18+
19+
}
20+
21+
if ($foo !== 5) {
22+
23+
} elseif ($foo !== 10) {
24+
25+
}
26+
27+
while ($foo == 5) {
28+
29+
}
30+
31+
while ($foo === 5) {
32+
33+
}
34+
35+
while ($foo = 5) {
36+
37+
}
38+
39+
while (($row = $db->fetch()) !== null) {
40+
41+
}
42+
43+
do {
44+
45+
} while ($foo === 5);

0 commit comments

Comments
 (0)