2023-04-17 15:47:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\TypeDeclaration\Rector\StmtsAwareInterface;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
2023-04-22 08:33:24 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2023-04-17 15:47:09 +00:00
|
|
|
use PhpParser\Node\Stmt\Declare_;
|
|
|
|
use PhpParser\Node\Stmt\DeclareDeclare;
|
|
|
|
use PhpParser\Node\Stmt\Nop;
|
2023-04-18 11:12:39 +00:00
|
|
|
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2023-04-17 15:47:09 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector\DeclareStrictTypesRectorTest
|
|
|
|
*/
|
|
|
|
final class DeclareStrictTypesRector extends AbstractRector
|
|
|
|
{
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Add declare(strict_types=1) if missing', [new CodeSample(<<<'CODE_SAMPLE'
|
|
|
|
function someFunction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
function someFunction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
* @return Node[]|null
|
|
|
|
*/
|
|
|
|
public function beforeTraverse(array $nodes) : ?array
|
|
|
|
{
|
|
|
|
parent::beforeTraverse($nodes);
|
2023-10-23 07:51:12 +00:00
|
|
|
$filePath = $this->file->getFilePath();
|
|
|
|
if ($this->skipper->shouldSkipElementAndFilePath(self::class, $filePath)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-04-18 11:12:39 +00:00
|
|
|
$newStmts = $this->file->getNewStmts();
|
|
|
|
if ($newStmts === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-03-08 06:34:02 +00:00
|
|
|
$rootStmt = \current($newStmts);
|
2023-11-10 21:55:35 +00:00
|
|
|
$stmt = $rootStmt;
|
|
|
|
if ($rootStmt instanceof FileWithoutNamespace) {
|
2024-03-08 06:34:02 +00:00
|
|
|
$currentStmt = \current($rootStmt->stmts);
|
2023-07-03 17:30:02 +00:00
|
|
|
if (!$currentStmt instanceof Stmt) {
|
2023-04-22 08:33:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-11-10 21:55:35 +00:00
|
|
|
$nodes = $rootStmt->stmts;
|
2023-07-03 17:30:02 +00:00
|
|
|
$stmt = $currentStmt;
|
2023-04-22 08:33:24 +00:00
|
|
|
}
|
2024-03-07 21:09:46 +00:00
|
|
|
if (!$stmt instanceof Stmt) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-10 21:55:35 +00:00
|
|
|
if ($this->shouldSkip($stmt)) {
|
|
|
|
return null;
|
2023-04-17 15:47:09 +00:00
|
|
|
}
|
|
|
|
$declareDeclare = new DeclareDeclare(new Identifier('strict_types'), new LNumber(1));
|
|
|
|
$strictTypesDeclare = new Declare_([$declareDeclare]);
|
2023-04-18 11:12:39 +00:00
|
|
|
$rectorWithLineChange = new RectorWithLineChange(self::class, $stmt->getLine());
|
|
|
|
$this->file->addRectorClassWithLine($rectorWithLineChange);
|
2023-11-10 21:55:35 +00:00
|
|
|
if ($rootStmt instanceof FileWithoutNamespace) {
|
|
|
|
/** @var Stmt[] $nodes */
|
|
|
|
$rootStmt->stmts = \array_merge([$strictTypesDeclare, new Nop()], $nodes);
|
|
|
|
return [$rootStmt];
|
|
|
|
}
|
2023-04-17 15:47:09 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
// workaroudn, as Rector now only hooks to specific nodes, not arrays
|
|
|
|
return null;
|
|
|
|
}
|
2023-11-10 21:55:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2023-04-17 15:47:09 +00:00
|
|
|
}
|