Skip to content

Commit dffd057

Browse files
authored
PhpdocNoIncorrectVarAnnotationFixer - fix for typed properties (#652)
1 parent 254caec commit dffd057

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![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)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-3030-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-3033-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpCsFixer\FixerDefinition\FixerDefinition;
1919
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
2020
use PhpCsFixer\Preg;
21+
use PhpCsFixer\Tokenizer\CT;
2122
use PhpCsFixer\Tokenizer\Token;
2223
use PhpCsFixer\Tokenizer\Tokens;
2324
use PhpCsFixerCustomFixers\TokenRemover;
@@ -71,7 +72,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7172
}
7273

7374
if ($tokens[$nextIndex]->isGivenKind([\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_VAR, \T_STATIC])) {
74-
$this->removeForClassElement($tokens, $index);
75+
$this->removeForClassElement($tokens, $index, $nextIndex);
7576
continue;
7677
}
7778

@@ -94,20 +95,27 @@ private function isTokenCandidate(Token $token): bool
9495
return $token->isGivenKind(\T_DOC_COMMENT) && \stripos($token->getContent(), '@var') !== false;
9596
}
9697

97-
private function removeForClassElement(Tokens $tokens, int $index): void
98+
private function removeForClassElement(Tokens $tokens, int $index, int $propertyStartIndex): void
9899
{
99-
/** @var int $nextIndex */
100-
$nextIndex = $tokens->getTokenNotOfKindsSibling($index, 1, [\T_PRIVATE, \T_PROTECTED, \T_PUBLIC, \T_VAR, \T_STATIC, \T_WHITESPACE]);
100+
$tokenKinds = [\T_NS_SEPARATOR, \T_STATIC, \T_STRING, \T_WHITESPACE, CT::T_ARRAY_TYPEHINT, CT::T_NULLABLE_TYPE, CT::T_TYPE_ALTERNATION];
101101

102-
if ($tokens[$nextIndex]->isGivenKind(\T_VARIABLE)) {
103-
if (Preg::match('/@var\h+(.+\h+)?\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $tokens[$index]->getContent()) === 1) {
104-
$this->removeVarAnnotation($tokens, $index, [$tokens[$nextIndex]->getContent()]);
105-
}
102+
if (\defined('T_READONLY')) {
103+
$tokenKinds[] = CT::T_TYPE_INTERSECTION;
104+
$tokenKinds[] = \T_READONLY;
105+
}
106+
107+
/** @var int $variableIndex */
108+
$variableIndex = $tokens->getTokenNotOfKindsSibling($propertyStartIndex, 1, $tokenKinds);
109+
110+
if (!$tokens[$variableIndex]->isGivenKind(\T_VARIABLE)) {
111+
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, null);
106112

107113
return;
108114
}
109115

110-
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, null);
116+
if (Preg::match('/@var\h+(.+\h+)?\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $tokens[$index]->getContent()) === 1) {
117+
$this->removeVarAnnotation($tokens, $index, [$tokens[$variableIndex]->getContent()]);
118+
}
111119
}
112120

113121
/**

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,60 @@ class Foo
230230
* @var int
231231
*/
232232
private $f;
233+
234+
/**
235+
* @var int
236+
*/
237+
private static $g;
233238
}
234239
',
235240
];
236241

242+
if (\PHP_VERSION_ID >= 70400) {
243+
yield 'keep correct PHPDoc for class properties, PHP 7.4' => [
244+
'<?php class Foo
245+
{
246+
/** @var array */
247+
private array $array;
248+
249+
/** @var bool */
250+
private bool $boolean;
251+
252+
/** @var null|string */
253+
private ?string $nullableString;
254+
255+
/** @var Bar */
256+
private Bar $bar;
257+
258+
/** @var Vendor\\Baz */
259+
private Vendor\\Baz $baz;
260+
}',
261+
];
262+
}
263+
264+
if (\PHP_VERSION_ID >= 80000) {
265+
yield 'keep correct PHPDoc for class properties, PHP 8.0' => [
266+
'<?php class Foo
267+
{
268+
/** @var int|string */
269+
private int|string $intOrString;
270+
}',
271+
];
272+
}
273+
274+
if (\PHP_VERSION_ID >= 80100) {
275+
yield 'keep correct PHPDoc for class properties, PHP 8.1' => [
276+
'<?php class Foo
277+
{
278+
/** @var string */
279+
private readonly string $readonlyString;
280+
281+
/** @var Bar&Vendor\\Baz */
282+
private Bar&Vendor\\Baz $barAndBaz;
283+
}',
284+
];
285+
}
286+
237287
yield 'remove PHPDoc for class properties' => [
238288
'<?php
239289
class Foo

tests/Readme/ReadmeCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @covers \PhpCsFixerCustomFixersDev\Readme\ReadmeCommand
2424
*
25-
* @requires PHP >=8.0
25+
* @requires PHP >=8.1
2626
*/
2727
final class ReadmeCommandTest extends TestCase
2828
{

0 commit comments

Comments
 (0)