Skip to content

Commit

Permalink
Updated Rector to commit 7e7304b93cd03fda558ac8ebfbc682ffa4938fe0
Browse files Browse the repository at this point in the history
rectorphp/rector-src@7e7304b [AutoImport] Handle DeclareStrictTypesRector on importNames() enabled on no namespace (#5241)
  • Loading branch information
TomasVotruba committed Nov 10, 2023
1 parent e41e10d commit e26480f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,28 @@ public function beforeTraverse(array $nodes) : ?array
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);
}
/**
Expand All @@ -91,4 +91,17 @@ 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;
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e26480f

Please sign in to comment.