Skip to content

Commit edf4757

Browse files
authored
Merge pull request #1188 from PHPCSStandards/php-8.4/psr2-propertydeclaration-support-abstract-properties
PHP 8.4 | PSR2/PropertyDeclaration: add support for abstract properties
2 parents a76ca58 + 1617656 commit edf4757

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
4444
$find[] = T_VAR;
4545
$find[] = T_READONLY;
4646
$find[] = T_FINAL;
47+
$find[] = T_ABSTRACT;
4748
$find[] = T_SEMICOLON;
4849
$find[] = T_OPEN_CURLY_BRACKET;
4950

@@ -195,6 +196,31 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
195196
}
196197
}//end if
197198

199+
if ($hasVisibilityModifier === true && $propertyInfo['is_abstract'] === true) {
200+
$scopePtr = $firstVisibilityModifier;
201+
$abstractPtr = $phpcsFile->findPrevious(T_ABSTRACT, ($stackPtr - 1));
202+
if ($abstractPtr > $scopePtr) {
203+
$error = 'The abstract declaration must come before the visibility declaration';
204+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AbstractAfterVisibility');
205+
if ($fix === true) {
206+
$phpcsFile->fixer->beginChangeset();
207+
208+
for ($i = ($abstractPtr + 1); $abstractPtr < $stackPtr; $i++) {
209+
if ($tokens[$i]['code'] !== T_WHITESPACE) {
210+
break;
211+
}
212+
213+
$phpcsFile->fixer->replaceToken($i, '');
214+
}
215+
216+
$phpcsFile->fixer->replaceToken($abstractPtr, '');
217+
$phpcsFile->fixer->addContentBefore($scopePtr, $tokens[$abstractPtr]['content'].' ');
218+
219+
$phpcsFile->fixer->endChangeset();
220+
}
221+
}
222+
}//end if
223+
198224
if ($hasVisibilityModifier === true && $propertyInfo['is_static'] === true) {
199225
$scopePtr = $lastVisibilityModifier;
200226
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,11 @@ class AsymmetricVisibility {
126126
final static protected(set) bool $wrongOrder12;
127127
static public(set) final bool $wrongOrder13;
128128
}
129+
130+
abstract class AbstractProperties {
131+
abstract public int $foo { get; }
132+
abstract protected (D|N)|false $foo { set; }
133+
abstract array $foo { get; }
134+
public ABSTRACT ?int $wrongOrder1 { set; }
135+
protected abstract ?string $wrongOrder2 { get; }
136+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.inc.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ class AsymmetricVisibility {
123123
final protected(set) static bool $wrongOrder12;
124124
final public(set) static bool $wrongOrder13;
125125
}
126+
127+
abstract class AbstractProperties {
128+
abstract public int $foo { get; }
129+
abstract protected (D|N)|false $foo { set; }
130+
abstract array $foo { get; }
131+
ABSTRACT public ?int $wrongOrder1 { set; }
132+
abstract protected ?string $wrongOrder2 { get; }
133+
}

src/Standards/PSR2/Tests/Classes/PropertyDeclarationUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function getErrorList()
7676
125 => 1,
7777
126 => 1,
7878
127 => 2,
79+
132 => 1,
80+
133 => 1,
81+
134 => 1,
82+
135 => 1,
7983
];
8084

8185
}//end getErrorList()

0 commit comments

Comments
 (0)