Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhpdocNoEmptyReturnFixer - account for null[] #3891

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/DocBlock/Annotation.php
Expand Up @@ -226,6 +226,22 @@ public function setTypes(array $types)
$this->clearCache();
}

/**
* Get the normalized types associated with this annotation, so they can easily be compared.
*
* @return string[]
*/
public function getNormalizedTypes()
{
$normalized = array_map(static function ($type) {
return strtolower($type);
}, $this->getTypes());

sort($normalized);

return $normalized;
}

/**
* Remove this annotation by removing all its lines.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php
Expand Up @@ -17,7 +17,6 @@
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

Expand Down Expand Up @@ -105,7 +104,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
*/
private function fixAnnotation(DocBlock $doc, Annotation $annotation)
{
if (1 === Preg::match('/@return\s+(void|null)(?!\|)/', $doc->getLine($annotation->getStart())->getContent())) {
$types = $annotation->getNormalizedTypes();

if (1 === count($types) && ('null' === $types[0] || 'void' === $types[0])) {
$annotation->remove();
}
}
Expand Down
23 changes: 23 additions & 0 deletions tests/DocBlock/AnnotationTest.php
Expand Up @@ -342,6 +342,29 @@ public function provideTypesCases()
];
}

/**
* @param string[] $expected
* @param string $input
*
* @dataProvider provideNormalizedTypesCases
*/
public function testNormalizedTypes($expected, $input)
{
$line = new Line($input);
$tag = new Annotation([$line]);

$this->assertSame($expected, $tag->getNormalizedTypes());
}

public function provideNormalizedTypesCases()
{
return [
[['null', 'string'], '* @param StRiNg|NuLl $foo'],
[['void'], '* @return Void'],
[['bar', 'baz', 'foo', 'null', 'qux'], '* @return Foo|Bar|Baz|Qux|null'],
];
}

public function testGetTypesOnBadTag()
{
$this->expectException(\RuntimeException::class);
Expand Down
53 changes: 53 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php
Expand Up @@ -58,6 +58,46 @@ public function testFixNull()
* @return null
*/

EOF;

$this->doTest($expected, $input);
}

public function testFixVoidCaseInsensitive()
{
$expected = <<<'EOF'
<?php
/**
*/

EOF;

$input = <<<'EOF'
<?php
/**
* @return vOId
*/

EOF;

$this->doTest($expected, $input);
}

public function testFixNullCaseInsensitive()
{
$expected = <<<'EOF'
<?php
/**
*/

EOF;

$input = <<<'EOF'
<?php
/**
* @return nULl
*/

EOF;

$this->doTest($expected, $input);
Expand Down Expand Up @@ -123,6 +163,19 @@ public function testOtherDoNothing()
* @return int|null
*/

EOF;

$this->doTest($expected);
}

public function testYetAnotherDoNothing()
{
$expected = <<<'EOF'
<?php
/**
* @return null[]|string[]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test to make sure the change is case-insensitive, i.e. NULL[] and VOID[] get fixed as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, they didn't because Annotation::getTypes isn't normalized. Fix incoming.

*/

EOF;

$this->doTest($expected);
Expand Down