Skip to content

Commit 29bdaee

Browse files
committed
Fixed DocCommentHelper::findDocCommentOwnerPointer()
1 parent fae26ee commit 29bdaee

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

SlevomatCodingStandard/Helpers/DocCommentHelper.php

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use function stripos;
1515
use function strtolower;
1616
use function trim;
17-
use const T_ABSTRACT;
1817
use const T_ATTRIBUTE;
19-
use const T_CLASS;
2018
use const T_CLOSE_CURLY_BRACKET;
2119
use const T_CONST;
2220
use const T_DOC_COMMENT_CLOSE_TAG;
@@ -25,21 +23,10 @@
2523
use const T_DOC_COMMENT_STRING;
2624
use const T_DOC_COMMENT_TAG;
2725
use const T_DOC_COMMENT_WHITESPACE;
28-
use const T_ENUM;
29-
use const T_FINAL;
3026
use const T_FUNCTION;
31-
use const T_INTERFACE;
3227
use const T_OPEN_CURLY_BRACKET;
33-
use const T_PRIVATE;
34-
use const T_PROTECTED;
35-
use const T_PUBLIC;
36-
use const T_READONLY;
3728
use const T_SEMICOLON;
38-
use const T_STATIC;
39-
use const T_TRAIT;
40-
use const T_VAR;
4129
use const T_VARIABLE;
42-
use const T_WHITESPACE;
4330

4431
/**
4532
* @internal
@@ -183,38 +170,36 @@ public static function findDocCommentOwnerPointer(File $phpcsFile, int $docComme
183170
{
184171
$tokens = $phpcsFile->getTokens();
185172

186-
$docCommentCloserPointer = $tokens[$docCommentOpenPointer]['comment_closer'];
187-
188173
if (self::isInline($phpcsFile, $docCommentOpenPointer)) {
189174
return null;
190175
}
191176

192177
$docCommentOwnerPointer = null;
193178

194-
for ($i = $docCommentCloserPointer + 1; $i < count($tokens); $i++) {
195-
if ($tokens[$i]['code'] === T_ATTRIBUTE) {
196-
$i = $tokens[$i]['attribute_closer'];
197-
continue;
179+
$i = $docCommentOpenPointer;
180+
do {
181+
$pointer = TokenHelper::findNext(
182+
$phpcsFile,
183+
[T_ATTRIBUTE, T_DOC_COMMENT_OPEN_TAG, T_FUNCTION, T_VARIABLE, T_CONST, ...TokenHelper::CLASS_TYPE_TOKEN_CODES],
184+
$i + 1,
185+
);
186+
if ($pointer === null) {
187+
break;
198188
}
199189

200-
if (in_array(
201-
$tokens[$i]['code'],
202-
[T_PUBLIC, T_PROTECTED, T_PRIVATE, T_VAR, T_READONLY, T_FINAL, T_STATIC, T_ABSTRACT, T_WHITESPACE],
203-
true,
204-
)) {
205-
continue;
190+
if ($tokens[$pointer]['code'] === T_DOC_COMMENT_OPEN_TAG) {
191+
return null;
206192
}
207193

208-
if (in_array(
209-
$tokens[$i]['code'],
210-
[T_FUNCTION, T_VARIABLE, T_CONST, ...TokenHelper::CLASS_TYPE_TOKEN_CODES],
211-
true,
212-
)) {
213-
$docCommentOwnerPointer = $i;
194+
if ($tokens[$pointer]['code'] === T_ATTRIBUTE) {
195+
$i = $tokens[$pointer]['attribute_closer'];
196+
continue;
214197
}
215198

199+
$docCommentOwnerPointer = $pointer;
216200
break;
217-
}
201+
202+
} while (true);
218203

219204
return $docCommentOwnerPointer;
220205
}
@@ -229,7 +214,7 @@ public static function isInline(File $phpcsFile, int $docCommentOpenPointer): bo
229214
$nextPointer !== null
230215
&& in_array(
231216
$tokens[$nextPointer]['code'],
232-
[T_PUBLIC, T_PROTECTED, T_PRIVATE, T_READONLY, T_FINAL, T_STATIC, T_ABSTRACT, T_CONST, T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM, T_ATTRIBUTE],
217+
[...TokenHelper::MODIFIERS_TOKEN_CODES, ...TokenHelper::CLASS_TYPE_TOKEN_CODES, T_ATTRIBUTE],
233218
true,
234219
)
235220
) {

tests/Helpers/DocCommentHelperTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHP_CodeSniffer\Files\File;
66
use function array_map;
7+
use function sprintf;
78
use const T_DOC_COMMENT_OPEN_TAG;
89

910
class DocCommentHelperTest extends TestCase
@@ -377,9 +378,10 @@ public function testIsInline(): void
377378
{
378379
$phpcsFile = $this->getTestedCodeSnifferFile();
379380

380-
foreach ([3, 10, 18, 32, 46, 51, 76, 99] as $line) {
381+
foreach ([3, 10, 32, 46, 51, 76, 99] as $line) {
381382
self::assertFalse(
382383
DocCommentHelper::isInline($phpcsFile, $this->findPointerByLineAndType($phpcsFile, $line, T_DOC_COMMENT_OPEN_TAG)),
384+
sprintf('Failed asserting that doc comment on line %d is not inline.', $line),
383385
);
384386
}
385387

tests/Sniffs/TypeHints/data/disallowMixedTypeHintNoErrors.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php // lint >= 8.5
22

33
/**
44
* @see mixed
@@ -32,11 +32,25 @@ public function foo(array $mixed)
3232
return $mixed === true;
3333
}
3434

35+
protected array $property;
36+
37+
public readonly array $data;
38+
3539
}
3640

3741
class WhateverOverridden extends Whatever
3842
{
3943

44+
/**
45+
* @var array<mixed>
46+
*/
47+
#[Override]
48+
protected array $property;
49+
50+
/** @var array<string, mixed> */
51+
#[Override]
52+
public readonly array $data;
53+
4054
/**
4155
* @var array<mixed>
4256
*/

0 commit comments

Comments
 (0)