You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for this feature as well, and this would be helpful for every method. Your example is precisely my use case. The fluent notation makes it much more cleaner to use. Your current PR is only the notNull method. Is your intention to add this to all methods? Otherwise I could also extend on this PR.
@frankdekker, I will add support for other methods as well if the maintainer accepts that such a feature is required. We should wait for @webmozart's reply.
I think that Assert should be used inside the createCustomer method:
publicfunctioncreateCustomer(mixed$firstName, mixed$lastName): void
{
Assert::stringNotEmpty($firstName);
Assert::stringNotEmpty($lastName);
// And then database write, or whatever
}
The usage suggested by OP promotes using Assert (and thus, Throwable) as a validation tool, which is not how this package was intended to be used. Throws are far too costly in PHP to be used for validation.
@shadowhand , that is an interesting idea. Unfortunately, the createCustomer method is a part of the library, and there is no easy way to extend it. Furthermore, createCustomer is strictly typed and does not allow nullable values to be passed as a parameter. Therefore, the only way to make static analysis tools happy is to add type guards before calling the method.
We had a similar need in our project, when working with poorly typed things (mostly vendor code). We decided to introduce an AssertReturn class which looks like this:
While I like this idea, it is very hard to implement with full static analysis. Or at least, I am not smart enough to understand how to correctly designate the change from, e.g. a mixed to a non-empty-string correctly. Simply setting the @return non-empty-string will cause all kinds of internal oddities in my experience.
All of which is to say, I am currently setting up a v2.0 branch, and I like this idea, but I need help implementing.
Will do, what would be your ideal minimum accepted PHP version? Do you want a proper native PHP approach (where possible) which would require I think PHP 8.0 (for union types) and enrich it with docblock @return or do you only want to use @return to keep the PHP 7.2 supported?
Do you want to keep supporting PHP 7 or do we want to "force" users to update their EOL PHP versions to the 'Active support' / 'Security fixes only' versions (currently 8.1, 8.2, 8.3, 8.4).
Activity
Implement return of validated value (webmozarts#280)
frankdekker commentedon Jan 14, 2023
I'm looking for this feature as well, and this would be helpful for every method. Your example is precisely my use case. The fluent notation makes it much more cleaner to use. Your current PR is only the
notNullmethod. Is your intention to add this to all methods? Otherwise I could also extend on this PR.dbalabka commentedon Jan 16, 2023
@frankdekker, I will add support for other methods as well if the maintainer accepts that such a feature is required. We should wait for @webmozart's reply.
shadowhand commentedon Oct 15, 2024
I think that Assert should be used inside the
createCustomermethod:The usage suggested by OP promotes using Assert (and thus, Throwable) as a validation tool, which is not how this package was intended to be used. Throws are far too costly in PHP to be used for validation.
dbalabka commentedon Oct 21, 2024
@shadowhand , that is an interesting idea. Unfortunately, the
createCustomermethod is a part of the library, and there is no easy way to extend it. Furthermore,createCustomeris strictly typed and does not allow nullable values to be passed as a parameter. Therefore, the only way to make static analysis tools happy is to add type guards before calling the method.ruudk commentedon Mar 3, 2025
We had a similar need in our project, when working with poorly typed things (mostly vendor code). We decided to introduce an AssertReturn class which looks like this:
I wish I didn't have to create this, and could just use
echo Assert::string($input);instead.shadowhand commentedon Oct 20, 2025
While I like this idea, it is very hard to implement with full static analysis. Or at least, I am not smart enough to understand how to correctly designate the change from, e.g. a
mixedto anon-empty-stringcorrectly. Simply setting the@return non-empty-stringwill cause all kinds of internal oddities in my experience.All of which is to say, I am currently setting up a v2.0 branch, and I like this idea, but I need help implementing.
frankdekker commentedon Oct 21, 2025
@shadowhand In the meantime we setup our own assert class, and so far we didn't run into much issues with phpstan. For ideas you can maybe take a look at our notation: https://github.com/123inkt/utils/blob/master/src/Assert.php#L500
kkevindev commentedon Nov 16, 2025
For the meantime, I've created a simple package where it just simply returns the validated value.
https://github.com/kkevindev/assert-return-value /
composer require kkevindev/assert-return-valueThis package will be obsoleted when this issue is resolved & a version is tagged.
shadowhand commentedon Nov 16, 2025
The current master branch is for version 2.0, which is unreleased and accepting BC breaking changes. Feel free to submit a PR with these changes.
kkevindev commentedon Nov 17, 2025
@shadowhand
Will do, what would be your ideal minimum accepted PHP version? Do you want a proper native PHP approach (where possible) which would require I think PHP 8.0 (for union types) and enrich it with docblock
@returnor do you only want to use@returnto keep the PHP 7.2 supported?Do you want to keep supporting PHP 7 or do we want to "force" users to update their EOL PHP versions to the 'Active support' / 'Security fixes only' versions (currently 8.1, 8.2, 8.3, 8.4).
shadowhand commentedon Nov 17, 2025
@kkevindev the minimum PHP version has already been bumped to PHP 8.2 for v2.