decouple hasDescription() method

This commit is contained in:
TomasVotruba 2020-02-29 15:43:38 +01:00
parent 53ac8fcc41
commit 24833bb00e
3 changed files with 28 additions and 16 deletions

View File

@ -257,10 +257,7 @@ final class PhpDocInfoPrinter
$output .= $tagSpaceSeparator;
}
if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES) && (property_exists(
$phpDocTagNode->value,
'description'
) && $phpDocTagNode->value->description)) {
if ($this->hasDescription($phpDocTagNode)) {
$quotedDescription = preg_quote($phpDocTagNode->value->description, '#');
$pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+');
$nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', $phpDocTagNode->value->description);
@ -344,4 +341,12 @@ final class PhpDocInfoPrinter
return $matches['space'] ?? '';
}
private function hasDescription(PhpDocTagNode $phpDocTagNode): bool
{
return $phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES) && (property_exists(
$phpDocTagNode->value,
'description'
) && $phpDocTagNode->value->description);
}
}

View File

@ -0,0 +1,3 @@
/**
* @return (string|bool)[]
*/

View File

@ -16,22 +16,15 @@ final class PhpDocInfoPrinterTest extends AbstractPhpDocInfoPrinterTest
*/
public function test(string $docFilePath): void
{
$docComment = FileSystem::read($docFilePath);
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, new Nop());
$this->assertSame($docComment, $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo));
$this->doComparePrintedFileEquals($docFilePath, $docFilePath);
}
public function testRemoveSpace(): void
{
$docComment = FileSystem::read(__DIR__ . '/FixtureChanged/with_space.txt');
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, new Nop());
$expectedDocComment = FileSystem::read(__DIR__ . '/FixtureChanged/with_space_expected.txt.inc');
$printedDocComment = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$this->assertSame($expectedDocComment, $printedDocComment);
$this->doComparePrintedFileEquals(
__DIR__ . '/FixtureChanged/with_space.txt',
__DIR__ . '/FixtureChanged/with_space_expected.txt.inc'
);
}
public function provideData(): Iterator
@ -59,4 +52,15 @@ final class PhpDocInfoPrinterTest extends AbstractPhpDocInfoPrinterTest
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureEmpty', '*.txt');
}
private function doComparePrintedFileEquals(string $inputDocFile, string $expectedOutputDocFile): void
{
$docComment = FileSystem::read($inputDocFile);
$phpDocInfo = $this->createPhpDocInfoFromDocCommentAndNode($docComment, new Nop());
$printedDocComment = $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
$expectedDocComment = FileSystem::read($expectedOutputDocFile);
$this->assertSame($expectedDocComment, $printedDocComment);
}
}