# 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:

```php
use Juampi92\Phecks\Domain\Contracts\Pipe;
```

It will make you implement the method:<br>

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

{% hint style="info" %}
It is recommended to use static analysis, with it's definitions:\
`@implements Pipe<TMatchInput, TMatchOutput>`
{% endhint %}

```php
/**
 * @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)

```php
/**
 * @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]);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://juampi92.gitbook.io/phecks/advanced/making-pipes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
