Skip to content

Commit c5eaed8

Browse files
committed
Merge branch 'feature/2787-squiz-disallowmultipleassignments-bugfix' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 27cbb66 + 4ce821e commit c5eaed8

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/Standards/Squiz/Sniffs/PHP/DisallowMultipleAssignmentsSniff.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public function process(File $phpcsFile, $stackPtr)
6464
}
6565
}
6666

67+
// Ignore member var definitions.
68+
if (empty($tokens[$stackPtr]['conditions']) === false) {
69+
$conditions = $tokens[$stackPtr]['conditions'];
70+
end($conditions);
71+
$deepestScope = key($conditions);
72+
if (isset(Tokens::$ooScopeTokens[$tokens[$deepestScope]['code']]) === true) {
73+
return;
74+
}
75+
}
76+
6777
/*
6878
The general rule is:
6979
Find an equal sign and go backwards along the line. If you hit an
@@ -124,14 +134,6 @@ public function process(File $phpcsFile, $stackPtr)
124134
$varToken = $start;
125135
}
126136

127-
// Ignore member var definitions.
128-
if (isset(Tokens::$scopeModifiers[$tokens[$varToken]['code']]) === true
129-
|| $tokens[$varToken]['code'] === T_VAR
130-
|| $tokens[$varToken]['code'] === T_STATIC
131-
) {
132-
return;
133-
}
134-
135137
// Ignore the first part of FOR loops as we are allowed to
136138
// assign variables there even though the variable is not the
137139
// first thing on the line.

src/Standards/Squiz/Tests/PHP/DisallowMultipleAssignmentsUnitTest.inc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,16 @@ $closureWithDefaultParamter = function(array $testArray=array()) {};
6262
while ( ( $csvdata = fgetcsv( $handle, 2000, $separator ) ) !== false ) {
6363
// Do something.
6464
}
65+
66+
// Issue #2787.
67+
class Foo {
68+
protected ?int $id = null;
69+
}
70+
71+
class Bar
72+
{
73+
// Multi-property assignments.
74+
private $a = false, $b = true; // Non-typed.
75+
public bool $c = false, $d = true;
76+
protected int $e = 123, $f = 987;
77+
}

0 commit comments

Comments
 (0)