Skip to content

Commit c67d872

Browse files
committed
minor #3891 PhpdocNoEmptyReturnFixer - account for null[] (dmvdbrugge)
This PR was merged into the 2.12 branch. Discussion ---------- PhpdocNoEmptyReturnFixer - account for null[] This fixes #3869 Commits ------- 23084e9 PhpdocNoEmptyReturnFixer - account for null[]
2 parents 7774286 + 23084e9 commit c67d872

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

src/DocBlock/Annotation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ public function setTypes(array $types)
226226
$this->clearCache();
227227
}
228228

229+
/**
230+
* Get the normalized types associated with this annotation, so they can easily be compared.
231+
*
232+
* @return string[]
233+
*/
234+
public function getNormalizedTypes()
235+
{
236+
$normalized = array_map(static function ($type) {
237+
return strtolower($type);
238+
}, $this->getTypes());
239+
240+
sort($normalized);
241+
242+
return $normalized;
243+
}
244+
229245
/**
230246
* Remove this annotation by removing all its lines.
231247
*/

src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use PhpCsFixer\DocBlock\DocBlock;
1818
use PhpCsFixer\FixerDefinition\CodeSample;
1919
use PhpCsFixer\FixerDefinition\FixerDefinition;
20-
use PhpCsFixer\Preg;
2120
use PhpCsFixer\Tokenizer\Token;
2221
use PhpCsFixer\Tokenizer\Tokens;
2322

@@ -105,7 +104,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
105104
*/
106105
private function fixAnnotation(DocBlock $doc, Annotation $annotation)
107106
{
108-
if (1 === Preg::match('/@return\s+(void|null)(?!\|)/', $doc->getLine($annotation->getStart())->getContent())) {
107+
$types = $annotation->getNormalizedTypes();
108+
109+
if (1 === count($types) && ('null' === $types[0] || 'void' === $types[0])) {
109110
$annotation->remove();
110111
}
111112
}

tests/DocBlock/AnnotationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,29 @@ public function provideTypesCases()
342342
];
343343
}
344344

345+
/**
346+
* @param string[] $expected
347+
* @param string $input
348+
*
349+
* @dataProvider provideNormalizedTypesCases
350+
*/
351+
public function testNormalizedTypes($expected, $input)
352+
{
353+
$line = new Line($input);
354+
$tag = new Annotation([$line]);
355+
356+
$this->assertSame($expected, $tag->getNormalizedTypes());
357+
}
358+
359+
public function provideNormalizedTypesCases()
360+
{
361+
return [
362+
[['null', 'string'], '* @param StRiNg|NuLl $foo'],
363+
[['void'], '* @return Void'],
364+
[['bar', 'baz', 'foo', 'null', 'qux'], '* @return Foo|Bar|Baz|Qux|null'],
365+
];
366+
}
367+
345368
public function testGetTypesOnBadTag()
346369
{
347370
$this->expectException(\RuntimeException::class);

tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,46 @@ public function testFixNull()
5858
* @return null
5959
*/
6060

61+
EOF;
62+
63+
$this->doTest($expected, $input);
64+
}
65+
66+
public function testFixVoidCaseInsensitive()
67+
{
68+
$expected = <<<'EOF'
69+
<?php
70+
/**
71+
*/
72+
73+
EOF;
74+
75+
$input = <<<'EOF'
76+
<?php
77+
/**
78+
* @return vOId
79+
*/
80+
81+
EOF;
82+
83+
$this->doTest($expected, $input);
84+
}
85+
86+
public function testFixNullCaseInsensitive()
87+
{
88+
$expected = <<<'EOF'
89+
<?php
90+
/**
91+
*/
92+
93+
EOF;
94+
95+
$input = <<<'EOF'
96+
<?php
97+
/**
98+
* @return nULl
99+
*/
100+
61101
EOF;
62102

63103
$this->doTest($expected, $input);
@@ -123,6 +163,19 @@ public function testOtherDoNothing()
123163
* @return int|null
124164
*/
125165

166+
EOF;
167+
168+
$this->doTest($expected);
169+
}
170+
171+
public function testYetAnotherDoNothing()
172+
{
173+
$expected = <<<'EOF'
174+
<?php
175+
/**
176+
* @return null[]|string[]
177+
*/
178+
126179
EOF;
127180

128181
$this->doTest($expected);

0 commit comments

Comments
 (0)