From 7e7304b93cd03fda558ac8ebfbc682ffa4938fe0 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 11 Nov 2023 04:52:41 +0700 Subject: [PATCH] [AutoImport] Handle DeclareStrictTypesRector on importNames() enabled on no namespace (#5241) * [AutoImport] Handle DeclareStrictTypesRector on importNames() enabled * update fixture * update fixture * update fixture * Fix * Fix --- .../AutoImportTest.php | 28 +++++++++++++ .../FixtureAutoImport/no_namespace.php.inc | 15 +++++++ .../FixtureAutoImport/with_namespace.php.inc | 16 ++++++++ .../config/auto_import_configured_rule.php | 11 +++++ .../DeclareStrictTypesRector.php | 40 +++++++++++++------ 5 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/AutoImportTest.php create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/no_namespace.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/with_namespace.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/config/auto_import_configured_rule.php diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/AutoImportTest.php b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/AutoImportTest.php new file mode 100644 index 00000000000..714b8295286 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/AutoImportTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/auto_import_configured_rule.php'; + } +} diff --git a/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/no_namespace.php.inc b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/no_namespace.php.inc new file mode 100644 index 00000000000..d7295fd0817 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector/FixtureAutoImport/no_namespace.php.inc @@ -0,0 +1,15 @@ + +----- + +----- +importNames(); + $rectorConfig->rule(DeclareStrictTypesRector::class); +}; diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index 1cf9d22e7aa..85a82874f71 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -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)); @@ -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]; } @@ -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; + } }