Skip to content

Add support for constructor assertions#2950

Merged
ondrejmirtes merged 1 commit intophpstan:1.10.xfrom
axlon:constructor-assert
Mar 1, 2024
Merged

Add support for constructor assertions#2950
ondrejmirtes merged 1 commit intophpstan:1.10.xfrom
axlon:constructor-assert

Conversation

@axlon
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 👍

@axlon axlon requested a review from ondrejmirtes February 29, 2024 18:37
@ondrejmirtes ondrejmirtes merged commit 8edfde0 into phpstan:1.10.x Mar 1, 2024
@ondrejmirtes
Copy link
Copy Markdown
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

Development

Successfully merging this pull request may close these issues.

Assertions don't work on class constructors

3 participants