Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update ExponentiateHelper.php
  • Loading branch information
staabm committed Jul 17, 2024
commit bc8af0d7d5ec4aadfc0d62e09041d855f1aa2bf8
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;
}
}

}