Skip to content

Commit

Permalink
PhpdocNoEmptyReturnFixer - account for null[]
Browse files Browse the repository at this point in the history
- Use $annotation->getTypes() instead of Preg::match
- Apply yoda
- Use normalized types
- Add test for new method
- With test params in correct order
- Add phpdoc and use anonymous function instead of string
  • Loading branch information
dmvdbrugge committed Aug 16, 2018
1 parent 76238aa commit 23084e9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
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[]
*/
EOF;

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

0 comments on commit 23084e9

Please sign in to comment.