Skip to content

Commit 636bbba

Browse files
authored
PhpdocNoIncorrectVarAnnotationFixer - do not remove PHPDoc when there is an attribute (#1057)
1 parent 0cf921c commit 636bbba

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
66
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
77
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
8-
![Tests](https://img.shields.io/badge/tests-3801-brightgreen.svg)
8+
![Tests](https://img.shields.io/badge/tests-3802-brightgreen.svg)
99
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
1010

1111
[![CI status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml/badge.svg)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions/workflows/ci.yaml)

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6666
// remove ones not having type at the beginning
6767
self::removeVarAnnotationNotMatchingPattern($tokens, $index, '/@var\\s+[\\?\\\\a-zA-Z_\\x7f-\\xff]/');
6868

69-
$nextIndex = $tokens->getNextMeaningfulToken($index);
69+
$nextIndex = self::getIndexAfterPhpDoc($tokens, $index);
7070

7171
if ($nextIndex === null) {
7272
self::removeVarAnnotationNotMatchingPattern($tokens, $index, null);
@@ -92,6 +92,18 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
9292
}
9393
}
9494

95+
private static function getIndexAfterPhpDoc(Tokens $tokens, int $index): ?int
96+
{
97+
$nextIndex = $tokens->getNextMeaningfulToken($index);
98+
99+
while ($nextIndex !== null && \defined('T_ATTRIBUTE') && $tokens[$nextIndex]->isGivenKind(\T_ATTRIBUTE)) {
100+
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $nextIndex);
101+
$nextIndex = $tokens->getNextMeaningfulToken($nextIndex);
102+
}
103+
104+
return $nextIndex;
105+
}
106+
95107
private static function removeForClassElement(Tokens $tokens, int $index, int $propertyStartIndex): void
96108
{
97109
$tokenKinds = [\T_NS_SEPARATOR, \T_STATIC, \T_STRING, \T_WHITESPACE, CT::T_ARRAY_TYPEHINT, CT::T_NULLABLE_TYPE, CT::T_TYPE_ALTERNATION];

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,15 @@ public function testFix80(string $expected, ?string $input = null): void
442442
*/
443443
public static function provideFix80Cases(): iterable
444444
{
445-
yield 'keep correct PHPDoc for class properties, PHP 8.0' => [
445+
yield 'keep correct PHPDoc for class properties' => [
446446
'<?php class Foo
447447
{
448448
/** @var int|string */
449449
private int|string $intOrString;
450450
}',
451451
];
452452

453-
yield 'keep correct PHPDoc for promoted properties, PHP 8.0' => [
453+
yield 'keep correct PHPDoc for promoted properties' => [
454454
'<?php class Foo
455455
{
456456
public function __construct(
@@ -465,6 +465,19 @@ public function __construct(
465465
) {}
466466
}',
467467
];
468+
469+
yield 'keep correct PHPDoc when there is attribute' => [
470+
<<<'PHP'
471+
<?php class Foo
472+
{
473+
/** @var int|string */
474+
#[Attribute1]
475+
#[Attribute2('foo')]
476+
#[Attribute3(4)]
477+
private int|string $intOrString;
478+
}
479+
PHP,
480+
];
468481
}
469482

470483
/**

0 commit comments

Comments
 (0)