Skip to content

Commit

Permalink
[AutoImport] Skip start with <?= short open tag on auto import (#5496)
Browse files Browse the repository at this point in the history
* [AutoImport] Skip start with <?= short open tag on auto import

* [AutoImport] Skip start with <?= short open tag on auto import

* fix phpstan
  • Loading branch information
samsonasik committed Jan 24, 2024
1 parent c2cdcd3 commit 7584d25
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public function enterNode(Node $node): ?Node
return null;
}

$firstStmt = current($file->getNewStmts());
if ($firstStmt instanceof FileWithoutNamespace && current($firstStmt->stmts) instanceof InlineHTML) {
if ($this->shouldSkipFileWithoutNamespace($file)) {
return null;
}

Expand Down Expand Up @@ -85,6 +84,25 @@ public function enterNode(Node $node): ?Node
return $node;
}

private function shouldSkipFileWithoutNamespace(File $file): bool
{
$firstStmt = current($file->getNewStmts());
if (! $firstStmt instanceof FileWithoutNamespace) {
return false;
}

$currentStmt = current($firstStmt->stmts);

if ($currentStmt instanceof InlineHTML || $currentStmt === false) {
return true;
}

$oldTokens = $file->getOldTokens();
$tokenStartPos = $currentStmt->getStartTokenPos();

return isset($oldTokens[$tokenStartPos][1]) && $oldTokens[$tokenStartPos][1] === '<?=';
}

private function processNodeName(FullyQualified $fullyQualified, File $file): ?Node
{
if ($fullyQualified->isSpecialClassName()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= ns\Class::method() ?>

0 comments on commit 7584d25

Please sign in to comment.