mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 89b3e62f82ad480b57f002096cc16af16d7ac597
89b3e62f82
[TypeDeclaration] Skip union false on NumericReturnTypeFromStrictScalarReturnsRector (#5204)
This commit is contained in:
parent
ad081742d1
commit
068fde1710
|
@ -4,30 +4,14 @@ declare (strict_types=1);
|
|||
namespace Rector\TypeDeclaration\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\BinaryOp;
|
||||
use PhpParser\Node\Expr\BinaryOp\BitwiseAnd;
|
||||
use PhpParser\Node\Expr\BinaryOp\BitwiseOr;
|
||||
use PhpParser\Node\Expr\BinaryOp\Minus;
|
||||
use PhpParser\Node\Expr\BinaryOp\Mod;
|
||||
use PhpParser\Node\Expr\BinaryOp\Mul;
|
||||
use PhpParser\Node\Expr\BinaryOp\Plus;
|
||||
use PhpParser\Node\Expr\BinaryOp\ShiftLeft;
|
||||
use PhpParser\Node\Expr\BinaryOp\ShiftRight;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\PostDec;
|
||||
use PhpParser\Node\Expr\PostInc;
|
||||
use PhpParser\Node\Expr\PreDec;
|
||||
use PhpParser\Node\Expr\PreInc;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Type\FloatType;
|
||||
use PHPStan\Type\IntegerType;
|
||||
use Rector\Core\Rector\AbstractScopeAwareRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
||||
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
|
||||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -42,9 +26,15 @@ final class NumericReturnTypeFromStrictScalarReturnsRector extends AbstractScope
|
|||
* @var \Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard
|
||||
*/
|
||||
private $classMethodReturnTypeOverrideGuard;
|
||||
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
||||
*/
|
||||
private $returnTypeInferer;
|
||||
public function __construct(ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, ReturnTypeInferer $returnTypeInferer)
|
||||
{
|
||||
$this->classMethodReturnTypeOverrideGuard = $classMethodReturnTypeOverrideGuard;
|
||||
$this->returnTypeInferer = $returnTypeInferer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -89,21 +79,14 @@ CODE_SAMPLE
|
|||
if ($node instanceof ClassMethod && $this->classMethodReturnTypeOverrideGuard->shouldSkipClassMethod($node, $scope)) {
|
||||
return null;
|
||||
}
|
||||
$return = $this->matchRootReturnWithExpr($node);
|
||||
if (!$return instanceof Return_) {
|
||||
return null;
|
||||
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
|
||||
if ($returnType->isFloat()->yes()) {
|
||||
$node->returnType = new Identifier('float');
|
||||
return $node;
|
||||
}
|
||||
if ($return->expr instanceof PreInc || $return->expr instanceof PostInc || $return->expr instanceof PostDec || $return->expr instanceof PreDec) {
|
||||
$exprType = $this->nodeTypeResolver->getNativeType($return->expr);
|
||||
if ($exprType instanceof IntegerType) {
|
||||
$node->returnType = new Identifier('int');
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// @see https://chat.openai.com/share/a9e4fb74-5366-4c4c-9998-d6caeb8b5acc
|
||||
if ($return->expr instanceof Minus || $return->expr instanceof Plus || $return->expr instanceof Mul || $return->expr instanceof Mod || $return->expr instanceof BitwiseAnd || $return->expr instanceof ShiftRight || $return->expr instanceof ShiftLeft || $return->expr instanceof BitwiseOr) {
|
||||
return $this->refactorBinaryOp($return->expr, $node);
|
||||
if ($returnType->isInteger()->yes()) {
|
||||
$node->returnType = new Identifier('int');
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -111,51 +94,4 @@ CODE_SAMPLE
|
|||
{
|
||||
return PhpVersionFeature::SCALAR_TYPES;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
*/
|
||||
private function matchRootReturnWithExpr($functionLike) : ?Return_
|
||||
{
|
||||
if ($functionLike->stmts === null) {
|
||||
return null;
|
||||
}
|
||||
foreach ($functionLike->stmts as $stmt) {
|
||||
if (!$stmt instanceof Return_) {
|
||||
continue;
|
||||
}
|
||||
if (!$stmt->expr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
return $stmt;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
|
||||
* @return null|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure
|
||||
*/
|
||||
private function refactorBinaryOp(BinaryOp $binaryOp, $functionLike)
|
||||
{
|
||||
$leftType = $this->nodeTypeResolver->getNativeType($binaryOp->left);
|
||||
$rightType = $this->nodeTypeResolver->getNativeType($binaryOp->right);
|
||||
if ($leftType instanceof IntegerType && $rightType instanceof IntegerType) {
|
||||
$functionLike->returnType = new Identifier('int');
|
||||
return $functionLike;
|
||||
}
|
||||
if ($leftType instanceof FloatType && $rightType instanceof FloatType) {
|
||||
$functionLike->returnType = new Identifier('float');
|
||||
return $functionLike;
|
||||
}
|
||||
if ($binaryOp instanceof Mul) {
|
||||
if ($leftType instanceof FloatType && $rightType instanceof IntegerType) {
|
||||
$functionLike->returnType = new Identifier('float');
|
||||
return $functionLike;
|
||||
}
|
||||
if ($leftType instanceof IntegerType && $rightType instanceof FloatType) {
|
||||
$functionLike->returnType = new Identifier('float');
|
||||
return $functionLike;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ final class VersionResolver
|
|||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = 'be0cc77862bef98727e1e3881620c5416841801f';
|
||||
public const PACKAGE_VERSION = '89b3e62f82ad480b57f002096cc16af16d7ac597';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2023-10-26 19:22:18';
|
||||
public const RELEASE_DATE = '2023-10-27 01:41:41';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue