Skip to content

Commit

Permalink
Merge branch refs/heads/1.9.x into 1.10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot authored Jan 12, 2023
2 parents 7380ed0 + ed14b2a commit b0fbe29
Show file tree
Hide file tree
Showing 38 changed files with 539 additions and 74 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ parameters:
count: 1
path: src/Reflection/InitializerExprTypeResolver.php

-
message: "#^Binary operation \"\\*\\*\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\|string\\|null results in an error\\.$#"
count: 1
path: src/Reflection/InitializerExprTypeResolver.php

-
message: "#^Binary operation \"\\+\" between bool\\|float\\|int\\|string\\|null and bool\\|float\\|int\\|string\\|null results in an error\\.$#"
count: 1
Expand Down
101 changes: 32 additions & 69 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1170,57 +1170,17 @@ public function getPowType(Expr $left, Expr $right, callable $getTypeCallback):
$leftType = $getTypeCallback($left);
$rightType = $getTypeCallback($right);

if ($leftType instanceof MixedType || $rightType instanceof MixedType) {
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
$exponentiatedTyped = $leftType->exponentiate($rightType);
if (!$exponentiatedTyped instanceof ErrorType) {
return $exponentiatedTyped;
}

$object = new ObjectWithoutClassType();
if (
!$leftType instanceof NeverType &&
!$rightType instanceof NeverType &&
(
!$object->isSuperTypeOf($leftType)->no()
|| !$object->isSuperTypeOf($rightType)->no()
)
) {
return TypeCombinator::union($leftType, $rightType);
$extensionSpecified = $this->callOperatorTypeSpecifyingExtensions(new BinaryOp\Pow($left, $right), $leftType, $rightType);
if ($extensionSpecified !== null) {
return $extensionSpecified;
}

$leftTypes = TypeUtils::getConstantScalars($leftType);
$rightTypes = TypeUtils::getConstantScalars($rightType);
$leftTypesCount = count($leftTypes);
$rightTypesCount = count($rightTypes);
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
$resultTypes = [];
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
foreach ($leftTypes as $leftTypeInner) {
foreach ($rightTypes as $rightTypeInner) {
$leftNumberType = $leftTypeInner->toNumber();
$rightNumberType = $rightTypeInner->toNumber();

if ($leftNumberType instanceof ErrorType || $rightNumberType instanceof ErrorType) {
return new ErrorType();
}

if (!$leftNumberType instanceof ConstantScalarType || !$rightNumberType instanceof ConstantScalarType) {
throw new ShouldNotHappenException();
}

$resultType = $this->getTypeFromValue($leftNumberType->getValue() ** $rightNumberType->getValue());
if ($generalize) {
$resultType = $resultType->generalize(GeneralizePrecision::lessSpecific());
}
$resultTypes[] = $resultType;
}
}

return TypeCombinator::union(...$resultTypes);
}

return $this->resolveCommonMath(new BinaryOp\Pow($left, $right), $leftType, $rightType);
return new ErrorType();
}

/**
Expand Down Expand Up @@ -1469,14 +1429,33 @@ private function resolveConstantArrayTypeComparison(ConstantArrayType $leftType,
return $resultType->toBoolean();
}

private function callOperatorTypeSpecifyingExtensions(Expr\BinaryOp $expr, Type $leftType, Type $rightType): ?Type
{
$operatorSigil = $expr->getOperatorSigil();
$operatorTypeSpecifyingExtensions = $this->operatorTypeSpecifyingExtensionRegistryProvider->getRegistry()->getOperatorTypeSpecifyingExtensions($operatorSigil, $leftType, $rightType);

/** @var Type[] $extensionTypes */
$extensionTypes = [];

foreach ($operatorTypeSpecifyingExtensions as $extension) {
$extensionTypes[] = $extension->specifyType($operatorSigil, $leftType, $rightType);
}

if (count($extensionTypes) > 0) {
return TypeCombinator::union(...$extensionTypes);
}

return null;
}

