PhpDocInfoPrinter: Support adding phpdocs on inline-html (#88)

This commit is contained in:
Markus Staab 2021-05-25 18:27:57 +02:00 committed by GitHub
parent 63faa6a33a
commit 81a649ff14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<h1><?php echo $hello1; ?></h1>
<?php
// some php
?>
<h2><?php echo $hello2; ?></h2>
<?php
// some more php
?>
-----
<?php
/**
* @var string $hello1
*/
?>
<h1><?php echo $hello1; ?></h1>
<?php
// some php
?>
<?php
/**
* @var string $hello2
*/
?>
<h2><?php echo $hello2; ?></h2>
<?php
// some more php
?>

View File

@ -0,0 +1,42 @@
<?php
namespace Rector\Tests\BetterPhpDocParser\PhpDocInlineHtml\Source;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use ViewScopeRector\Inferer\Rocket\FileLocator;
use ViewScopeRector\Inferer\Rocket\ViewFileLocator;
class InlineHtmlRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Defines a @var on inline html.', [new CodeSample('', '')]);
}
public function getNodeTypes(): array
{
return [Node\Stmt\InlineHTML::class];
}
/**
* @param Node\Stmt\InlineHTML $inlineHtml
*/
public function refactor(Node $inlineHtml): ?Node
{
if (strpos($inlineHtml->value, '<h1>') !== false) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($inlineHtml);
$phpDocInfo->addTagValueNode(new VarTagValueNode(new IdentifierTypeNode('string'), '$hello1', ''));
}
if (strpos($inlineHtml->value, '<h2>') !== false) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($inlineHtml);
$phpDocInfo->addTagValueNode(new VarTagValueNode(new IdentifierTypeNode('string'), '$hello2', ''));
}
return null;
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\BetterPhpDocParser\PhpDocInlineHtml;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class TestInlineHtmlTest extends AbstractRectorTestCase {
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Configuration\Option;
use ViewScopeRector\Inferer\Rocket\TestFileLocator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$parameters = $containerConfigurator->parameters();
$services->set(\Rector\Tests\BetterPhpDocParser\PhpDocInlineHtml\Source\InlineHtmlRector::class);
};

View File

@ -475,6 +475,10 @@ final class PhpDocInfo
$this->isSingleLine = false;
}
public function getNode(): \PhpParser\Node {
return $this->node;
}
private function getTypeOrMixed(?PhpDocTagValueNode $phpDocTagValueNode): Type
{
if ($phpDocTagValueNode === null) {

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Printer;
use Nette\Utils\Strings;
use PhpParser\Node\Stmt\InlineHTML;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
@ -93,6 +94,10 @@ final class PhpDocInfoPrinter
return $this->docBlockInliner->inline($docContent);
}
if ($phpDocInfo->getNode() instanceof InlineHTML) {
return '<?php'.PHP_EOL. $docContent .PHP_EOL.'?>';
}
return $docContent;
}
@ -114,6 +119,10 @@ final class PhpDocInfoPrinter
return '';
}
if ($phpDocInfo->getNode() instanceof InlineHTML) {
return '<?php'.PHP_EOL. $phpDocInfo->getPhpDocNode() .PHP_EOL.'?>';
}
return (string) $phpDocInfo->getPhpDocNode();
}