From c8d4dd98ae5a52cd39bf236a74e230d304a3df4d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 16 Nov 2024 16:56:04 +0000 Subject: [PATCH] Updated Rector to commit e562253268ac3c2b37c3b267d1208148a0a313b5 https://github.com/rectorphp/rector-src/commit/e562253268ac3c2b37c3b267d1208148a0a313b5 [dx] Add HTMLAverseRectorInterface to teach rules to skip HTML + PHP mix that would go wrong (#6445) --- .../NewlineBeforeNewAssignSetRector.php | 3 ++- .../Rector/Stmt/NewlineAfterStatementRector.php | 3 ++- .../DeclareStrictTypesRector.php | 15 ++++++++++----- src/Application/VersionResolver.php | 4 ++-- .../Rector/HTMLAverseRectorInterface.php | 12 ++++++++++++ src/Rector/AbstractRector.php | 4 ++++ src/ValueObject/Application/File.php | 16 ++++++++++++++++ vendor/composer/autoload_classmap.php | 1 + vendor/composer/autoload_static.php | 1 + 9 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 src/Contract/Rector/HTMLAverseRectorInterface.php diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index cb089aaa898e..f6b76d23c9cd 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -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 diff --git a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php index a28b9a4ea915..6f1d11553ef0 100644 --- a/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php +++ b/rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php @@ -23,6 +23,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; @@ -30,7 +31,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 4665cd37d157..22ae8441460b 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -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 @@ -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; } @@ -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; } @@ -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; + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 011375f9da7f..a4cf43e6886a 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -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 */ diff --git a/src/Contract/Rector/HTMLAverseRectorInterface.php b/src/Contract/Rector/HTMLAverseRectorInterface.php new file mode 100644 index 000000000000..794687206355 --- /dev/null +++ b/src/Contract/Rector/HTMLAverseRectorInterface.php @@ -0,0 +1,12 @@ +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; diff --git a/src/ValueObject/Application/File.php b/src/ValueObject/Application/File.php index bad7b1e65573..594bf233ff87 100644 --- a/src/ValueObject/Application/File.php +++ b/src/ValueObject/Application/File.php @@ -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; @@ -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; @@ -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; + } } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 1741179a136d..9b98eb2aa224 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -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', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 6896099a9788..6d7f892da892 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -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',