Skip to content

FilteredArbitrary ​

Rasuvaeff\PropertyTesting\Arbitrary\FilteredArbitrary

Class — Package: property-testing-core — Source — Version: v1.0.0-1-g88fdee2

Implements: Enumerable, ArbitraryInterface

Type parameters:

  • TInner of mixed = mixed

Generates values from a delegate arbitrary, retrying until a predicate holds.

Filtering is bounded: after Arbitrary\FilteredArbitrary::MAX_ATTEMPTS consecutive rejections the generator throws GenerationExhaustedException rather than yield a value that fails the predicate — a property never receives an out-of-domain input. Use Assume::that() inside the property when the rejection rate is high, which skips discarded runs cleanly, or Gen::flatMap() to construct dependent values without filtering at all.

Shrinking walks the inner value's tree, keeping only branches whose value satisfies the predicate (a rejected candidate's subtree is pruned with it).

Constructor ​

php
__construct(
    \ArbitraryInterface<\Arbitrary\TInner> $inner,
    callable $predicate,
)
ParameterTypeDefaultDescription
$inner\ArbitraryInterface<\Arbitrary\TInner>required
$predicatecallablerequired

Methods ​

generate() ​

php
generate(Random $random): Shrinkable

Produce one random value from this arbitrary's space, together with its shrink tree. Candidates must be ordered most aggressive first (typically toward a zero/empty/identity element) and every branch of the tree must be finite, so shrinking terminates.

Documentation inherited from ArbitraryInterface.

domainSize() ​

php
domainSize(): ?int

The source's size — an upper bound, since the predicate is only applied while walking.

enumerate() ​

php
enumerate(): iterable

Every value of the domain, once each, as a shrinkable node with the same tree generate() would give it, in a fixed order that does not depend on any seed. Only meaningful when domainSize() is not null; an implementation may throw otherwise.

Documentation inherited from Enumerable.

Throws:

  • LogicException — When the source has no finite domain (\Rasuvaeff\PropertyTesting\Arbitrary\domainSize() is null).