[Php56] Skip AddDefaultValueForUndefinedVariableRector on has InlineHTML (#757)

* AddDefaultValueForUndefinedVariableRector adds variable to embedded HTML

* Closes #754

* fixture

* move to InlineHTMLAnalyzer

* cs fix

* fix

Co-authored-by: Brandon Olivares <programmer2188@gmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-25 15:17:21 +07:00 committed by GitHub
parent 991ea06c15
commit 4520aaaefc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 9 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;
final class SkipWithEmbeddedHTML
{
public function run(): void
{
?>
<p>Let's insert stuff here.</p>
<?php
if (random_int(1, 10) === 5) {
$a = true;
}
if (isset($a)) {
doStuff();
}
}
}
?>

View File

@ -7,8 +7,8 @@ namespace Rector\PSR4\Rector\FileWithoutNamespace;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Rector\AbstractRector;
use Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface;
@ -24,7 +24,8 @@ final class NormalizeNamespaceByPSR4ComposerAutoloadRector extends AbstractRecto
{
public function __construct(
private PSR4AutoloadNamespaceMatcherInterface $psr4AutoloadNamespaceMatcher,
private FullyQualifyStmtsAnalyzer $fullyQualifyStmtsAnalyzer
private FullyQualifyStmtsAnalyzer $fullyQualifyStmtsAnalyzer,
private InlineHTMLAnalyzer $inlineHTMLAnalyzer
) {
}
@ -81,7 +82,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
return null;
}
@ -106,11 +107,6 @@ CODE_SAMPLE
return $node;
}
private function shouldSkip(Node $node): bool
{
return (bool) $this->betterNodeFinder->findFirstInstanceOf($node, InlineHTML::class);
}
private function refactorFileWithoutNamespace(
FileWithoutNamespace $fileWithoutNamespace,
string $expectedNamespace

View File

@ -14,6 +14,7 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\NodeAnalyzer\InlineHTMLAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Php56\NodeAnalyzer\UndefinedVariableResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -33,7 +34,8 @@ final class AddDefaultValueForUndefinedVariableRector extends AbstractRector
private const ALREADY_ADDED_VARIABLE_NAMES = 'already_added_variable_names';
public function __construct(
private UndefinedVariableResolver $undefinedVariableResolver
private UndefinedVariableResolver $undefinedVariableResolver,
private InlineHTMLAnalyzer $inlineHTMLAnalyzer
) {
}
@ -84,6 +86,10 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
return null;
}
$undefinedVariableNames = $this->undefinedVariableResolver->resolve($node);
// avoids adding same variable multiple tiemes

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Rector\Core\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\InlineHTML;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
final class InlineHTMLAnalyzer
{
public function __construct(
private BetterNodeFinder $betterNodeFinder
) {
}
public function hasInlineHTML(Node $node): bool
{
if ($node instanceof FunctionLike) {
return (bool) $this->betterNodeFinder->findInstanceOf((array) $node->getStmts(), InlineHTML::class);
}
return (bool) $this->betterNodeFinder->findInstanceOf($node, InlineHTML::class);
}
}