Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Psl\Type\non_empty_string() is not marked as @psalm-pure? #130

Open
Ocramius opened this issue Feb 16, 2021 · 4 comments
Open

Psl\Type\non_empty_string() is not marked as @psalm-pure? #130

Ocramius opened this issue Feb 16, 2021 · 4 comments
Assignees
Labels
Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@Ocramius
Copy link
Contributor

In the context of following snippet:

    /**
     * @throws InvalidPackageName
     *
     * @psalm-pure
     */
    public static function fromName(string $name): self
    {
        if (preg_match('{^[a-z0-9](?:[_.-]?[a-z0-9]+)*/[a-z0-9](?:(?:[_.]?|-{0,2})[a-z0-9]+)*$}iD', $name) !== 1) {
            throw InvalidPackageName::fromInvalidName($name);
        }

        return new self(
            non_empty_string()
                ->coerce($name)
        );
    }

psalm reports:

... Cannot call an impure function from a mutation-free context (see https://psalm.dev/202)
            non_empty_string()

I wonder if all type constructors can/should be declared pure? Is this where we got stuck on conditional type purity?

@Ocramius Ocramius added the Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Feb 16, 2021
@azjezz
Copy link
Owner

azjezz commented Feb 16, 2021

back to vimeo/psalm#4145 😛

the issue here is that we can't declare Type::assert/Type::coerce as "pure", because not all implementations are pure ( for example IterableType::assert iterates over an iterable, which might not be immutable ).

so i suggest you add @psalm-suppress here.

@azjezz
Copy link
Owner

azjezz commented Feb 16, 2021

Also, i attempted to improve this by having the psalm plugin report the call as pure if the argument is pure/immutable, but that is not possible via the plugin system, and it seems psalm have a special case for internal php functions ( e.g: https://github.com/vimeo/psalm/blob/bd6efd7cf2b7e6fdf01b343be0c5bbb9aacdbbbb/src/Psalm/Internal/Codebase/Functions.php#L499 )

@Ocramius
Copy link
Contributor Author

Wow that's ugly :D

Guess we'll have to improve upstream before being able to declare it here: still like vimeo/psalm#4145 as an idea, but hard to bring it to life :-\

@azjezz
Copy link
Owner

azjezz commented Feb 16, 2021

yea, i will trying to play around with psalm to see if i can make it possible to have special "purity checkers" for function/method calls, this will allow to mark things like Type\string()->assert() pure without the need for vimeo/psalm#4145 using the plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
Development

No branches or pull requests

2 participants