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

Add support for constructor assertions #2950

Merged
merged 1 commit into from Mar 1, 2024

Conversation

axlon
Copy link
Contributor

@axlon axlon commented Feb 29, 2024

Enables the following:

class Person
{
    /** @phpstan-assert non-empty-string $firstName */
    public function __construct(string $firstName) {}
}

$person = new Person($firstName);
// $firstName can no longer be empty

Fixes phpstan/phpstan#10645 (partially)

@phpstan-bot
Copy link
Collaborator

You've opened the pull request against the latest branch 1.11.x. If your code is relevant on 1.10.x and you want it to be released sooner, please rebase your pull request and change its target to 1.10.x.

@axlon axlon changed the base branch from 1.11.x to 1.10.x February 29, 2024 17:14
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "partially"?

}
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this into anonymous function and use a string parameter coming into the function.

@axlon
Copy link
Contributor Author

axlon commented Feb 29, 2024

What do you mean by "partially"?

@ondrejmirtes This PR currently does not tackle assertions on promoted properties, so for example:

class Person
{
    /** @phpstan-assert non-empty-string $this->firstName */
    public function __construct(public string $firstName) {}
}

$person = new Person($firstName);
dumpType($person->firstName); // non-empty-string

I'm working on this as well but it needs a little more time

@ondrejmirtes
Copy link
Member

This use case does not make sense to me, you can type the property to be non-empty-string from the start, no need to assert it.

@axlon
Copy link
Contributor Author

axlon commented Feb 29, 2024

The use case is being able to annotate promoted props to be a narrowed version of the variable going in. I skipped some statements in my original example for brevity but there was supposed to be an assert call in the constructor asserting the promoted prop was not empty.

Essential this, but with promoted props:

class Person
{
    /**
     * @var non-empty-string
     */
    public string $firstName;

    public function __construct(string $firstName)
    {
        assert($firstName !== '');
        $this->firstName = $firstName;
    }
}

I am aware I could also annotate the class with @property as well, so its not really a big deal if asserting $this->firstName from a constructor is a no go 👍

@ondrejmirtes ondrejmirtes merged commit 8edfde0 into phpstan:1.10.x Mar 1, 2024
440 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants