Skip to content

Commit

Permalink
[AutoImport] Handle DeclareStrictTypesRector on importNames() enabled…
Browse files Browse the repository at this point in the history
… on no namespace (#5241)

* [AutoImport] Handle DeclareStrictTypesRector on importNames() enabled

* update fixture

* update fixture

* update fixture

* Fix

* Fix
  • Loading branch information
samsonasik committed Nov 10, 2023
1 parent 2f74b02 commit 7e7304b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AutoImportTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/auto_import_configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

// no namespace on purpose to reproduce issue
\Foo\Bar::BAZ;

?>
-----
<?php

declare(strict_types=1);

use Foo\Bar;

// no namespace on purpose to reproduce issue
Bar::BAZ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\FixtureAutoImport;

\Foo\Bar::BAZ;

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\FixtureAutoImport;

use Foo\Bar;
Bar::BAZ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->rule(DeclareStrictTypesRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,22 @@ public function beforeTraverse(array $nodes): ?array
return null;
}

$stmt = current($newStmts);
$rootStmt = current($newStmts);
$stmt = $rootStmt;

if ($stmt instanceof FileWithoutNamespace) {
$currentStmt = current($stmt->stmts);
if ($rootStmt instanceof FileWithoutNamespace) {
$currentStmt = current($rootStmt->stmts);

if (! $currentStmt instanceof Stmt) {
return null;
}

$nodes = $stmt->stmts;
$nodes = $rootStmt->stmts;
$stmt = $currentStmt;
}

// when first stmt is Declare_, verify if there is strict_types definition already,
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
if ($stmt instanceof Declare_) {
foreach ($stmt->declares as $declare) {
if ($declare->key->toString() === 'strict_types') {
return null;
}
}
if ($this->shouldSkip($stmt)) {
return null;
}

$declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1));
Expand All @@ -93,6 +88,12 @@ public function beforeTraverse(array $nodes): ?array
$rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);

if ($rootStmt instanceof FileWithoutNamespace) {
/** @var Stmt[] $nodes */
$rootStmt->stmts = [$strictTypesDeclare, new Nop(), ...$nodes];
return [$rootStmt];
}

return [$strictTypesDeclare, new Nop(), ...$nodes];
}

Expand All @@ -112,4 +113,19 @@ public function refactor(Node $node): ?Node
// workaroudn, as Rector now only hooks to specific nodes, not arrays
return null;
}

private function shouldSkip(Stmt $stmt): bool
{
// when first stmt is Declare_, verify if there is strict_types definition already,
// as multiple declare is allowed, with declare(strict_types=1) only allowed on very first stmt
if ($stmt instanceof Declare_) {
foreach ($stmt->declares as $declare) {
if ($declare->key->toString() === 'strict_types') {
return true;
}
}
}

return false;
}
}

0 comments on commit 7e7304b

Please sign in to comment.