Skip to content
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

PHP 8.1 | PSR2/PropertyDeclaration: add check for visibility before readonly keyword #3637

Merged
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
68 changes: 54 additions & 14 deletions src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,70 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
$phpcsFile->addError($error, $stackPtr, 'ScopeMissing', $data);
}

/*
* Note: per PSR-PER section 4.6, the order should be:
* - Inheritance modifier: `abstract` or `final`.
* - Visibility modifier: `public`, `protected`, or `private`.
* - Scope modifier: `static`.
* - Mutation modifier: `readonly`.
* - Type declaration.
* - Name.
*
* Ref: https://www.php-fig.org/per/coding-style/#46-modifier-keywords
*
* At this time (PHP 8.2), inheritance modifiers cannot be applied to properties and
* the `static` and `readonly` modifiers are mutually exclusive and cannot be used together.
*
* Based on that, the below modifier keyword order checks are sufficient (for now).
*/

if ($propertyInfo['scope_specified'] === true && $propertyInfo['is_static'] === true) {
$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
$staticPtr = $phpcsFile->findPrevious(T_STATIC, ($stackPtr - 1));
if ($scopePtr < $staticPtr) {
return;
}
if ($scopePtr > $staticPtr) {
$error = 'The static declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

$error = 'The static declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'StaticBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();
for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
}

for ($i = ($scopePtr + 1); $scopePtr < $stackPtr; $i++) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
break;
$phpcsFile->fixer->replaceToken($i, '');
}

$phpcsFile->fixer->replaceToken($i, '');
$phpcsFile->fixer->replaceToken($scopePtr, '');
$phpcsFile->fixer->addContentBefore($staticPtr, $propertyInfo['scope'].' ');

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

if ($propertyInfo['scope_specified'] === true && $propertyInfo['is_readonly'] === true) {
$scopePtr = $phpcsFile->findPrevious(Tokens::$scopeModifiers, ($stackPtr - 1));
$readonlyPtr = $phpcsFile->findPrevious(T_READONLY, ($stackPtr - 1));
if ($scopePtr > $readonlyPtr) {
$error = 'The readonly declaration must come after the visibility declaration';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ReadonlyBeforeVisibility');
if ($fix === true) {
$phpcsFile->fixer->beginChangeset();

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

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

$phpcsFile->fixer->replaceToken($scopePtr, '');
$phpcsFile->fixer->addContentBefore($staticPtr, $propertyInfo['scope'].' ');
$phpcsFile->fixer->replaceToken($scopePtr, '');
$phpcsFile->fixer->addContentBefore($readonlyPtr, $propertyInfo['scope'].' ');

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ class ReadOnlyProp {
protected readonly ?string $foo;

readonly array $foo;

readonly public int $wrongOrder1;

readonly protected ?string $wrongOrder2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ class ReadOnlyProp {
protected readonly ?string $foo;

readonly array $foo;

public readonly int $wrongOrder1;

protected readonly ?string $wrongOrder2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function getErrorList()
76 => 1,
80 => 1,
82 => 1,
84 => 1,
86 => 1,
];

}//end getErrorList()
Expand Down