Skip to content

Commit 4ce821e

Browse files
committed
Squiz/DisallowMultipleAssignments: fix ignoring of property declarations
The `Squiz.PHP.DisallowMultipleAssignments` intended to ignore property declarations, but did not allow for typed properties. I've now moved the "is this a property ?" check up to bow out earlier for all properties. Includes unit test. I've also added tests for multi-property declarations. While it is debatable whether or not this sniff should report on these, the existing behaviour was to ignore them. This behaviour has been maintained and is now documented and safeguarded via the test. Fixes 2787
1 parent 90b719d commit 4ce821e

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)