-
Notifications
You must be signed in to change notification settings - Fork 439
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
base: 1.10.x
Are you sure you want to change the base?
Conversation
@@ -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); |
There was a problem hiding this comment.
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
This pull request has been marked as ready for review. |
$allowedOperandTypes = new UnionType([ | ||
new IntegerType(), | ||
new FloatType(), | ||
new IntersectionType([ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
closes phpstan/phpstan#10125