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

Allow multiple spaces in PropertyDeclaration sniff to respect more carefully the PSR-12. #3756

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 10 additions & 12 deletions src/Standards/PSR2/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)

if ($propertyInfo['type'] !== '') {
$typeToken = $propertyInfo['type_end_token'];
$error = 'There must be 1 space after the property type declaration; %s found';
$error = 'There must be a space after the property type declaration; %s found';
if ($tokens[($typeToken + 1)]['code'] !== T_WHITESPACE) {
$data = ['0'];
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
Expand All @@ -85,7 +85,9 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
if ($tokens[$next]['line'] !== $tokens[$typeToken]['line']) {
$found = 'newline';
} else {
$found = $tokens[($typeToken + 1)]['length'];
// According to PSR-12: "There MUST be a space between type declaration and property name."
// This does not mean a single space only is expected, but at least 1. So, if many, ignore sniff.
return;
}

$data = [$found];
Expand All @@ -96,17 +98,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr)
} else {
$fix = $phpcsFile->addFixableError($error, $typeToken, 'SpacingAfterType', $data);
if ($fix === true) {
if ($found === 'newline') {
$phpcsFile->fixer->beginChangeset();
for ($x = ($typeToken + 1); $x < $next; $x++) {
$phpcsFile->fixer->replaceToken($x, '');
}

$phpcsFile->fixer->addContent($typeToken, ' ');
$phpcsFile->fixer->endChangeset();
} else {
$phpcsFile->fixer->replaceToken(($typeToken + 1), ' ');
$phpcsFile->fixer->beginChangeset();
for ($x = ($typeToken + 1); $x < $next; $x++) {
$phpcsFile->fixer->replaceToken($x, '');
}

$phpcsFile->fixer->addContent($typeToken, ' ');
$phpcsFile->fixer->endChangeset();
}
}
}//end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MyClass
{
public string $var = null;
protected ?Folder\ClassName $var = null;
public int $var = null;
public int $var = null;
public static int /*comment*/$var = null;
}

Expand All @@ -74,7 +74,7 @@ class ReadOnlyProp {
$bar,
$var = null;

protected readonly ?string $foo;
protected readonly ?string $foo;

readonly array $foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ public function getErrorList()
62 => 1,
68 => 1,
69 => 1,
71 => 1,
72 => 1,
76 => 1,
80 => 1,
82 => 1,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author
* @copyright 1997 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 1994-1997 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://www.php.net/license/3_0.txt
* @summary An unknown summary tag
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author
* @copyright 1997 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 1994-1997 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2022 Squiz Pty Ltd (ABN 77 084 670 600)
* @copyright 2023 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://www.php.net/license/3_0.txt
* @summary An unknown summary tag
*
Expand Down