Skip to content

Commit

Permalink
Update ExponentiateHelper.php
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jul 17, 2024
1 parent dc0180e commit bc8af0d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Type/ExponentiateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use Throwable;
use function is_float;
use function is_int;
use function pow;
Expand Down Expand Up @@ -85,9 +86,15 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
$max = null;
if ($exponent->getMin() !== null) {
$min = self::pow($base->getValue(), $exponent->getMin());
if ($min === null) {
return null;
}
}
if ($exponent->getMax() !== null) {
$max = self::pow($base->getValue(), $exponent->getMax());
if ($max === null) {
return null;
}
}

if (!is_float($min) && !is_float($max)) {
Expand All @@ -97,6 +104,11 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ

if ($exponent instanceof ConstantScalarType) {
$result = self::pow($base->getValue(), $exponent->getValue());

if ($result === null) {
return null;
}

if (is_int($result)) {
return new ConstantIntegerType($result);
}
Expand All @@ -106,12 +118,13 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
return null;
}

/**
* @return float|int
*/
private static function pow(mixed $base, mixed $exp)
private static function pow(mixed $base, mixed $exp): float|int|null
{
return pow($base, $exp);
try {
return pow($base, $exp);
} catch (Throwable) {
return null;
}
}

}

0 comments on commit bc8af0d

Please sign in to comment.