psalm icon indicating copy to clipboard operation
psalm copied to clipboard

non-empty-literal-string|literal-string is inferred as string

Open VincentLanglet opened this issue 2 months ago • 4 comments

https://psalm.dev/r/21adcbe806

VincentLanglet avatar Apr 28 '24 22:04 VincentLanglet

I found these snippets:

https://psalm.dev/r/21adcbe806
<?php

/**
 * @param non-empty-literal-string $a
 * @param literal-string $b
 */
function foo(string $a, string $b) {
    /** @psalm-trace $c */
    $c = random_int(0, 12) ? $a : $b;
}
Psalm output (using commit 08afc45):

INFO: Trace - 9:5 - $c: string

INFO: UnusedVariable - 9:5 - $c is never referenced or the value is not used

INFO: UnusedParam - 7:32 - Param b is never referenced in this method

INFO: UnusedParam - 7:21 - Param a is never referenced in this method

INFO: MissingReturnType - 7:10 - Method foo does not have a return type, expecting void

psalm-github-bot[bot] avatar Apr 28 '24 22:04 psalm-github-bot[bot]

Hi @orklah @weirdan,

I assume this could be an easy fix but I have no idea where such type resolution is done in the psalm codebase. Any idea which method does such job ?

VincentLanglet avatar Apr 28 '24 22:04 VincentLanglet

Yeah, it should be around https://github.com/vimeo/psalm/blob/c6311028647126fa760ff54b580bcde7ca9a0f92/src/Psalm/Internal/Type/TypeCombiner.php#L987

The TypeCombiner is the class that makes true|false become bool or non-empty-string|non-falsy-string becomes non-empty-string for example.

specifically, scrapeStringProperties handles strings of different shapes, you can see things like $has_only_numeric_strings or $has_only_non_empty_strings that are flags to check that every string match the property you want to conserve at the end

orklah avatar Apr 29 '24 16:04 orklah

Thanks, the PR is done https://github.com/vimeo/psalm/pull/10930

(I also will need your help for https://github.com/vimeo/psalm/pull/10927#issuecomment-2083300456)

VincentLanglet avatar Apr 29 '24 20:04 VincentLanglet