vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.55k stars 659 forks source link

Details of mixed places with `--stats` #4430

Open thunderer opened 3 years ago

thunderer commented 3 years ago

How to find the exact places where Psalm --stats thinks the value is mixed? I see exactly one file with 99% (1 mixed), there are no other issues or suggestions with or without it. Should there be any mixed places when there are no violations using maximum level 1 and totallyTyped is turned on?

For the reference, I'm talking about Shortcode library. There is one mixed place in WordpressParser I have trouble finding. Thanks in advance for your help.

psalm-github-bot[bot] commented 3 years ago

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

weirdan commented 3 years ago

I think it's caused by array_map('preg_quote', $this->names);. preg_quote() may accept two arguments, but Psalm sees you're only passing a single array, so it must be assuming the second argument is mixed.

Replacing that with explicit lambda gets rid of that mixed:

diff --git a/src/Parser/WordpressParser.php b/src/Parser/WordpressParser.php
index 41b5f90..9eee024 100644
--- a/src/Parser/WordpressParser.php
+++ b/src/Parser/WordpressParser.php
@@ -72,7 +72,7 @@ public static function createFromNames(array $names)
     public function parse($text)
     {
         $names = $this->names
-            ? implode('|', array_map('preg_quote', $this->names))
+            ? implode('|', array_map(function($arg) { return preg_quote($arg, '/'); }, $this->names))
             : RegexBuilderUtility::buildNameRegex();
         $regex = str_replace('<NAMES>', $names, static::$shortcodeRegex);
         preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE);
weirdan commented 3 years ago

Note that you probably need that second parameter, otherwise slashes in $this->names would cause regex compilation error.

thunderer commented 3 years ago

@weirdan thanks, indeed that was the place. I had similar issues in other (closed-source) projects where I can't share code publicly, though. Can you answer my original questions?

weirdan commented 3 years ago

can Psalm report such places?

Not to my knowledge.

should there be any unreported mixed placed with totallyTyped and level 1?

I think there shouldn't, but as far as I understand, from Psalm's point of view that wasn't really an error as the function had the default value for that parameter. I believe Psalm shouldn't increment mixed count in this use case - so in my opinion that's the bug.