Skip to content

Commit

Permalink
[Php56] Skip AddDefaultValueForUndefinedVariableRector on has InlineH…
Browse files Browse the repository at this point in the history
…TML (#757)

* AddDefaultValueForUndefinedVariableRector adds variable to embedded HTML

* Closes #754

* fixture

* move to InlineHTMLAnalyzer

* cs fix

* fix

Co-authored-by: Brandon Olivares <programmer2188@gmail.com>
  • Loading branch information
samsonasik and devbanana committed Aug 25, 2021
1 parent 991ea06 commit 4520aaa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
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;
Expand All @@ -24,7 +24,8 @@ final class NormalizeNamespaceByPSR4ComposerAutoloadRector extends AbstractRecto
{
public function __construct(
private PSR4AutoloadNamespaceMatcherInterface $psr4AutoloadNamespaceMatcher,
private FullyQualifyStmtsAnalyzer $fullyQualifyStmtsAnalyzer
private FullyQualifyStmtsAnalyzer $fullyQualifyStmtsAnalyzer,
private InlineHTMLAnalyzer $inlineHTMLAnalyzer
) {
}

Expand Down Expand Up @@ -81,7 +82,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
return null;
}

Expand All @@ -106,11 +107,6 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkip(Node $node): bool
{
return (bool) $this->betterNodeFinder->findFirstInstanceOf($node, InlineHTML::class);
}

private function refactorFileWithoutNamespace(
FileWithoutNamespace $fileWithoutNamespace,
string $expectedNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -84,6 +86,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
return null;
}

$undefinedVariableNames = $this->undefinedVariableResolver->resolve($node);

// avoids adding same variable multiple tiemes
Expand Down
27 changes: 27 additions & 0 deletions src/NodeAnalyzer/InlineHTMLAnalyzer.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4520aaa

Please sign in to comment.