Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExponentiateHelper: fix internal error and internal warning #3031

Open
wants to merge 2 commits into
base: 1.11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Type/ExponentiateHelper.php
Expand Up @@ -4,8 +4,10 @@

use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use Throwable;
use function is_float;
use function is_int;
use function pow;

final class ExponentiateHelper
{
Expand Down Expand Up @@ -95,7 +97,12 @@ private static function exponentiateConstantScalar(ConstantScalarType $base, Typ
}

if ($exponent instanceof ConstantScalarType) {
$result = $base->getValue() ** $exponent->getValue();
try {
// @ to avoid "Warning: A non-numeric value encountered"
$result = @pow($base->getValue(), $exponent->getValue()); // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather prevent the problem by checking the type of the values instead of silencing the error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require some crazy regexes. I dont think there is a simple way to simulate the magical numeric-string behaviour: https://3v4l.org/ECZjp#v8.3.6

This way we are sure we get exactly what PHP evalutes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like is_numeric would do the job: https://3v4l.org/sYolO#v8.3.6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, those are both numeric, but results in different output type: https://3v4l.org/aDslS#v8.3.6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a similar open PR you might can get inspiration from

#2796

} catch (Throwable) {
return new ErrorType();
}
if (is_int($result)) {
return new ConstantIntegerType($result);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -191,6 +191,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-1014.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-pr-339.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/pow.php');
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/pow-php8.php');
} else {
yield from $this->gatherAssertTypes(__DIR__ . '/data/pow-php7.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/throw-expr.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5351.php');

Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/pow-php7.php
@@ -0,0 +1,8 @@
<?php

namespace PowFunction;

use function PHPStan\Testing\assertType;

assertType('4', '4a' ** 1);
assertType('0', 'a' ** 1);
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/pow-php8.php
@@ -0,0 +1,8 @@
<?php

namespace PowFunction;

use function PHPStan\Testing\assertType;

assertType('4', '4a' ** 1);
assertType('*ERROR*', 'a' ** 1);