Skip to content

Commit

Permalink
Updated Rector to commit e562253268ac3c2b37c3b267d1208148a0a313b5
Browse files Browse the repository at this point in the history
rectorphp/rector-src@e562253 [dx] Add HTMLAverseRectorInterface to teach rules to skip HTML + PHP mix that would go wrong (#6445)
  • Loading branch information
TomasVotruba committed Nov 16, 2024
1 parent d04183c commit c8d4dd9
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
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;
/**
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\NewlineBeforeNewAssignSetRectorTest
*/
final class NewlineBeforeNewAssignSetRector extends AbstractRector
final class NewlineBeforeNewAssignSetRector extends AbstractRector implements HTMLAverseRectorInterface
{
/**
* @var string|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
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;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\NewlineAfterStatementRectorTest
*/
final class NewlineAfterStatementRector extends AbstractRector
final class NewlineAfterStatementRector extends AbstractRector implements HTMLAverseRectorInterface
{
/**
* @var array<class-string<Node>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
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
{
/**
* @readonly
Expand Down Expand Up @@ -59,6 +60,9 @@ public function beforeTraverse(array $nodes) : ?array
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $filePath)) {
return null;
}
if ($this->startWithShebang($this->file)) {
return null;
}
if ($nodes === []) {
return null;
}
Expand All @@ -69,9 +73,6 @@ public function beforeTraverse(array $nodes) : ?array
if (!$currentStmt instanceof Stmt) {
return null;
}
if ($currentStmt instanceof InlineHTML) {
return null;
}
$nodes = $rootStmt->stmts;
$stmt = $currentStmt;
}
Expand Down Expand Up @@ -106,4 +107,8 @@ 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 \strncmp($file->getFileContent(), '#!', \strlen('#!')) === 0;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c7dfbe7c462c103f78b1fa33e89a32dfa6e26299';
public const PACKAGE_VERSION = 'e562253268ac3c2b37c3b267d1208148a0a313b5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-11-16 22:24:43';
public const RELEASE_DATE = '2024-11-16 16:53:39';
/**
* @var int
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Contract/Rector/HTMLAverseRectorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare (strict_types=1);
namespace Rector\Contract\Rector;

/**
* Rector rule with this marker interface will skip all files
* with any HTML node. This is practical to avoid malformed PHP+HTML files
*/
interface HTMLAverseRectorInterface
{
}
4 changes: 4 additions & 0 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeDecorator\CreatedByRuleDecorator;
Expand Down Expand Up @@ -130,6 +131,9 @@ public final function enterNode(Node $node)
if (!$this->isMatchingNodeType($node)) {
return null;
}
if (\is_a($this, HTMLAverseRectorInterface::class, \true) && $this->file->containsHTML()) {
return null;
}
$filePath = $this->file->getFilePath();
if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) {
return null;
Expand Down
16 changes: 16 additions & 0 deletions src/ValueObject/Application/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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;
Expand Down Expand Up @@ -48,6 +50,11 @@ final class File
* @var RectorWithLineChange[]
*/
private $rectorWithLineChanges = [];
/**
* Cached result per file
* @var bool|null
*/
private $containsHtml;
public function __construct(string $filePath, string $fileContent)
{
$this->filePath = $filePath;
Expand Down Expand Up @@ -143,4 +150,13 @@ 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;
}
}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@
'Rector\\Contract\\DependencyInjection\\ResetableInterface' => $baseDir . '/src/Contract/DependencyInjection/ResetableInterface.php',
'Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface' => $baseDir . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php',
'Rector\\Contract\\Rector\\ConfigurableRectorInterface' => $baseDir . '/src/Contract/Rector/ConfigurableRectorInterface.php',
'Rector\\Contract\\Rector\\HTMLAverseRectorInterface' => $baseDir . '/src/Contract/Rector/HTMLAverseRectorInterface.php',
'Rector\\Contract\\Rector\\RectorInterface' => $baseDir . '/src/Contract/Rector/RectorInterface.php',
'Rector\\Contract\\Rector\\ScopeAwareRectorInterface' => $baseDir . '/src/Contract/Rector/ScopeAwareRectorInterface.php',
'Rector\\CustomRules\\SimpleNodeDumper' => $baseDir . '/src/CustomRules/SimpleNodeDumper.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,7 @@ class ComposerStaticInit58d677395c7d7c6a1b0f093c876515de
'Rector\\Contract\\DependencyInjection\\ResetableInterface' => __DIR__ . '/../..' . '/src/Contract/DependencyInjection/ResetableInterface.php',
'Rector\\Contract\\PhpParser\\Node\\StmtsAwareInterface' => __DIR__ . '/../..' . '/src/Contract/PhpParser/Node/StmtsAwareInterface.php',
'Rector\\Contract\\Rector\\ConfigurableRectorInterface' => __DIR__ . '/../..' . '/src/Contract/Rector/ConfigurableRectorInterface.php',
'Rector\\Contract\\Rector\\HTMLAverseRectorInterface' => __DIR__ . '/../..' . '/src/Contract/Rector/HTMLAverseRectorInterface.php',
'Rector\\Contract\\Rector\\RectorInterface' => __DIR__ . '/../..' . '/src/Contract/Rector/RectorInterface.php',
'Rector\\Contract\\Rector\\ScopeAwareRectorInterface' => __DIR__ . '/../..' . '/src/Contract/Rector/ScopeAwareRectorInterface.php',
'Rector\\CustomRules\\SimpleNodeDumper' => __DIR__ . '/../..' . '/src/CustomRules/SimpleNodeDumper.php',
Expand Down

0 comments on commit c8d4dd9

Please sign in to comment.