Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![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)
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
![Tests](https://img.shields.io/badge/tests-3801-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-3802-brightgreen.svg)
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)

[![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)
Expand Down
14 changes: 13 additions & 1 deletion src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
// remove ones not having type at the beginning
self::removeVarAnnotationNotMatchingPattern($tokens, $index, '/@var\\s+[\\?\\\\a-zA-Z_\\x7f-\\xff]/');

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

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

private static function getIndexAfterPhpDoc(Tokens $tokens, int $index): ?int
{
$nextIndex = $tokens->getNextMeaningfulToken($index);

while ($nextIndex !== null && \defined('T_ATTRIBUTE') && $tokens[$nextIndex]->isGivenKind(\T_ATTRIBUTE)) {
$nextIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ATTRIBUTE, $nextIndex);
$nextIndex = $tokens->getNextMeaningfulToken($nextIndex);
}

return $nextIndex;
}

private static function removeForClassElement(Tokens $tokens, int $index, int $propertyStartIndex): void
{
$tokenKinds = [\T_NS_SEPARATOR, \T_STATIC, \T_STRING, \T_WHITESPACE, CT::T_ARRAY_TYPEHINT, CT::T_NULLABLE_TYPE, CT::T_TYPE_ALTERNATION];
Expand Down
17 changes: 15 additions & 2 deletions tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,15 @@ public function testFix80(string $expected, ?string $input = null): void
*/
public static function provideFix80Cases(): iterable
{
yield 'keep correct PHPDoc for class properties, PHP 8.0' => [
yield 'keep correct PHPDoc for class properties' => [
'<?php class Foo
{
/** @var int|string */
private int|string $intOrString;
}',
];

yield 'keep correct PHPDoc for promoted properties, PHP 8.0' => [
yield 'keep correct PHPDoc for promoted properties' => [
'<?php class Foo
{
public function __construct(
Expand All @@ -465,6 +465,19 @@ public function __construct(
) {}
}',
];

yield 'keep correct PHPDoc when there is attribute' => [
<<<'PHP'
<?php class Foo
{
/** @var int|string */
#[Attribute1]
#[Attribute2('foo')]
#[Attribute3(4)]
private int|string $intOrString;
}
PHP,
];
}

/**
Expand Down
Loading