/**
* @param BinaryOp\Plus|BinaryOp\Minus|BinaryOp\Mul|BinaryOp\Pow|BinaryOp\Div $expr
* @param BinaryOp\Plus|BinaryOp\Minus|BinaryOp\Mul|BinaryOp\Div $expr
*/
private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $rightType): Type
{
if (($leftType instanceof IntegerRangeType || $leftType instanceof ConstantIntegerType || $leftType instanceof UnionType) &&
($rightType instanceof IntegerRangeType || $rightType instanceof ConstantIntegerType || $rightType instanceof UnionType) &&
!$expr instanceof BinaryOp\Pow) {
($rightType instanceof IntegerRangeType || $rightType instanceof ConstantIntegerType || $rightType instanceof UnionType)
) {

if ($leftType instanceof ConstantIntegerType) {
return $this->integerRangeMath(
Expand Down Expand Up @@ -1507,18 +1486,9 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
return $this->integerRangeMath($leftType, $expr, $rightType);
}

$operatorSigil = $expr->getOperatorSigil();
$operatorTypeSpecifyingExtensions = $this->operatorTypeSpecifyingExtensionRegistryProvider->getRegistry()->getOperatorTypeSpecifyingExtensions($operatorSigil, $leftType, $rightType);

/** @var Type[] $extensionTypes */
$extensionTypes = [];

foreach ($operatorTypeSpecifyingExtensions as $extension) {
$extensionTypes[] = $extension->specifyType($operatorSigil, $leftType, $rightType);
}

if (count($extensionTypes) > 0) {
return TypeCombinator::union(...$extensionTypes);
$specifiedTypes = $this->callOperatorTypeSpecifyingExtensions($expr, $leftType, $rightType);
if ($specifiedTypes !== null) {
return $specifiedTypes;
}

$types = TypeCombinator::union($leftType, $rightType);
Expand Down Expand Up @@ -1546,13 +1516,6 @@ private function resolveCommonMath(Expr\BinaryOp $expr, Type $leftType, Type $ri
return new FloatType();
}

if ($expr instanceof Expr\BinaryOp\Pow) {
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
}

$resultType = TypeCombinator::union($leftNumberType, $rightNumberType);
if ($expr instanceof Expr\BinaryOp\Div) {
if ($types instanceof MixedType || $resultType->isInteger()->yes()) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,9 @@ public static function intersectWith(Type $type): Type
return $type;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

}
9 changes: 9 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Accessory;

use PHPStan\TrinaryLogic;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantArrayType;
Expand Down Expand Up @@ -256,4 +257,12 @@ public static function __set_state(array $properties): Type
return new self();
}

public function exponentiate(Type $exponent): Type
{
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
}

}
9 changes: 9 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Accessory;

use PHPStan\TrinaryLogic;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -265,4 +266,12 @@ public function tryRemove(Type $typeToRemove): ?Type
return null;
}

public function exponentiate(Type $exponent): Type
{
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
}

}
9 changes: 9 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Accessory;

use PHPStan\TrinaryLogic;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -256,4 +257,12 @@ public static function __set_state(array $properties): Type
return new self();
}

public function exponentiate(Type $exponent): Type
{
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
}

}
9 changes: 9 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Accessory;

use PHPStan\TrinaryLogic;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -268,4 +269,12 @@ public function tryRemove(Type $typeToRemove): ?Type
return null;
}

public function exponentiate(Type $exponent): Type
{
return new BenevolentUnionType([
new FloatType(),
new IntegerType(),
]);
}

}
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasMethodType.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self($properties['methodName']);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self($properties['offsetType']);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,11 @@ public function traverse(callable $cb): Type
return new self($this->offsetType, $newValueType);
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self($properties['offsetType'], $properties['valueType']);
Expand Down
6 changes: 6 additions & 0 deletions src/Type/Accessory/HasPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Reflection\TrivialParametersAcceptor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\CompoundType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
Expand Down Expand Up @@ -114,6 +115,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self($properties['propertyName']);
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ public function traverse(callable $cb): Type
return $this;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

public static function __set_state(array $properties): Type
{
return new self();
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@ public function tryRemove(Type $typeToRemove): ?Type
return null;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function tryRemove(Type $typeToRemove): ?Type
return null;
}

public function exponentiate(Type $exponent): Type
{
return ExponentiateHelper::exponentiate($this, $exponent);
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ public function isCommonCallable(): bool
return $this->isCommonCallable;
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ public function isScalar(): TrinaryLogic
return TrinaryLogic::createNo();
}

public function exponentiate(Type $exponent): Type
{
return new ErrorType();
}

/**
* @param mixed[] $properties
*/
Expand Down
Loading

0 comments on commit b0fbe29

Please sign in to comment.