Skip to content

Commit 9512d68

Browse files
committed
Merge branch 'master' into 4.x
2 parents cbf45ef + edf4757 commit 9512d68

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
@@ -62,6 +62,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
6262
$find[] = T_VAR;
6363
$find[] = T_READONLY;
6464
$find[] = T_FINAL;
65+
$find[] = T_ABSTRACT;
6566
$find[] = T_SEMICOLON;
6667
$find[] = T_OPEN_CURLY_BRACKET;
6768

@@ -203,6 +204,31 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
203204
}
204205
}//end if
205206

207+
if ($hasVisibilityModifier === true && $propertyInfo['is_abstract'] === true) {
208+
$scopePtr = $firstVisibilityModifier;
209+
$abstractPtr = $phpcsFile->findPrevious(T_ABSTRACT, ($stackPtr - 1));
210+
if ($abstractPtr > $scopePtr) {
211+
$error = 'The abstract declaration must come before the visibility declaration';
212+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AbstractAfterVisibility');
213+
if ($fix === true) {
214+
$phpcsFile->fixer->beginChangeset();
215+
216+
for ($i = ($abstractPtr + 1); $abstractPtr < $stackPtr; $i++) {
217+
if ($tokens[$i]['code'] !== T_WHITESPACE) {
218+
break;
219+
}
220+
221+
$phpcsFile->fixer->replaceToken($i, '');
222+
}
223+
224+
$phpcsFile->fixer->replaceToken($abstractPtr, '');
225+
$phpcsFile->fixer->addContentBefore($scopePtr, $tokens[$abstractPtr]['content'].' ');
226+
227+
$phpcsFile->fixer->endChangeset();
228+
}
229+
}
230+
}//end if
231+
206232
if ($hasVisibilityModifier === true && $propertyInfo['is_static'] === true) {
207233
$scopePtr = $lastVisibilityModifier;
208234
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,11 @@ class AsymmetricVisibility {
131131
final static protected(set) bool $wrongOrder12;
132132
static public(set) final bool $wrongOrder13;
133133
}
134+
135+
abstract class AbstractProperties {
136+
abstract public int $foo { get; }
137+
abstract protected (D|N)|false $foo { set; }
138+
abstract array $foo { get; }
139+
public ABSTRACT ?int $wrongOrder1 { set; }
140+
protected abstract ?string $wrongOrder2 { get; }
141+
}

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

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public function getErrorList($testFile='')
8080
130 => 1,
8181
131 => 1,
8282
132 => 2,
83+
137 => 1,
84+
138 => 1,
85+
139 => 1,
86+
140 => 1,
8387
];
8488

8589
default:

0 commit comments

Comments
 (0)