mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
Updated Rector to commit 237f255023
237f255023
[TypeDeclaration] Add ReturnTypeFromStrictNativeFuncCallRector (#2570)
This commit is contained in:
parent
13fb2e3eb1
commit
65284cc98a
|
@ -8,7 +8,8 @@ use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeR
|
|||
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
|
||||
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector;
|
||||
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
|
||||
|
@ -16,5 +17,6 @@ use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
|
|||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
|
||||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([AddClosureReturnTypeRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, ReturnTypeFromStrictTypedCallRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictReturnExprRector::class]);
|
||||
$rectorConfig->rules([AddClosureReturnTypeRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, ReturnTypeFromStrictTypedCallRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class]);
|
||||
$rectorConfig->rule(ReturnTypeFromStrictNativeFuncCallRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 519 Rules Overview
|
||||
# 520 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -94,7 +94,7 @@
|
|||
|
||||
- [Transform](#transform) (36)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (27)
|
||||
- [TypeDeclaration](#typedeclaration) (28)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
|
@ -11894,11 +11894,11 @@ Add return type to function like with return new
|
|||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictReturnExprRector
|
||||
### ReturnTypeFromStrictBoolReturnExprRector
|
||||
|
||||
Add strict return type based on returned strict expr type
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.php)
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
|
@ -11913,6 +11913,25 @@ Add strict return type based on returned strict expr type
|
|||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictNativeFuncCallRector
|
||||
|
||||
Add strict return type based native function return
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- public function run()
|
||||
+ public function run(): int
|
||||
{
|
||||
return strlen('value');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### ReturnTypeFromStrictTypedCallRector
|
||||
|
||||
Add return type from strict return type of call
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
final class ExclusiveNativeFuncCallReturnMatcher
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
/**
|
||||
* @param Return_[] $returns
|
||||
* @return FuncCall[]|null
|
||||
*/
|
||||
public function match(array $returns)
|
||||
{
|
||||
$funcCalls = [];
|
||||
foreach ($returns as $return) {
|
||||
// we need exact expr return
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if (!$return->expr instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($return->expr);
|
||||
if (!$functionReflection instanceof FunctionReflection) {
|
||||
return null;
|
||||
}
|
||||
if (!$functionReflection->isBuiltin()) {
|
||||
return null;
|
||||
}
|
||||
$funcCalls[] = $return->expr;
|
||||
}
|
||||
return $funcCalls;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\Yield_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer;
|
||||
final class StrictBoolReturnTypeAnalyzer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer
|
||||
*/
|
||||
private $alwaysStrictBoolExprAnalyzer;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, AlwaysStrictBoolExprAnalyzer $alwaysStrictBoolExprAnalyzer)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->alwaysStrictBoolExprAnalyzer = $alwaysStrictBoolExprAnalyzer;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
||||
*/
|
||||
public function hasAlwaysStrictBoolReturn($functionLike) : bool
|
||||
{
|
||||
if ($functionLike->stmts === null) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class])) {
|
||||
return \false;
|
||||
}
|
||||
/** @var Return_[] $returns */
|
||||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
|
||||
if ($returns === []) {
|
||||
return \false;
|
||||
}
|
||||
// is one statement depth 3?
|
||||
if (!$this->areExclusiveExprReturns($returns)) {
|
||||
return \false;
|
||||
}
|
||||
// has root return?
|
||||
if (!$this->hasClassMethodRootReturn($functionLike)) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($returns as $return) {
|
||||
// we need exact expr return
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if (!$this->alwaysStrictBoolExprAnalyzer->isStrictBoolExpr($return->expr)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* @param Return_[] $returns
|
||||
*/
|
||||
private function areExclusiveExprReturns(array $returns) : bool
|
||||
{
|
||||
foreach ($returns as $return) {
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
*/
|
||||
private function hasClassMethodRootReturn($functionLike) : bool
|
||||
{
|
||||
foreach ((array) $functionLike->stmts as $stmt) {
|
||||
if ($stmt instanceof Return_) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\Yield_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter\ExclusiveNativeFuncCallReturnMatcher;
|
||||
final class StrictNativeFunctionReturnTypeAnalyzer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnFilter\ExclusiveNativeFuncCallReturnMatcher
|
||||
*/
|
||||
private $exclusiveNativeFuncCallReturnMatcher;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, ExclusiveNativeFuncCallReturnMatcher $exclusiveNativeFuncCallReturnMatcher)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->exclusiveNativeFuncCallReturnMatcher = $exclusiveNativeFuncCallReturnMatcher;
|
||||
}
|
||||
/**
|
||||
* @return FuncCall[]|null
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
||||
*/
|
||||
public function matchAlwaysReturnNativeFuncCalls($functionLike) : ?array
|
||||
{
|
||||
if ($functionLike->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($functionLike, [Yield_::class])) {
|
||||
return null;
|
||||
}
|
||||
/** @var Return_[] $returns */
|
||||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($functionLike, Return_::class);
|
||||
if ($returns === []) {
|
||||
return null;
|
||||
}
|
||||
// is one statement depth 3?
|
||||
if (!$this->areExclusiveExprReturns($returns)) {
|
||||
return null;
|
||||
}
|
||||
// has root return?
|
||||
if (!$this->hasClassMethodRootReturn($functionLike)) {
|
||||
return null;
|
||||
}
|
||||
$nativeFuncCalls = $this->exclusiveNativeFuncCallReturnMatcher->match($returns);
|
||||
if ($nativeFuncCalls === null) {
|
||||
return null;
|
||||
}
|
||||
return $nativeFuncCalls;
|
||||
}
|
||||
/**
|
||||
* @param Return_[] $returns
|
||||
*/
|
||||
private function areExclusiveExprReturns(array $returns) : bool
|
||||
{
|
||||
foreach ($returns as $return) {
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
*/
|
||||
private function hasClassMethodRootReturn($functionLike) : bool
|
||||
{
|
||||
foreach ((array) $functionLike->stmts as $stmt) {
|
||||
if ($stmt instanceof Return_) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictBoolReturnTypeAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector\ReturnTypeFromStrictBoolReturnExprRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromStrictBoolReturnExprRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictBoolReturnTypeAnalyzer
|
||||
*/
|
||||
private $strictBoolReturnTypeAnalyzer;
|
||||
public function __construct(StrictBoolReturnTypeAnalyzer $strictBoolReturnTypeAnalyzer)
|
||||
{
|
||||
$this->strictBoolReturnTypeAnalyzer = $strictBoolReturnTypeAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add strict return type based on returned strict expr type', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && $this->somethingElse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): bool
|
||||
{
|
||||
return $this->first() && $this->somethingElse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, Function_::class, Closure::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|Function_|Closure $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->strictBoolReturnTypeAnalyzer->hasAlwaysStrictBoolReturn($node)) {
|
||||
return null;
|
||||
}
|
||||
$node->returnType = new Identifier('bool');
|
||||
return $node;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersion::PHP_70;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PHPStan\Type\MixedType;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictNativeFunctionReturnTypeAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeFuncCallRector\ReturnTypeFromStrictNativeFuncCallRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromStrictNativeFuncCallRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictNativeFunctionReturnTypeAnalyzer
|
||||
*/
|
||||
private $strictNativeFunctionReturnTypeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
||||
*/
|
||||
private $typeFactory;
|
||||
public function __construct(StrictNativeFunctionReturnTypeAnalyzer $strictNativeFunctionReturnTypeAnalyzer, TypeFactory $typeFactory)
|
||||
{
|
||||
$this->strictNativeFunctionReturnTypeAnalyzer = $strictNativeFunctionReturnTypeAnalyzer;
|
||||
$this->typeFactory = $typeFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add strict return type based native function return', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return strlen('value');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): int
|
||||
{
|
||||
return strlen('value');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, Closure::class, Function_::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|Closure|Function_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
$nativeFuncCalls = $this->strictNativeFunctionReturnTypeAnalyzer->matchAlwaysReturnNativeFuncCalls($node);
|
||||
if ($nativeFuncCalls === null) {
|
||||
return null;
|
||||
}
|
||||
$funcCallTypes = [];
|
||||
foreach ($nativeFuncCalls as $nativeFuncCall) {
|
||||
$funcCallTypes[] = $this->getType($nativeFuncCall);
|
||||
}
|
||||
$returnType = $this->typeFactory->createMixedPassedOrUnionType($funcCallTypes);
|
||||
if ($returnType instanceof MixedType) {
|
||||
return null;
|
||||
}
|
||||
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
|
||||
if (!$returnTypeNode instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
$node->returnType = $returnTypeNode;
|
||||
return $node;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersion::PHP_70;
|
||||
}
|
||||
}
|
|
@ -1,132 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Yield_;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersion;
|
||||
use Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictReturnExprRector\ReturnTypeFromStrictReturnExprRectorTest
|
||||
*/
|
||||
final class ReturnTypeFromStrictReturnExprRector extends AbstractRector implements MinPhpVersionInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeAnalyzer\AlwaysStrictBoolExprAnalyzer
|
||||
*/
|
||||
private $alwaysStrictBoolExprAnalyzer;
|
||||
public function __construct(AlwaysStrictBoolExprAnalyzer $alwaysStrictBoolExprAnalyzer)
|
||||
{
|
||||
$this->alwaysStrictBoolExprAnalyzer = $alwaysStrictBoolExprAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add strict return type based on returned strict expr type', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return $this->first() && $this->somethingElse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
public function run(): bool
|
||||
{
|
||||
return $this->first() && $this->somethingElse();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($node->returnType !== null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->hasSingleStrictReturn($node)) {
|
||||
return null;
|
||||
}
|
||||
$node->returnType = new Identifier('bool');
|
||||
return $node;
|
||||
}
|
||||
public function provideMinPhpVersion() : int
|
||||
{
|
||||
return PhpVersion::PHP_70;
|
||||
}
|
||||
/**
|
||||
* @param Return_[] $returns
|
||||
*/
|
||||
private function areExclusiveExprReturns(array $returns) : bool
|
||||
{
|
||||
foreach ($returns as $return) {
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
private function hasClassMethodRootReturn(ClassMethod $classMethod) : bool
|
||||
{
|
||||
foreach ((array) $classMethod->stmts as $stmt) {
|
||||
if ($stmt instanceof Return_) {
|
||||
return \true;
|
||||
}
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
private function hasSingleStrictReturn(ClassMethod $classMethod) : bool
|
||||
{
|
||||
if ($classMethod->stmts === null) {
|
||||
return \false;
|
||||
}
|
||||
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($classMethod, [Yield_::class])) {
|
||||
return \false;
|
||||
}
|
||||
/** @var Return_[] $returns */
|
||||
$returns = $this->betterNodeFinder->findInstancesOfInFunctionLikeScoped($classMethod, Return_::class);
|
||||
if ($returns === []) {
|
||||
return \false;
|
||||
}
|
||||
// is one statement depth 3?
|
||||
if (!$this->areExclusiveExprReturns($returns)) {
|
||||
return \false;
|
||||
}
|
||||
// has root return?
|
||||
if (!$this->hasClassMethodRootReturn($classMethod)) {
|
||||
return \false;
|
||||
}
|
||||
foreach ($returns as $return) {
|
||||
// we need exact expr return
|
||||
if (!$return->expr instanceof Expr) {
|
||||
return \false;
|
||||
}
|
||||
if (!$this->alwaysStrictBoolExprAnalyzer->isStrictBoolExpr($return->expr)) {
|
||||
return \false;
|
||||
}
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
}
|
|
@ -16,11 +16,11 @@ final class VersionResolver
|
|||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'e42466004ebbbbcfdeff4b7afebdfa8c8b092348';
|
||||
public const PACKAGE_VERSION = '237f255023d122d970f9a567dc4685db9c56b978';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2022-06-25 21:58:25';
|
||||
public const RELEASE_DATE = '2022-06-26 11:56:28';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
|
@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
|
|||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092::getLoader();
|
||||
return ComposerAutoloaderInit38ba857e9e694c7dd8d7f2a0a6917c37::getLoader();
|
||||
|
|
6
vendor/composer/autoload_classmap.php
vendored
6
vendor/composer/autoload_classmap.php
vendored
|
@ -2997,7 +2997,10 @@ return array(
|
|||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ControllerRenderMethodAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ControllerRenderMethodAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnFilter\\ExclusiveNativeFuncCallReturnMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnFilter/ExclusiveNativeFuncCallReturnMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictBoolReturnTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictBoolReturnTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictNativeFunctionReturnTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\DetailedTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeTypeAnalyzer/DetailedTypeAnalyzer.php',
|
||||
|
@ -3022,7 +3025,8 @@ return array(
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnAnnotationIncorrectNullableRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnAnnotationIncorrectNullableRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNativeFuncCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',
|
||||
|
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
|
@ -2,7 +2,7 @@
|
|||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092
|
||||
class ComposerAutoloaderInit38ba857e9e694c7dd8d7f2a0a6917c37
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
|
@ -22,19 +22,19 @@ class ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092
|
|||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit38ba857e9e694c7dd8d7f2a0a6917c37', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit38ba857e9e694c7dd8d7f2a0a6917c37', 'loadClassLoader'));
|
||||
|
||||
require __DIR__ . '/autoload_static.php';
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit9bd05731d04b20ce3df7ea2849039092::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37::getInitializer($loader));
|
||||
|
||||
$loader->setClassMapAuthoritative(true);
|
||||
$loader->register(true);
|
||||
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit9bd05731d04b20ce3df7ea2849039092::$files;
|
||||
$includeFiles = \Composer\Autoload\ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37::$files;
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequire9bd05731d04b20ce3df7ea2849039092($fileIdentifier, $file);
|
||||
composerRequire38ba857e9e694c7dd8d7f2a0a6917c37($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
|
@ -46,7 +46,7 @@ class ComposerAutoloaderInit9bd05731d04b20ce3df7ea2849039092
|
|||
* @param string $file
|
||||
* @return void
|
||||
*/
|
||||
function composerRequire9bd05731d04b20ce3df7ea2849039092($fileIdentifier, $file)
|
||||
function composerRequire38ba857e9e694c7dd8d7f2a0a6917c37($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
|
||||
|
|
14
vendor/composer/autoload_static.php
vendored
14
vendor/composer/autoload_static.php
vendored
|
@ -4,7 +4,7 @@
|
|||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInit9bd05731d04b20ce3df7ea2849039092
|
||||
class ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37
|
||||
{
|
||||
public static $files = array (
|
||||
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
|
||||
|
@ -3298,7 +3298,10 @@ class ComposerStaticInit9bd05731d04b20ce3df7ea2849039092
|
|||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodAndPropertyAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodAndPropertyAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ClassMethodParamTypeCompleter' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ClassMethodParamTypeCompleter.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ControllerRenderMethodAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ControllerRenderMethodAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnFilter\\ExclusiveNativeFuncCallReturnMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnFilter/ExclusiveNativeFuncCallReturnMatcher.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnStrictTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnStrictTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictBoolReturnTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictBoolReturnTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictNativeFunctionReturnTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\CallTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php',
|
||||
'Rector\\TypeDeclaration\\NodeTypeAnalyzer\\DetailedTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeTypeAnalyzer/DetailedTypeAnalyzer.php',
|
||||
|
@ -3323,7 +3326,8 @@ class ComposerStaticInit9bd05731d04b20ce3df7ea2849039092
|
|||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnAnnotationIncorrectNullableRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnAnnotationIncorrectNullableRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnNeverTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnNeverTypeRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromReturnNewRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictReturnExprRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictBoolReturnExprRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictBoolReturnExprRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNativeFuncCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNativeFuncCallRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php',
|
||||
|
@ -3403,9 +3407,9 @@ class ComposerStaticInit9bd05731d04b20ce3df7ea2849039092
|
|||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit9bd05731d04b20ce3df7ea2849039092::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit9bd05731d04b20ce3df7ea2849039092::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit9bd05731d04b20ce3df7ea2849039092::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit38ba857e9e694c7dd8d7f2a0a6917c37::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user