Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoImport] Handle DeclareStrictTypesRector on importNames() enabled on no namespace #5241

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}