-
Notifications
You must be signed in to change notification settings - Fork 502
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
Conversation
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. |
There was a problem hiding this 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"?
} | ||
} | ||
|
||
/** |
There was a problem hiding this comment.
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.
@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 |
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. |
ac033fd
to
9096a80
Compare
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 |
Thank you! |
Enables the following:
Fixes phpstan/phpstan#10645 (partially)