psalm issues around usage of `global` keyword - PHP

It seems like psalm doesn't properly support the usage of global to declare a global variable:

https://psalm.dev/r/d23b41c6f3

<?php

$c = 0;

function foo() : void {
    global $c;
    $c++;
}

foo();
printf("%d", $c);

this sample has two false positives: Inside of foo() it complains about the increment of $c to be Unused which isn't true because the printf() in the global scope does make use of the increment.

It also complains in printf() because it thinks $c is mixed, but it can't be.

Asked Oct 16 '21 09:10
avatar pilif
pilif

2 Answer:

I found these snippets:

https://psalm.dev/r/d23b41c6f3

<?php

$c = 0;

function foo() : void {
    global $c;
    $c++;
}

foo();
printf("%d", $c);
Psalm output (using commit c87b19e):

INFO: UnusedVariable - 7:4 - Variable $c is never referenced

INFO: MixedArgument - 11:14 - Argument 2 of printf cannot be mixed, expecting float|int|string

1
Answered Jan 24 '20 at 13:44
avatar  of psalm-github-bot[bot]
psalm-github-bot[bot]

Fixed the unused variable false-positive, the mixed will stay because global is dangerous

1
Answered May 10 '21 at 20:51
avatar  of muglug
muglug