Skip to content

Commit 0d13cee

Browse files
authored
Merge pull request #2026 from PHPCompatibility/feature/syntax-removedcurlybracearrayaccess-false-positive-on-property-hooks
Syntax/RemovedCurlyBraceArrayAccess: prevent false positive on PHP 8.4 property hook declarations
2 parents 4cb668c + b27936e commit 0d13cee

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

PHPCompatibility/Sniffs/Syntax/RemovedCurlyBraceArrayAccessSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPCompatibility\Sniffs\Syntax\NewFunctionArrayDereferencingSniff;
1818
use PHP_CodeSniffer\Files\File;
1919
use PHP_CodeSniffer\Util\Tokens;
20+
use PHPCSUtils\Utils\Scopes;
2021

2122
/**
2223
* Using the curly brace syntax to access array or string offsets has been deprecated in PHP 7.4
@@ -160,6 +161,11 @@ public function process(File $phpcsFile, $stackPtr)
160161

161162
// Note: Overwriting braces in each `if` is fine as only one will match anyway.
162163
if ($tokens[$stackPtr]['code'] === \T_VARIABLE) {
164+
if (Scopes::isOOProperty($phpcsFile, $stackPtr) === true) {
165+
// This will be a PHP 8.4 property hook, not array access.
166+
return;
167+
}
168+
163169
$braces = $this->isVariableArrayAccess($phpcsFile, $stackPtr);
164170
}
165171

PHPCompatibility/Tests/Syntax/RemovedCurlyBraceArrayAccessUnitTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ Partially\Qualified\ClassName::FOO[1]{0};
147147
namespace\callMe(){0};
148148
\Fully\Qualified\callMe($input){0};
149149
Partially\Qualified\callMe($a, $b){0};
150+
151+
// Safeguard against false positives for PHP 8.4 property hooks.
152+
interface Thing {
153+
public string $id { get; }
154+
}

PHPCompatibility/Tests/Syntax/RemovedCurlyBraceArrayAccessUnitTest.inc.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,8 @@ Partially\Qualified\ClassName::FOO[1][0];
147147
namespace\callMe()[0];
148148
\Fully\Qualified\callMe($input)[0];
149149
Partially\Qualified\callMe($a, $b)[0];
150+
151+
// Safeguard against false positives for PHP 8.4 property hooks.
152+
interface Thing {
153+
public string $id { get; }
154+
}

PHPCompatibility/Tests/Syntax/RemovedCurlyBraceArrayAccessUnitTest.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,39 @@ public static function dataRemovedCurlyBraceArrayAccess()
111111

112112

113113
/**
114-
* testNoFalsePositives
114+
* Test that there are no false positives for valid code.
115+
*
116+
* @dataProvider dataNoFalsePositives
117+
*
118+
* @param int $line Line number.
115119
*
116120
* @return void
117121
*/
118-
public function testNoFalsePositives()
122+
public function testNoFalsePositives($line)
119123
{
120124
$file = $this->sniffFile(__FILE__, '7.4');
125+
$this->assertNoViolation($file, $line);
126+
}
127+
128+
/**
129+
* Data provider.
130+
*
131+
* @see testNoFalsePositives()
132+
*
133+
* @return array<array<int>>
134+
*/
135+
public static function dataNoFalsePositives()
136+
{
137+
$data = [];
121138

122139
// No errors expected on the first 49 lines.
123140
for ($line = 1; $line <= 49; $line++) {
124-
$this->assertNoViolation($file, $line);
141+
$data[] = [$line];
125142
}
143+
144+
$data[] = [153];
145+
146+
return $data;
126147
}
127148

128149

0 commit comments

Comments
 (0)