diff --git a/rules-tests/CodingStyle/Rector/Stmt/NewlineAfterStatementRector/Fixture/skip_html_php_mix.php.inc b/rules-tests/CodingStyle/Rector/Stmt/NewlineAfterStatementRector/Fixture/skip_html_php_mix.php.inc new file mode 100644 index 00000000000..4c1895b05c0 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Stmt/NewlineAfterStatementRector/Fixture/skip_html_php_mix.php.inc @@ -0,0 +1,17 @@ + +
+

Hi

+
+ + +
+

Bye

+
+ + + +End of file diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index 604427d2266..7716b2dc1ad 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -15,6 +15,7 @@ use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\Nop; +use Rector\Contract\Rector\HTMLAverseRectorInterface; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -22,7 +23,7 @@ /** * @see \Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\NewlineBeforeNewAssignSetRectorTest */ -final class NewlineBeforeNewAssignSetRector extends AbstractRector +final class NewlineBeforeNewAssignSetRector extends AbstractRector implements HTMLAverseRectorInterface { private ?string $previousStmtVariableName = null; diff --git a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php index d1999370bc6..b89d65724ef 100644 --- a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php +++ b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php @@ -24,6 +24,7 @@ use PhpParser\Node\Stmt\TryCatch; use PhpParser\Node\Stmt\While_; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Contract\Rector\HTMLAverseRectorInterface; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -32,7 +33,7 @@ /** * @see \Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\NewlineAfterStatementRectorTest */ -final class NewlineAfterStatementRector extends AbstractRector +final class NewlineAfterStatementRector extends AbstractRector implements HTMLAverseRectorInterface { /** * @var array> diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index f45489a1df0..d131efa6cbc 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -10,20 +10,21 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\DeclareDeclare; -use PhpParser\Node\Stmt\InlineHTML; use PhpParser\Node\Stmt\Nop; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Contract\Rector\HTMLAverseRectorInterface; use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\Rector\AbstractRector; use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder; +use Rector\ValueObject\Application\File; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** * @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\DeclareStrictTypesRectorTest */ -final class DeclareStrictTypesRector extends AbstractRector +final class DeclareStrictTypesRector extends AbstractRector implements HTMLAverseRectorInterface { public function __construct( private readonly DeclareStrictTypeFinder $declareStrictTypeFinder @@ -65,6 +66,10 @@ public function beforeTraverse(array $nodes): ?array return null; } + if ($this->startWithShebang($this->file)) { + return null; + } + if ($nodes === []) { return null; } @@ -79,10 +84,6 @@ public function beforeTraverse(array $nodes): ?array return null; } - if ($currentStmt instanceof InlineHTML) { - return null; - } - $nodes = $rootStmt->stmts; $stmt = $currentStmt; } @@ -124,4 +125,9 @@ public function refactor(Node $node): ?Node // workaroudn, as Rector now only hooks to specific nodes, not arrays return null; } + + private function startWithShebang(File $file): bool + { + return str_starts_with($file->getFileContent(), '#!'); + } } diff --git a/src/Contract/Rector/HTMLAverseRectorInterface.php b/src/Contract/Rector/HTMLAverseRectorInterface.php new file mode 100644 index 00000000000..a456e29647a --- /dev/null +++ b/src/Contract/Rector/HTMLAverseRectorInterface.php @@ -0,0 +1,13 @@ +file->containsHTML()) { + return null; + } + $filePath = $this->file->getFilePath(); if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) { return null; @@ -134,6 +139,7 @@ final public function enterNode(Node $node): int|Node|null // ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN $originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node; + $refactoredNode = $this->refactor($node); // @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing diff --git a/src/ValueObject/Application/File.php b/src/ValueObject/Application/File.php index 9658d1d13bb..fec056fdfcd 100644 --- a/src/ValueObject/Application/File.php +++ b/src/ValueObject/Application/File.php @@ -6,6 +6,8 @@ use PhpParser\Node; use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\InlineHTML; +use PhpParser\NodeFinder; use Rector\ChangesReporting\ValueObject\RectorWithLineChange; use Rector\Exception\ShouldNotHappenException; use Rector\ValueObject\Reporting\FileDiff; @@ -38,6 +40,11 @@ final class File */ private array $rectorWithLineChanges = []; + /** + * Cached result per file + */ + private ?bool $containsHtml = null; + public function __construct( private readonly string $filePath, private string $fileContent @@ -150,4 +157,16 @@ public function getRectorWithLineChanges(): array { return $this->rectorWithLineChanges; } + + public function containsHTML(): bool + { + if ($this->containsHtml !== null) { + return $this->containsHtml; + } + + $nodeFinder = new NodeFinder(); + + $this->containsHtml = (bool) $nodeFinder->findFirstInstanceOf($this->oldStmts, InlineHTML::class); + return $this->containsHtml; + } }