diff --git a/src/DocBlock/Annotation.php b/src/DocBlock/Annotation.php index f84a00db349..f8cd52ac9af 100644 --- a/src/DocBlock/Annotation.php +++ b/src/DocBlock/Annotation.php @@ -248,7 +248,23 @@ public function getNormalizedTypes() public function remove() { foreach ($this->lines as $line) { - $line->remove(); + if ($line->isTheStart() && $line->isTheEnd()) { + // Single line doc block, remove entirely + $line->remove(); + } elseif ($line->isTheStart()) { + // Multi line doc block, but start is on the same line as the first annotation, keep only the start + $content = Preg::replace('#(\s*/\*\*).*#', '$1', $line->getContent()); + + $line->setContent($content); + } elseif ($line->isTheEnd()) { + // Multi line doc block, but end is on the same line as the last annotation, keep only the end + $content = Preg::replace('#(\s*)\S.*(\*/.*)#', '$1$2', $line->getContent()); + + $line->setContent($content); + } else { + // Multi line doc block, neither start nor end on this line, can be removed safely + $line->remove(); + } } $this->clearCache(); @@ -286,7 +302,7 @@ private function getTypesContent() } $matchingResult = Preg::match( - '{^(?:\s*\*|/\*\*)\s*@'.$name.'\s+'.self::REGEX_TYPES.'(?:[ \t].*)?$}sx', + '{^(?:\s*\*|/\*\*)\s*@'.$name.'\s+'.self::REGEX_TYPES.'(?:[* \t].*)?$}sx', $this->lines[0]->getContent(), $matches ); diff --git a/tests/DocBlock/AnnotationTest.php b/tests/DocBlock/AnnotationTest.php index 9dfce595e13..521644a0a85 100644 --- a/tests/DocBlock/AnnotationTest.php +++ b/tests/DocBlock/AnnotationTest.php @@ -213,6 +213,46 @@ public function provideRemoveCases() return $cases; } + /** + * @param string $expected + * @param string $input + * + * @dataProvider provideRemoveEdgeCasesCases + */ + public function testRemoveEdgeCases($expected, $input) + { + $doc = new DocBlock($input); + $annotation = $doc->getAnnotation(0); + + $annotation->remove(); + $this->assertSame($expected, $doc->getContent()); + } + + public function provideRemoveEdgeCasesCases() + { + return [ + // Single line + ['', '/** @return null*/'], + ['', '/** @return null */'], + ['', '/** @return null */'], + + // Multi line, annotation on start line + ['/** + */', '/** @return null + */'], + ['/** + */', '/** @return null '.' + */'], + // Multi line, annotation on end line + ['/** + */', '/** + * @return null*/'], + ['/** + */', '/** + * @return null */'], + ]; + } + /** * @param string $input * @param string[] $expected diff --git a/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php b/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php index 9c0d275496b..c982f29dfa5 100644 --- a/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php +++ b/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php @@ -58,6 +58,44 @@ public function testFixNull() * @return null */ +EOF; + + $this->doTest($expected, $input); + } + + public function testFixNullWithEndOnSameLine() + { + $expected = <<<'EOF' +doTest($expected, $input); + } + + public function testFixNullWithEndOnSameLineNoSpace() + { + $expected = <<<'EOF' +doTest($expected, $input);