Skip to content

Commit 8542524

Browse files
committed
Fix error causing false-positive redeclaration warnings where use of late static binding constants as function arguments were being detected as static variable declarations on subsequent variable arguments to the function.
1 parent ecc76fc commit 8542524

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,12 @@ protected function checkForStaticDeclaration(
10041004
// Static declarations are a bit more complicated than globals, since they
10051005
// can contain assignments. The assignment is compile-time however so can
10061006
// only be constant values, which makes life manageable.
1007+
//
1008+
// Just to complicate matters further, late static binding constants
1009+
// take the form static::CONSTANT and are invalid within static variable
1010+
// assignments, but we don't want to accidentally match their use of the
1011+
// static keyword.
1012+
//
10071013
// Valid values are:
10081014
// number T_MINUS T_LNUMBER T_DNUMBER
10091015
// string T_CONSTANT_ENCAPSED_STRING
@@ -1031,6 +1037,16 @@ protected function checkForStaticDeclaration(
10311037
return false;
10321038
}
10331039

1040+
// Is it a late static binding static::?
1041+
// If so, this isn't the static keyword we're looking for, but since
1042+
// static:: isn't allowed in a compile-time constant, we also know
1043+
// we can't be part of a static declaration anyway, so there's no
1044+
// need to look any further.
1045+
$lateStaticBindingPtr = $phpcsFile->findNext(T_WHITESPACE, $staticPtr + 1, null, true, null, true);
1046+
if (($lateStaticBindingPtr !== false) && ($tokens[$lateStaticBindingPtr]['code'] === T_DOUBLE_COLON)) {
1047+
return false;
1048+
}
1049+
10341050
// It's a static declaration.
10351051
$this->markVariableDeclaration($varName, 'static', null, $stackPtr, $currScope);
10361052
if ($this->isNextThingAnAssign($phpcsFile, $stackPtr) !== false) {

Tests/CodeAnalysis/VariableAnalysisUnitTest.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function function_with_pass_by_ref_assign_only_arg(&$return_value) {
426426
}
427427

428428
class ClassWithLateStaticBinding {
429-
static function method_with_late_static($param) {
429+
static function method_with_late_static_binding($param) {
430430
static::some_method($param);
431431
static::some_method($var);
432432
static::some_method(static::CONSTANT, $param);

0 commit comments

Comments
 (0)