Correct hash function stub return type#343
Conversation
|
This need a similar treatment hash_hmac got in #328. |
|
Ah. OK. At least it doesn't have the PHP 7.1 problem as hash_algos is old enough. I would have a go at that, but I'm a bit stuck for now as #109 prevents me working on a copy locally. |
|
I just merged #109 so let me know if master works for you :) |
|
Please rebase instead of creating these merge commits. |
Correct return type of the `hash` function. [The PHP docs](https://www.php.net/manual/en/function.hash.php) say that this function always returns a string, however this is not true. It can also return a boolean `false` if you pass in the name of a non-existent hash algorithm. For example: var_dump(@hash('foo', 'bar', true)); bool(false)
|
Thanks for merging #109, it did indeed clean up my issue there. I've had a go at addressing this issue, looks like it's not quite right – I think I've not understood what I have rebased and force-pushed, but I'm not certain if it's made my earlier merge go away. |
|
I don't see the conflict - |
|
The build fails saying it's expecting Similarly the other two that are expected to return |
|
In case it's unclear, I could make this pass by setting all the expected values to |
ondrejmirtes
left a comment
There was a problem hiding this comment.
Just added three things that should make the tests pass :)
| if ($functionReflection->getName() === 'hash') { | ||
| $defaultReturnType = new StringType(); | ||
| } else { | ||
| $defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
There was a problem hiding this comment.
But we want this code be executed ;)
| } | ||
| $string = $values[0]; | ||
|
|
||
| return in_array($string->getValue(), hash_algos(), true) ? $defaultReturnType : new ConstantBooleanType(false); |
There was a problem hiding this comment.
This should be:
in_array($string->getValue(), hash_algos(), true) ? new StringType() : new ConstantBooleanType(false)|
So, still failing the same way 🤷♂️ |
|
You haven't registered the extension in config.neon... |
|
It's not executed at all. |
|
Thank you! |
|
Yay! Thanks for helping me through this - it's a very complex project you've built here. |
|
It's a complex field :) |
| } | ||
| $string = $values[0]; | ||
|
|
||
| return in_array($string->getValue(), hash_algos(), true) ? new StringType() : new ConstantBooleanType(false); |
There was a problem hiding this comment.
Would it be useful/possible to directly emit a phpstan error when hash() is used with a hash-algo not defined in hash_algos()?
There was a problem hiding this comment.
You’ll get that error if you try to use false as a string.
Correct return type of the
hashfunction. The PHP docs say that this function only returns a string, however this is not true. It can also return a booleanfalseif you pass in the name of a non-existent hash algorithm. For example: