Making Pipes

If you follow the recommended setup, you can allocate your Pipes in ./phecks/Pipes.

The first step is to implement the following interface:

use Juampi92\Phecks\Domain\Contracts\Pipe;

It will make you implement the method:

/**
 * @param TMatchInput $input
 * @return Collection<array-key, TMatchOutput>
 */
public function __invoke($input): Collection
{

It is recommended to use static analysis, with it's definitions: @implements Pipe<TMatchInput, TMatchOutput>

/**
 * @implements  Pipe<ReflectionClass, ReflectionClass>
 */
class InterfacesExtractor implements Pipe
{
    /**
     * @param ReflectionClass $input
     * @return Collection<array-key, ReflectionClass>
     */
    public function __invoke($input): Collection
    {
        // The returned collection can be empty (will remove the item),
        // or can have one or more items. All of the items will share
        // the same source file.
        return new Collection(
            $input->getInterfaces(),
        );
    }
}

Example of a "filter" pipe (keeps or removes an item, never adds or transforms)

/**
 * @implements Pipe<ReflectionClass, ReflectionClass>
 */
class WhereDoesNotExtendClassFilter implements Pipe
{
    /**
     * @param  class-string  $mustNotExtend
     */
    public function __construct(
        private readonly string $mustNotExtend,
    ) {}

    /**
     * @param ReflectionClass $input
     * @return Collection<array-key, ReflectionClass>
     */
    public function __invoke($input): Collection
    {
        if ($input->isSubclassOf($this->mustNotExtend)) {
            // Returning an empty collection removes the item.
            return new Collection();
        }

        return new Collection([$input]);
    }
}

Last updated