-
Notifications
You must be signed in to change notification settings - Fork 483
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
Add return type extension for array_column() #948
Conversation
b81d119
to
9e2e1f3
Compare
There's some noise in the CI pipeline which I need to look into, but here are some real issues found with your extension: https://github.com/phpstan/phpstan-src/runs/4977783162?check_suite_focus=true |
Mmh, yes I see the problem:
Reported error:
This code is obviously correct, but teaching PHPStan to correctly recognize this is obviously not possible at the moment. The previously inferred return type was |
What if we first ran array_column to extract the value of nodes, and only after that we ran array_filter? That could preserve the type. |
@@ -92,6 +92,9 @@ public function or(self ...$operands): self | |||
|
|||
public static function extremeIdentity(self ...$operands): self | |||
{ | |||
if ($operands === []) { | |||
throw new ShouldNotHappenException(); | |||
} | |||
$operandValues = array_column($operands, 'value'); |
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.
What's the current behaviour with an empty array? If the code doesn't crash then this is a BC break.
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.
min([])
and max([])
cause fatal errors on PHP >= 8. On PHP < 8 it causes a warning and returns false
, which gets coerced to 0
when given to create()
. See https://3v4l.org/RS767
This would indeed work! I didn't even know PHPStan recognizes |
Can you please send a PR to phpdoc-parser that switches those calls around? :) Thanks. |
9e2e1f3
to
90210ef
Compare
Thank you! |
Hi, 21 days ago, so right after the release of v1.4.4 (22 days ago) the daily build on ekino/phpstan-banned-code for PHP 7.4 broke. Could it be possible that this is due to this pull request?
which in a simplified version means that phpstan thinks that Here is an example file to reproduce the (potential) problem:
and this is the phpstan.neon.dist used:
Is something wrong about the example code? Did I misunderstand something? |
@BitScout Please open a new issue (Bug report). |
Note that when the iterable value type of the given array is a
ConstantArrayType
with optional keys, then it will not attempt to produce a precise result. I think it's not worth supporting this and would either produce useless types, or unnecessarily complex types.