Skip to content

Commit 6aa8c00

Browse files
committed
ClassMemberSpacingSniff: Fixed false positives because of attributes
1 parent 0249deb commit 6aa8c00

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

SlevomatCodingStandard/Sniffs/Classes/ClassMemberSpacingSniff.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use const T_ABSTRACT;
2222
use const T_ANON_CLASS;
2323
use const T_AS;
24+
use const T_ATTRIBUTE_END;
2425
use const T_CLOSE_CURLY_BRACKET;
2526
use const T_CONST;
2627
use const T_FINAL;
@@ -194,19 +195,31 @@ private function getMemberStartPointer(File $phpcsFile, int $memberPointer): int
194195
$tokens = $phpcsFile->getTokens();
195196

196197
$memberFirstCodePointer = $this->getMemberFirstCodePointer($phpcsFile, $memberPointer);
197-
$pointerBefore = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $memberFirstCodePointer - 1);
198-
199-
if (
200-
in_array($tokens[$pointerBefore]['code'], Tokens::$commentTokens, true)
201-
&& $tokens[$pointerBefore]['line'] + 1 === $tokens[$memberFirstCodePointer]['line']
202-
) {
203-
$pointerBeforeComment = TokenHelper::findPreviousEffective($phpcsFile, $pointerBefore - 1);
204-
if ($tokens[$pointerBeforeComment]['line'] !== $tokens[$pointerBefore]['line']) {
205-
return array_key_exists('comment_opener', $tokens[$pointerBefore])
206-
? $tokens[$pointerBefore]['comment_opener']
207-
: CommentHelper::getMultilineCommentStartPointer($phpcsFile, $pointerBefore);
198+
199+
do {
200+
$pointerBefore = TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $memberFirstCodePointer - 1);
201+
202+
if ($tokens[$pointerBefore]['code'] === T_ATTRIBUTE_END) {
203+
$memberFirstCodePointer = $tokens[$pointerBefore]['attribute_opener'];
204+
continue;
208205
}
209-
}
206+
207+
if (
208+
in_array($tokens[$pointerBefore]['code'], Tokens::$commentTokens, true)
209+
&& $tokens[$pointerBefore]['line'] + 1 === $tokens[$memberFirstCodePointer]['line']
210+
) {
211+
$pointerBeforeComment = TokenHelper::findPreviousEffective($phpcsFile, $pointerBefore - 1);
212+
if ($tokens[$pointerBeforeComment]['line'] !== $tokens[$pointerBefore]['line']) {
213+
$memberFirstCodePointer = array_key_exists('comment_opener', $tokens[$pointerBefore])
214+
? $tokens[$pointerBefore]['comment_opener']
215+
: CommentHelper::getMultilineCommentStartPointer($phpcsFile, $pointerBefore);
216+
continue;
217+
}
218+
}
219+
220+
break;
221+
222+
} while (true);
210223

211224
return $memberFirstCodePointer;
212225
}

tests/Sniffs/Classes/data/classMemberSpacingNoErrors.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ abstract class WithoutErrors
5656
/** @var string|null */
5757
private $two;
5858

59+
#[AttributeBeforeComment]
5960
/**
6061
* @return object
6162
*/
@@ -82,7 +83,11 @@ final public function two()
8283

8384
public $third;
8485

85-
abstract public function third();
86+
/**
87+
* @return mixed
88+
*/
89+
#[SomeAttribute]
90+
abstract public function thirdWithAttributeAndDocDomment();
8691

8792
/**
8893
* @codingStandardsIgnoreStart
@@ -92,7 +97,9 @@ abstract public function third();
9297
protected $forth;
9398
// @codingStandardsIgnoreEnd
9499

95-
public function forth()
100+
#[SomeAttribute]
101+
#[JustAnotherAttribute]
102+
public function forthWithAttribute()
96103
{
97104
}
98105

0 commit comments

Comments
 (0)