mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 57275efac450b9b0466e9dc934d3c07e2e9a1e43
57275efac4
[TypeDeclarations] Add IncreaseDeclareStrictTypesRector to raise level of declare() coverage (#5849)
This commit is contained in:
parent
c747b603a3
commit
78e3d54584
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
final class DeclareStrictTypeFinder
|
||||
{
|
||||
public function hasDeclareStrictTypes(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_) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($stmt->declares as $declare) {
|
||||
if ($declare->key->toString() === 'strict_types') {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,7 @@ use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
|
|||
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
|
@ -21,6 +22,15 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class DeclareStrictTypesRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder
|
||||
*/
|
||||
private $declareStrictTypeFinder;
|
||||
public function __construct(DeclareStrictTypeFinder $declareStrictTypeFinder)
|
||||
{
|
||||
$this->declareStrictTypeFinder = $declareStrictTypeFinder;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add declare(strict_types=1) if missing', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
|
@ -62,10 +72,9 @@ CODE_SAMPLE
|
|||
$nodes = $rootStmt->stmts;
|
||||
$stmt = $currentStmt;
|
||||
}
|
||||
if (!$stmt instanceof Stmt) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkip($stmt)) {
|
||||
// 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 ($this->declareStrictTypeFinder->hasDeclareStrictTypes($stmt)) {
|
||||
return null;
|
||||
}
|
||||
$declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1));
|
||||
|
@ -94,17 +103,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
use PhpParser\Node\Stmt\DeclareDeclare;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
|
||||
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
||||
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202404\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\IncreaseDeclareStrictTypesRector\IncreaseDeclareStrictTypesRectorTest
|
||||
*/
|
||||
final class IncreaseDeclareStrictTypesRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\DeclareStrictTypeFinder
|
||||
*/
|
||||
private $declareStrictTypeFinder;
|
||||
private const LIMIT = 'limit';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $limit = 10;
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $changedItemCount = 0;
|
||||
public function __construct(DeclareStrictTypeFinder $declareStrictTypeFinder)
|
||||
{
|
||||
$this->declareStrictTypeFinder = $declareStrictTypeFinder;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add declare strict types to a limited amount of classes at a time, to try out in the wild and increase level gradually', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
function someFunction()
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
declare(strict_types=1);
|
||||
|
||||
function someFunction()
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, [self::LIMIT => 10])]);
|
||||
}
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return Node[]|null
|
||||
*/
|
||||
public function beforeTraverse(array $nodes) : ?array
|
||||
{
|
||||
parent::beforeTraverse($nodes);
|
||||
$newStmts = $this->file->getNewStmts();
|
||||
if ($newStmts === []) {
|
||||
return null;
|
||||
}
|
||||
$rootStmt = \current($newStmts);
|
||||
$stmt = $rootStmt;
|
||||
// skip classes without namespace for safety reasons
|
||||
if ($rootStmt instanceof FileWithoutNamespace) {
|
||||
return null;
|
||||
}
|
||||
if ($this->declareStrictTypeFinder->hasDeclareStrictTypes($stmt)) {
|
||||
return null;
|
||||
}
|
||||
// keep change withing a limit
|
||||
if ($this->changedItemCount >= $this->limit) {
|
||||
return null;
|
||||
}
|
||||
++$this->changedItemCount;
|
||||
$strictTypesDeclare = $this->creteStrictTypesDeclare();
|
||||
$rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine());
|
||||
$this->file->addRectorClassWithLine($rectorWithLineChange);
|
||||
return \array_merge([$strictTypesDeclare, new Nop()], $nodes);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [StmtsAwareInterface::class];
|
||||
}
|
||||
/**
|
||||
* @param StmtsAwareInterface $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// workaround, as Rector now only hooks to specific nodes, not arrays
|
||||
return null;
|
||||
}
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
Assert::keyExists($configuration, self::LIMIT);
|
||||
$this->limit = (int) $configuration[self::LIMIT];
|
||||
}
|
||||
private function creteStrictTypesDeclare() : Declare_
|
||||
{
|
||||
$declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1));
|
||||
return new Declare_([$declareDeclare]);
|
||||
}
|
||||
}
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'ce1033d8e6434f3111307dd274dec753eb6481b1';
|
||||
public const PACKAGE_VERSION = '57275efac450b9b0466e9dc934d3c07e2e9a1e43';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-04-30 19:39:49';
|
||||
public const RELEASE_DATE = '2024-04-30 13:22:44';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
|
@ -2362,6 +2362,7 @@ return array(
|
|||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\DeclareStrictTypeFinder' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/DeclareStrictTypeFinder.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\NeverFuncCallAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParamAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php',
|
||||
|
@ -2422,6 +2423,7 @@ return array(
|
|||
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictSetUpRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\StmtsAwareInterface\\DeclareStrictTypesRector' => $baseDir . '/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\StmtsAwareInterface\\IncreaseDeclareStrictTypesRector' => $baseDir . '/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\While_\\WhileNullableToInstanceofRector' => $baseDir . '/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php',
|
||||
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictBoolExprAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictBoolExprAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictScalarExprAnalyzer' => $baseDir . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php',
|
||||
|
|
|
@ -2581,6 +2581,7 @@ class ComposerStaticInit70e3025dac7e7555f69a9b4ca9e3dfde
|
|||
'Rector\\TypeDeclaration\\NodeAnalyzer\\CallerParamMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/CallerParamMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\DeclareStrictTypeFinder' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/DeclareStrictTypeFinder.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\NeverFuncCallAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/NeverFuncCallAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ParamAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ParamAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnAnalyzer.php',
|
||||
|
@ -2641,6 +2642,7 @@ class ComposerStaticInit70e3025dac7e7555f69a9b4ca9e3dfde
|
|||
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictSetUpRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\StmtsAwareInterface\\DeclareStrictTypesRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/StmtsAwareInterface/DeclareStrictTypesRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\StmtsAwareInterface\\IncreaseDeclareStrictTypesRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\While_\\WhileNullableToInstanceofRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/While_/WhileNullableToInstanceofRector.php',
|
||||
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictBoolExprAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictBoolExprAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\TypeAnalyzer\\AlwaysStrictScalarExprAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeAnalyzer/AlwaysStrictScalarExprAnalyzer.php',
|
||||
|
|
|
@ -504,8 +504,8 @@
|
|||
},
|
||||
{
|
||||
"name": "illuminate\/container",
|
||||
"version": "v10.48.9",
|
||||
"version_normalized": "10.48.9.0",
|
||||
"version": "v10.48.10",
|
||||
"version_normalized": "10.48.10.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/illuminate\/container.git",
|
||||
|
@ -561,8 +561,8 @@
|
|||
},
|
||||
{
|
||||
"name": "illuminate\/contracts",
|
||||
"version": "v10.48.9",
|
||||
"version_normalized": "10.48.9.0",
|
||||
"version": "v10.48.10",
|
||||
"version_normalized": "10.48.10.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/illuminate\/contracts.git",
|
||||
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue