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

Invalid unused variable inside closure #5518

Closed
jpresutti opened this issue Mar 29, 2021 · 3 comments
Closed

Invalid unused variable inside closure #5518

jpresutti opened this issue Mar 29, 2021 · 3 comments
Labels

Comments

@jpresutti
Copy link

Using Psalm/Phar 4.7

ERROR: UnusedVariable - Traits/Collection.php:120:25 - $sortA is never referenced or the value is not used (see https://psalm.dev/077)
$sortA .= $a->$k;

$sortA is definitely used in the strcmp

usort(
            $array,
            function (object $a, object $b) use ($key) {
                if (is_array($key)) {
                    $sortA = '';
                    $sortB = '';
                    foreach ($key as $k) {
                        $sortA .= $a->$k;
                        $sortB .= $b->$k;
                    }

                    return strcmp($sortA, $sortB);
                }

                return strcmp((string)$a->$key, (string)$b->$key);
            }
        );
@psalm-github-bot
Copy link

Hey @jpresutti, can you reproduce the issue on https://psalm.dev ?

@muglug
Copy link
Collaborator

muglug commented Mar 29, 2021

Concatenation with unknown object property is cause of the bug: https://psalm.dev/r/e5c59c7b9b

@psalm-github-bot
Copy link

I found these snippets:

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

/** @param array<string> $key */
function foo(object $a, string $k) : string {
	$sortA = '';
    
    $sortA .= $a->$k;

    return $sortA;
}
Psalm output (using commit 3ce41d7):

INFO: UnusedVariable - 7:5 - $sortA is never referenced or the value is not used

@muglug muglug added the bug label Mar 29, 2021
@muglug muglug closed this as completed in 7b7354e Apr 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants