Skip to content

Psalm seems to think of "set" as a type - #1722

Merged
tvdijen merged 1 commit into
simplesamlphp:masterfrom
pradtke:psalm-tweaks
Dec 5, 2022
Merged

tvdijen merged 1 commit into
simplesamlphp:masterfrom
pradtke:psalm-tweaks

Conversation

@pradtke

@pradtke pradtke commented Dec 5, 2022

Copy link
Copy Markdown
Contributor

I'm am certainly no psalm expert. In my attempts to add more psalm to the ratelimit module psalm was complaining that Configuration's getOptionalString could never return null even if default is null. I think that psalm maybe doesn't understand $default is set since when I changed it to $default is null or $default is string (and adjust ternary expression) then psalm stopped complaining.

Open to other ideas.

@tvdijen

tvdijen commented Dec 5, 2022

Copy link
Copy Markdown
Member

Hmm, I don't understand what the issue is.. Psalm isn't complaining about it at all, and this PR only increases the amount of psalm-errors?

@pradtke

pradtke commented Dec 5, 2022

Copy link
Copy Markdown
Contributor Author

Hi @tvdijen , it is hard to show within the SSP project because of its psalm error levels and which things are set to just info.
I created minimal repo to show the psalm error.
There is a super simple test case

    $config = Configuration::loadFromArray([
        'option' => 'value'
    ]);


    $value = $config->getOptionalString('someOption', null);
    /**
     * Psalm wrongly complains about this since it thinks $value can never be null
     */
    $this->assertNull($value);

and for phpunit it passes fine. However when running psalm it reports

ERROR: DocblockTypeContradiction - tests/ConfigurationForPsalmStuffTest.php:16:16 - Cannot resolve types for $value - docblock-defined type string does not contain null (see https://psalm.dev/155) $this->assertNull($value);

It is basically saying that the return type of $config->getOptionalString can never be null. We know it can be though, so why is psalm confused? I think it is because of @psalm-return ($default is set ? string|null : string) on getOptionalString. According to psalms conditional types docs the format is (<template param> is <union type> ? <union type> : <union type>). SSP is using set as the union type , which isn't a type in the docs (from what I can see).

So I think it should be ($default is null ? string|null : string) or ($default is string ? string: string|null)

@pradtke pradtke changed the title Psalm seemed to thing of "set" as a type Psalm seems to think of "set" as a type Dec 5, 2022
@tvdijen
tvdijen merged commit 6d8486e into simplesamlphp:master Dec 5, 2022
@github-actions

Copy link
Copy Markdown
Contributor

\n This pull request has been automatically locked since there has \n not been any recent activity after it was closed.\n Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants