-
Notifications
You must be signed in to change notification settings - Fork 659
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
#4997 added more precise type inference for count()
returning 0
or positive-int
on known arrays
#4999
#4997 added more precise type inference for count()
returning 0
or positive-int
on known arrays
#4999
Conversation
…sitive-int` on known types
Seems like current CallMap expect |
@psalm-flow is related to the continuity of taint though the function. It shouldn't be needed for count. |
…ourceBundle` counting, as well as handling hardcoded 2-element-arrays cases This patch: * adds support for `count(\SimpleXmlElement)` (https://www.php.net/manual/en/simplexmlelement.count.php) * adds support for `count(\ResourceBundle)` (https://www.php.net/manual/en/resourcebundle.count.php) * removes usage of global constants from stub (not supported - see https://www.php.net/manual/en/function.count.php) * adds support for identifying fixed-element-count arrays, for example `count(callable&array)`, which is always `2`
we can't really declare If |
EDIT: solved ✔️ (no need to refactor) This block can probably also be refactored away somehow: psalm/src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php Lines 286 to 326 in 1afce4d
The new stubs don't seem to contradict it, but I'm not sure how well tested it is right now, nor if the stubs override it. |
EDIT: solved ✔️ @orklah
https://app.circleci.com/pipelines/github/vimeo/psalm/4824/workflows/6f0caadd-ba64-414d-ad4e-d453e9cc6dab/jobs/21060, Perhaps I should allow |
👍 for that |
EDIT: solved ✔️ Mutation modifier seems to be computed in psalm's internals: psalm/src/Psalm/Internal/Codebase/Functions.php Lines 401 to 421 in 1afce4d
I think we're OK as-is, if that is still taken into account whilst looking at the stubs. I'd gladly write test scenarios for these cases, just need someone to point me at an appropriate location where to put them :-) |
…eInt` for `count(TNonEmptyArray)` and `count(TNonEmptyList)`
src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php
Show resolved
Hide resolved
src/Psalm/Internal/Analyzer/Statements/Expression/Call/FunctionCallReturnTypeFetcher.php
Show resolved
Hide resolved
I believe FunctionCallTest.php is the appropriate location for those. I added tests here in the past (for example for parse_url) |
count()
returning 0
or positive-int
on known typescount()
returning 0
or positive-int
on known arrays
…recise type of a `\count(T)` expression when given a `T`, so we baked the whole type resolution for `positive-int`, `0` and `positive-int|0` directly in there. While this complicates things, it is also true that it is not possible right now (for the stubs) to provide the level of detail around `count()` that is required by the type inference system for such a complex function with so many different semantics.
3758952
to
fad0042
Compare
@orklah thanks for pushing me in the right direction: I've added detailed tests around This ended up being much simpler than expected, given that the pre-existing code needed very minor adjustments to operate as expected, so we should be good to review and hopefully merge if the solution is acceptable 👍 |
Thanks! |
…`0` or `positive-int` on known arrays (vimeo#4999) * vimeo#4997 added more precise stub for `count()` returning `0` or `positive-int` on known types * vimeo#4997 updated `count()` to support `\SimpleXmlElement` and `\ResourceBundle` counting, as well as handling hardcoded 2-element-arrays cases This patch: * adds support for `count(\SimpleXmlElement)` (https://www.php.net/manual/en/simplexmlelement.count.php) * adds support for `count(\ResourceBundle)` (https://www.php.net/manual/en/resourcebundle.count.php) * removes usage of global constants from stub (not supported - see https://www.php.net/manual/en/function.count.php) * adds support for identifying fixed-element-count arrays, for example `count(callable&array)`, which is always `2` * vimeo#4997 adapted `FunctionCallReturnTypeFetcher` to infer `TPositiveInt` for `count(TNonEmptyArray)` and `count(TNonEmptyList)` * The `FunctionCallReturnTypeFetcher` is responsible for defining the precise type of a `\count(T)` expression when given a `T`, so we baked the whole type resolution for `positive-int`, `0` and `positive-int|0` directly in there. While this complicates things, it is also true that it is not possible right now (for the stubs) to provide the level of detail around `count()` that is required by the type inference system for such a complex function with so many different semantics.
Fixes #4997
Note: I only provided the stub, but am unsure about where tests for stubs (if there are any) are applicable.Questions open:
@psalm-flow
- unclear whether this is needed, seems related to taint analysis, so probably not necessary@psalm-pure
- unclear whether this is to be declared. Purity depends on the given argument: perhaps @azjezz knows more here? See #4997 added more precise type inference forcount()
returning0
orpositive-int
on known arrays #4999 (comment)