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

MissingPureAnnotation issues #4300

Closed
enumag opened this issue Oct 8, 2020 · 5 comments · Fixed by #7545
Closed

MissingPureAnnotation issues #4300

enumag opened this issue Oct 8, 2020 · 5 comments · Fixed by #7545

Comments

@enumag
Copy link
Contributor

enumag commented Oct 8, 2020

I ran into some issues while running --alter --issues=MissingPureAnnotation. I created a repo to reproduce them:

https://github.com/enumag/psalm-bug-2

composer install
vendor/bin/psalm --alter --issues=MissingPureAnnotation --dry-run

output:

Altering files...
/home/enumag/Projects/enumag/psalm-bug-2/src/index.php:
--- /home/enumag/Projects/enumag/psalm-bug-2/src/index.php
+++ /home/enumag/Projects/enumag/psalm-bug-2/src/index.php
@@ -5,12 +5,15 @@
 final class Arrays
 {
        /**
-        * Lorem ipsum.
+        *       * Lorem ipsum.
+        *       *
         *
         * @param mixed[]                   $array
         * @param Closure(mixed): array-key $callback
         *
         * @return mixed[]
+        *
+        * @psalm-pure
         */
        public static function indexBy(array $array, Closure $callback): array
        {

1) Docblock text

As you noticed psalm somehow doubled the whitespace and star before the docblock description.

2) @psalm-pure should not be added

Because the $callback closure is not pure.

3) Impossible to mark the callback as pure

I want to keep the callback as Closure only and it won't actually be pure. However I tried to mark it a pure anyway using @param pure-callbable&Closure(mixed): array-key $callback just to see if it's possible. But then vendor/bin/psalm fails with some false-positive:

ERROR: InvalidDocblock - src/index.php:11:12 - Intersection types must be all objects or all object-like arrays, Psalm\Type\Atomic\TCallable provided in docblock for Arrays::indexBy (see https://psalm.dev/008)
         * @param pure-callable&Closure(mixed): array-key $callback
@weirdan
Copy link
Collaborator

weirdan commented Oct 8, 2020

re: pure param annotation - it should be possible to specify it like this: https://psalm.dev/r/add7ac5ba4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/add7ac5ba4
<?php

/**
 * @psalm-pure
 * @param pure-Closure():int $c
 */
function f(Closure $c): int {
    return $c();
}

echo f(function () { return rand(0,1); });
Psalm output (using commit e9b520d):

ERROR: MismatchingDocblockParamType - 5:11 - Parameter $c has wrong type 'pure-callable():int', should be 'Closure'

ERROR: InvalidArgument - 11:8 - Argument 1 of f expects pure-callable():int, impure-Closure():int provided

@enumag
Copy link
Contributor Author

enumag commented Oct 8, 2020

I see. That solves the last issue then. Thanks!

@orklah
Copy link
Collaborator

orklah commented Jan 31, 2022

A lot have changed since then. Could you check if the two first issues are still there?

@enumag
Copy link
Contributor Author

enumag commented Jan 31, 2022

@orklah First issue is indeed solved. The second one is not. Even Psalm 4.19 is still trying to add @psalm-pure when the closure is not marked as pure.

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

Successfully merging a pull request may close this issue.

3 participants