psalm icon indicating copy to clipboard operation
psalm copied to clipboard

psalm --alter introduce syntax errors into code

Open parf opened this issue 3 years ago • 10 comments

psalm --alter adds non existent classes nulld, nulle, \Closured into source code

> tools/psalm.phar --version
Psalm 4.x-dev@
> php -v
PHP 8.0.18 (cli) (built: Apr 13 2022 02:45:05) ( NTS gcc x86_64 )

tools/psalm.phar --alter --issues=MissingParamType --dry-run

     static function map(
         iterable $arr,
-        int|string|array|\Closure $map = null,
-        mixed $where = null,
-        mixed $skip = null,
-        mixed $while = null,
+        int|nulle $map = null,
+        int|nulld $where = null,
+        int|nulld $skip = null,
+        int|nulld $while = null,
         mixed $reverse = false,
         
         
      *
      * @param iterable<mixed> $arr
      */
-    static function _while(iterable $arr, mixed $while): \Generator {
+    static function _while(iterable $arr, \Closured $while): \Generator {

         

parf avatar May 09 '22 19:05 parf

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

psalm-github-bot[bot] avatar May 09 '22 19:05 psalm-github-bot[bot]

--alter --issues=MissingParamType --dry-run

not really, there is no --alter optoin. here is a link to sample code https://psalm.dev/r/59942a73d8 this code however does not have above error

parf avatar May 09 '22 19:05 parf

I found these snippets:

https://psalm.dev/r/59942a73d8
<?php
abstract class Arr0 {

     /**
     * @param iterable<mixed>              $arr
     * @param \Closure|int|string|string[] $map
     *
     * @return mixed[]
     */

    static function map(
        iterable $arr,
        int|string|array|\Closure $map = null,
        mixed $where = null,
        mixed $skip = null,
        mixed $while = null,
        mixed $reverse = false,
    ): array {
        return [];
    }
}
Psalm output (using commit f960d71):

No issues!

psalm-github-bot[bot] avatar May 09 '22 19:05 psalm-github-bot[bot]

Could you check if you have weird characters in your file? Maybe accents or wrongly encoded unicode chars?

orklah avatar May 19 '22 19:05 orklah

I have the same issue. It changes the paramType to what it deems correct concatenated with the last letter of the original typing. For example mixed will change to intd instead of int.

stejes avatar Nov 16 '22 16:11 stejes

@orklah it's a genuine bug I've been able to reproduce.

weirdan avatar Nov 16 '22 17:11 weirdan

https://psalm.dev/r/4cbc908e6e (press Fix code).

weirdan avatar Nov 16 '22 18:11 weirdan

I found these snippets:

https://psalm.dev/r/4cbc908e6e
<?php
abstract class Arr0 {

     /**
     * @param iterable<mixed>              $arr
     * @param \Closure|int|string|string[] $map
     * @psalm-suppress UnusedParam
     */

    static function map(
        iterable $arr,
        int|string|array|\Closure $map = null,
        mixed $where = null,
        mixed $skip = null,
        mixed $while = null,
        mixed $reverse = false,
    ): void {
        
    }
}
$a = Arr0::map([], 1, 1, 1, 1);
Psalm output (using commit 12f33fa):

ERROR: AssignmentToVoid - 21:1 - Cannot assign $a to type void

INFO: UnusedVariable - 21:1 - $a is never referenced or the value is not used

psalm-github-bot[bot] avatar Nov 16 '22 18:11 psalm-github-bot[bot]

But I don't think we should be fixing explicit mixed in parameter types at all.

weirdan avatar Nov 16 '22 18:11 weirdan

This is fixed in #9803, isn't it?

ciltocruz avatar Sep 17 '25 22:09 ciltocruz