Skip to content

Commit 4d807f2

Browse files
committed
fix
1 parent ba5bdb9 commit 4d807f2

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/Type/ExponentiateHelper.php

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPStan\Type\Constant\ConstantIntegerType;
88
use function is_float;
99
use function is_int;
10+
use function pow;
1011

1112
final class ExponentiateHelper
1213
{
@@ -25,28 +26,9 @@ public static function exponentiate(Type $base, Type $exponent): Type
2526
return new NeverType();
2627
}
2728

28-
$allowedOperandTypes = new UnionType([
29-
new IntegerType(),
30-
new FloatType(),
31-
new IntersectionType([
32-
new StringType(),
33-
new AccessoryNumericStringType(),
34-
]),
35-
new BooleanType(),
36-
new NullType(),
37-
]);
38-
if (!$allowedOperandTypes->isSuperTypeOf($exponent)->yes()) {
39-
return new ErrorType();
40-
}
41-
if (!$allowedOperandTypes->isSuperTypeOf($base)->yes()) {
42-
return new ErrorType();
43-
}
44-
45-
if ($base instanceof ConstantScalarType) {
46-
$result = self::exponentiateConstantScalar($base, $exponent);
47-
if ($result !== null) {
48-
return $result;
49-
}
29+
$result = self::exponentiateConstantScalar($base, $exponent);
30+
if ($result !== null) {
31+
return $result;
5032
}
5133

5234
// exponentiation of a float, stays a float
@@ -84,16 +66,37 @@ public static function exponentiate(Type $base, Type $exponent): Type
8466
]);
8567
}
8668

87-
private static function exponentiateConstantScalar(ConstantScalarType $base, Type $exponent): ?Type
69+
private static function exponentiateConstantScalar(Type $base, Type $exponent): ?Type
8870
{
71+
$allowedOperandTypes = new UnionType([
72+
new IntegerType(),
73+
new FloatType(),
74+
new IntersectionType([
75+
new StringType(),
76+
new AccessoryNumericStringType(),
77+
]),
78+
new BooleanType(),
79+
new NullType(),
80+
]);
81+
if (!$allowedOperandTypes->isSuperTypeOf($exponent)->yes()) {
82+
return new ErrorType();
83+
}
84+
if (!$allowedOperandTypes->isSuperTypeOf($base)->yes()) {
85+
return new ErrorType();
86+
}
87+
88+
if (!$base instanceof ConstantScalarType) {
89+
return null;
90+
}
91+
8992
if ($exponent instanceof IntegerRangeType) {
9093
$min = null;
9194
$max = null;
9295
if ($exponent->getMin() !== null) {
93-
$min = $base->getValue() ** $exponent->getMin();
96+
$min = self::pow($base->getValue(), $exponent->getMin());
9497
}
9598
if ($exponent->getMax() !== null) {
96-
$max = $base->getValue() ** $exponent->getMax();
99+
$max = self::pow($base->getValue(), $exponent->getMax());
97100
}
98101

99102
if (!is_float($min) && !is_float($max)) {
@@ -102,7 +105,7 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
102105
}
103106

104107
if ($exponent instanceof ConstantScalarType) {
105-
$result = $base->getValue() ** $exponent->getValue();
108+
$result = self::pow($base->getValue(), $exponent->getValue());
106109
if (is_int($result)) {
107110
return new ConstantIntegerType($result);
108111
}
@@ -112,4 +115,12 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
112115
return null;
113116
}
114117

118+
/**
119+
* @return float|int
120+
*/
121+
private static function pow(mixed $base, mixed $exp)
122+
{
123+
return pow($base, $exp);
124+
}
125+
115126
}

0 commit comments

Comments
 (0)