From 23084e9d817146b6ea3506921f1c64b6da9c1206 Mon Sep 17 00:00:00 2001 From: Dave van der Brugge Date: Mon, 9 Jul 2018 11:38:10 +0200 Subject: [PATCH] PhpdocNoEmptyReturnFixer - account for null[] - 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 --- src/DocBlock/Annotation.php | 16 ++++++ src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php | 5 +- tests/DocBlock/AnnotationTest.php | 23 ++++++++ .../Phpdoc/PhpdocNoEmptyReturnFixerTest.php | 53 +++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/src/DocBlock/Annotation.php b/src/DocBlock/Annotation.php index f43f8a61af0..ed679d7c65a 100644 --- a/src/DocBlock/Annotation.php +++ b/src/DocBlock/Annotation.php @@ -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. */ diff --git a/src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php b/src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php index ca713f46e5b..97700775a6f 100644 --- a/src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php +++ b/src/Fixer/Phpdoc/PhpdocNoEmptyReturnFixer.php @@ -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; @@ -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(); } } diff --git a/tests/DocBlock/AnnotationTest.php b/tests/DocBlock/AnnotationTest.php index 26343f1cc8b..9dfce595e13 100644 --- a/tests/DocBlock/AnnotationTest.php +++ b/tests/DocBlock/AnnotationTest.php @@ -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); diff --git a/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php b/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php index c0f59f08e0f..15c7def79d7 100644 --- a/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php +++ b/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php @@ -58,6 +58,46 @@ public function testFixNull() * @return null */ +EOF; + + $this->doTest($expected, $input); + } + + public function testFixVoidCaseInsensitive() + { + $expected = <<<'EOF' +doTest($expected, $input); + } + + public function testFixNullCaseInsensitive() + { + $expected = <<<'EOF' +doTest($expected, $input); @@ -123,6 +163,19 @@ public function testOtherDoNothing() * @return int|null */ +EOF; + + $this->doTest($expected); + } + + public function testYetAnotherDoNothing() + { + $expected = <<<'EOF' +doTest($expected);