Skip to content

Commit

Permalink
bug #3893 Fix handling /** and */ on the same line as the first and/o…
Browse files Browse the repository at this point in the history
…r last annotation (dmvdbrugge)

This PR was merged into the 2.15 branch.

Discussion
----------

Fix handling /** and */ on the same line as the first and/or last annotation

In both `Annotation::remove()` and `PhpdocNoEmptyReturnFixer`

This PR is sort-of combined with #3891

Commits
-------

ad23563 Fix handling `/**` and `*/` on the same line as the first and/or last annotation
  • Loading branch information
SpacePossum committed Apr 19, 2020
2 parents 45e0d57 + ad23563 commit d945442
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/DocBlock/Annotation.php
Expand Up @@ -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();
Expand Down Expand Up @@ -286,7 +302,7 @@ private function getTypesContent()
}

$matchingResult = Preg::match(
'{^(?:\s*\*|/\*\*)\s*@'.$name.'\s+'.self::REGEX_TYPES.'(?:\h.*)?$}sx',
'{^(?:\s*\*|/\*\*)\s*@'.$name.'\s+'.self::REGEX_TYPES.'(?:[*\h].*)?$}sx',
$this->lines[0]->getContent(),
$matches
);
Expand Down
52 changes: 52 additions & 0 deletions tests/DocBlock/AnnotationTest.php
Expand Up @@ -213,6 +213,58 @@ 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();
static::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
Expand Down
38 changes: 38 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php
Expand Up @@ -58,6 +58,44 @@ public function testFixNull()
* @return null
*/
EOF;

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

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

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

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

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

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

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

0 comments on commit d945442

Please sign in to comment.