Skip to content

PHP 8.4 | PSR2/PropertyDeclaration: add support for abstract properties #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: php-8.4/file-getmemberproperties-support-abstract-properties
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
$find[] = T_VAR;
$find[] = T_READONLY;
$find[] = T_FINAL;
$find[] = T_ABSTRACT;
$find[] = T_SEMICOLON;
$find[] = T_OPEN_CURLY_BRACKET;

Expand Down Expand Up @@ -195,6 +196,31 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
}
}//end if

if ($hasVisibilityModifier === true && $propertyInfo['is_abstract'] === true) {
$scopePtr = $firstVisibilityModifier;
$abstractPtr = $phpcsFile->findPrevious(T_ABSTRACT, ($stackPtr - 1));
if ($abstractPtr > $scopePtr) {
$error = 'The abstract declaration must come before the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'AbstractAfterVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

for ($i = ($abstractPtr + 1); $abstractPtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}

$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($abstractPtr, '');
$phpcsFile->fixer->addContentBefore($scopePtr, $tokens[$abstractPtr]['content'].' ');

$phpcsFile->fixer->endChangeset();
}
}
}//end if

if ($hasVisibilityModifier === true && $propertyInfo['is_static'] === true) {
$scopePtr = $lastVisibilityModifier;
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,11 @@ class AsymmetricVisibility {
final static protected(set) bool $wrongOrder12;
static public(set) final bool $wrongOrder13;
}

abstract class AbstractProperties {
abstract public int $foo { get; }
abstract protected (D|N)|false $foo { set; }
abstract array $foo { get; }
public ABSTRACT ?int $wrongOrder1 { set; }
protected abstract ?string $wrongOrder2 { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,11 @@ class AsymmetricVisibility {
final protected(set) static bool $wrongOrder12;
final public(set) static bool $wrongOrder13;
}

abstract class AbstractProperties {
abstract public int $foo { get; }
abstract protected (D|N)|false $foo { set; }
abstract array $foo { get; }
ABSTRACT public ?int $wrongOrder1 { set; }
abstract protected ?string $wrongOrder2 { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function getErrorList()
125 => 1,
126 => 1,
127 => 2,
132 => 1,
133 => 1,
134 => 1,
135 => 1,
];

}//end getErrorList()
Expand Down
Loading