Updated Rector to commit 7e7304b93cd03fda558ac8ebfbc682ffa4938fe0

7e7304b93c [AutoImport] Handle DeclareStrictTypesRector on importNames() enabled on no namespace (#5241)
This commit is contained in:
Tomas Votruba 2023-11-10 21:55:35 +00:00
parent e41e10db62
commit e26480f1cb
2 changed files with 27 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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
*/