Skip to content

all*() methods and ::isIterable() assertion #337

@jmfeurprier

Description

@jmfeurprier

Since version 2, methods like allString(iterable $value, string $message = ''): iterable are more strongly typed (param and return types), which is great.

I do understand and approve why $message is now typed, but typing the input $value introduces a new behavior: $value now has to really be of type iterable before calling the assertion method (PHPStan reports the issue, and PHP could fail at runtime if $value is not an iterable type).

This requires adding Assert::isIterable(...) before every code using all*() assertion method.

What surprises me too is the first line of this same method:

    public static function allString(iterable $value, string $message = ''): iterable
    {
        static::isIterable($value); // Here

        foreach ($value as $entry) {
            static::string($entry, $message);
        }

        return $value;
    }

In that case, the assertion is redundant and should always evaluate to true.

Shouldn't $value remain of type mixed (implicitly) like in version 1?

Also, methods allNullOr...() seem to have the wrong type defined:

public static function allNullOrInteger(?iterable $value, string $message = ''): ?iterable { ... }

Which means, for $value. that it is either "an iterable variable OR a null variable", instead of the expected "an iterable variable filled with a mix of integer or null values".

Thanks for the great tool!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions