webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[RFC] Return values

mamazu opened this issue · comments

Currently if you want to use the result of a given assertion you have to do something like this:

public function returnValue(): int {
    $value = intOrNullFunction();
    Assert::integer($value);
    return $value;
}

What would be nicer is if the assertion on success would return the value:

public function returnValue(): int {
    return Assert::integer(intOrNullFunction());
}

What do you think?

I don't think other assertion libraries do this, nor do i think its the responsability of an assert library to return the values.

You could however extend the base Assert class, and make the methods return the values e.g.

public static function  integer($value): int
{
    parent::integer($value);
    return $value;
}

I don't think other assertion libraries do this

https://github.com/azjezz/psl returns the value from assert methods and I think it is very handy:

public function returnValue(): int {
    return Type\int()->assert(intOrNullFunction());
}

So I agree to @mamazu and would love to see the return values in this lib, too.

IMHO an assertion library needs to assert - return something doesn't seem to be its purpose

@BackEndTea's suggestion seems the most convenient case here if you really need this feature