diff --git a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php index d26e7b3b438..472ad758a88 100644 --- a/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php +++ b/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php @@ -52,28 +52,28 @@ CODE_SAMPLE if ($newStmts === []) { return null; } - $stmt = \current($newStmts); - if ($stmt instanceof FileWithoutNamespace) { - $currentStmt = \current($stmt->stmts); + $rootStmt = \current($newStmts); + $stmt = $rootStmt; + 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)); $strictTypesDeclare = new Declare_([$declareDeclare]); $rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine()); $this->file->addRectorClassWithLine($rectorWithLineChange); + if ($rootStmt instanceof FileWithoutNamespace) { + /** @var Stmt[] $nodes */ + $rootStmt->stmts = \array_merge([$strictTypesDeclare, new Nop()], $nodes); + return [$rootStmt]; + } return \array_merge([$strictTypesDeclare, new Nop()], $nodes); } /** @@ -91,4 +91,17 @@ CODE_SAMPLE // 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; + } } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 137fe2c4420..3faebe360b2 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 = '2f74b02ef6c754c68abcdd246837583967e24678'; + public const PACKAGE_VERSION = '7e7304b93cd03fda558ac8ebfbc682ffa4938fe0'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-11-11 02:09:43'; + public const RELEASE_DATE = '2023-11-11 04:52:41'; /** * @var int */