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

Non-numeric strings in pow() lead to error #2796

Open
wants to merge 2 commits into
base: 1.10.x
Choose a base branch
from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Nov 29, 2023

@@ -177,3 +177,23 @@ function doFoo(int $intA, int $intB, string $s, bool $bool, $numericS, float $fl
assertType('*ERROR*', $bool ** $arr);
assertType('*ERROR*', $bool ** []);
};

function invalidConstantOperands(): void {
assertType('*ERROR*', 'a' ** 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

php fatal errors on regular (non-numeric strings). I think we should too

https://3v4l.org/iTXRN

@staabm staabm marked this pull request as ready for review November 29, 2023 21:32
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

$allowedOperandTypes = new UnionType([
new IntegerType(),
new FloatType(),
new IntersectionType([
Copy link
Contributor Author

Choose a reason for hiding this comment

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

limited the allowed types to numeric-strings (instead of just string before)

/**
* @return float|int
*/
private static function pow(mixed $base, mixed $exp)
Copy link
Contributor Author

@staabm staabm Nov 29, 2023

Choose a reason for hiding this comment

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

I had to wrap the pow operation into a separate method, as otherwise phpstan errored about invalid args to pow() / **?

new NullType(),
]);
if (!$allowedOperandTypes->isSuperTypeOf($exponent)->yes()) {
return new ErrorType();
Copy link
Member

Choose a reason for hiding this comment

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

I disagree with returning ErrorType here.
string might still be a numeric-string so it's wrong to return ErrorType for that case.

We just want to prevent the internal error the issue is about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants