Skip to content

Commit d966f64

Browse files
committed
Fix: do not remove nullable @var
1 parent 1d44649 commit d966f64

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ In your PHP CS Fixer configuration register fixers and use them:
9292
- **PhpdocNoIncorrectVarAnnotationFixer** - `@var` should be correct in the code.
9393
```diff
9494
<?php
95-
-/** @var LoggerInterface $foo */
95+
-/** @var Foo $foo */
9696
+
97-
$bar = new Logger();
97+
$bar = new Foo();
9898

9999
```
100100

src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public function getDefinition() : FixerDefinition
1717
return new FixerDefinition(
1818
'`@var` should be correct in the code.',
1919
[new CodeSample('<?php
20-
/** @var LoggerInterface $foo */
21-
$bar = new Logger();
20+
/** @var Foo $foo */
21+
$bar = new Foo();
2222
')]
2323
);
2424
}
@@ -40,7 +40,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void
4040
}
4141

4242
// remove ones not having type at the beginning
43-
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, '/@var\s+[\\\\a-zA-Z_\x7f-\xff]/');
43+
$this->removeVarAnnotationNotMatchingPattern($tokens, $index, '/@var\s+[\?\\\\a-zA-Z_\x7f-\xff]/');
4444

4545
$nextIndex = $tokens->getNextMeaningfulToken($index);
4646

tests/Fixer/PhpdocNoIncorrectVarAnnotationFixerTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function provideFixCases() : \Iterator
4545
{
4646
yield [
4747
'<?php
48-
/** @var LoggerInterface $foo */
49-
$foo = new Logger();
48+
/** @var Foo $foo */
49+
$foo = new Foo();
5050
',
5151
];
5252

@@ -57,6 +57,13 @@ public function provideFixCases() : \Iterator
5757
',
5858
];
5959

60+
yield [
61+
'<?php
62+
/** @var ?Foo $foo */
63+
$foo = new Foo();
64+
',
65+
];
66+
6067
yield [
6168
'<?php
6269

0 commit comments

Comments
 (0)