Pipes

Pipes are classes that given one input, they return a collection of outputs.

They are multi-purpose classes that are intended to be piped.

Usage

Extractors are defined to have a specific input type and a specific output type. They can be the same, or they can be different, but they will be predictable.

$fileSource
    ->run() // MatchCollection<FileMatch>
    ->pipe(new ClassnameExtractor()) // MatchCollection<class-string>
    ->pipe(new ReflectionClassExtractor()) // MatchCollection<ReflectionClass>
    ->pipe(new ReflectionMethodExtractor(ReflectionMethod::IS_PUBLIC) // MatchCollection<ReflectionMethod>

As you can see, the type of the MatchCollection changes in between extractions.

Also, the items on the collection also changes. Before the ReflectionMethodExtractor, the collection had only ReflectionClasses. After it, it has ReflectionMethod classes, one per public method per class.

If a class has multiple public methods, it will increase the output. If it has none, it will decrease the output.

Closure Pipe

You don't need a class to be able to pipe.

$fileSource
    ->run() // MatchCollection<FileMatch>
    ->pipe(fn (FileMatch $file) =>
        new Collection([ $this->transformFile($file) ])
    ) // MatchCollection<TransformedFile>

More specific filters

If you would like to simply reject or filter some matches, you can use filter and reject instead of making a pipe.

$fileSource
    ->run() // MatchCollection<ReflectionClass>
    ->pipe(new MethodExtractor(\ReflectionMethod::IS_PUBLIC)) // MatchCollection<ReflectionMethod>
    ->reject(fn (ReflectionMethod $method): bool =>
        $method->getDeclaringClass()->isTrait()
    ) // MatchCollection<ReflectionMethod> remove trait methods.

Last updated