Skip to content

Add return $value for all Assert assertions. - #324

Merged
shadowhand merged 8 commits into
webmozarts:masterfrom
kkevindev:feat-add-return-types
Nov 21, 2025
Merged

shadowhand merged 8 commits into
webmozarts:masterfrom
kkevindev:feat-add-return-types

Conversation

@kkevindev

@kkevindev kkevindev commented Nov 18, 2025 •

Copy link
Copy Markdown
Contributor

Added return statement for all Assert assertions.

Closes #280.

Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
@shadowhand

Copy link
Copy Markdown
Collaborator

BTW, use php bin/generate.php to update Mixin.php.

@kkevindev

Copy link
Copy Markdown
Contributor Author

BTW, use php bin/generate.php to update Mixin.php.

Thanks for the tip @shadowhand, updated PR accordingly.

@kkevindev
kkevindev marked this pull request as ready for review November 18, 2025 11:24
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
Comment thread src/Assert.php Resolved
@kkevindev

Copy link
Copy Markdown
Contributor Author

Ah, didn't the code style in the project for \ for is_string. But made the change @shadowhand.

@kkevindev
kkevindev requested a review from shadowhand November 18, 2025 13:06
@shadowhand

Copy link
Copy Markdown
Collaborator

@kkevindev if you rebase master the CI issues should be resolved.

@kkevindev

Copy link
Copy Markdown
Contributor Author

@kkevindev if you rebase master the CI issues should be resolved.

done! @shadowhand

@kkevindev

Copy link
Copy Markdown
Contributor Author

Looks like we still need the @psalm-* docblocks (see failing test) @shadowhand .

@shadowhand

Copy link
Copy Markdown
Collaborator

@kkevindev I think that's because Mixin needs to be regenerated?

@shadowhand shadowhand left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Everything is looking good, except for Mixin generation.

Comment thread bin/src/MixinGenerator.php Outdated

$staticFunction = $this->phpdoc($phpdocLines, $indent)."\n";
$staticFunction .= $indentation.'public static function '.$name.$this->functionParameters($parameters, $types, $defaults).": void\n"
$staticFunction .= $indentation.'public static function '.$name.$this->functionParameters($parameters, $types, $defaults)."\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This dropped the return type, without adding the newly added types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@shadowhand I just noticed alot of the @psalm-return annotations on the Mixin class weren't even correct. This is fixed now.

I got it to a point where the @return phpdoc return type contains the exact return type and native php method return type is the same as the parameter value, so can be mixed, see:

    public static function allString(iterable $value, string $message = ''): iterable
    public static function nullOrString(mixed $value, string $message = ''): mixed

I've looked into a way to get the native php return type but since we are guessing the return type based on the psalm phpdoc on the Assert class, we currently do not have a way to convert these non-native types non-empty-string|null (for nullOrStringNotEmpty) to be string|null. Any idea's?

I think for now this is acceptable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yup good enough. Appreciate all the work on this!

@kkevindev
kkevindev requested a review from shadowhand November 20, 2025 23:12
@shadowhand
shadowhand merged commit 193aad6 into webmozarts:master Nov 21, 2025
@shadowhand shadowhand added this to the v2.0 milestone Nov 21, 2025
@ruudk

ruudk commented Nov 21, 2025

Copy link
Copy Markdown
Contributor

Sorry for this off topic message: BUT I LOVE THIS 😁 👏 🙌 Thanks! Soon I can finally remove my custom AssertReturn class that did this by hand 🎉

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.

Return validated value

3 participants