✅
Phecks
  • User Guide
    • Getting Started
    • Configuration
    • Command-line interface
    • The Baseline
    • Continuous Integration (CI)
  • About phecks
    • The idea behind it
    • What is a check?
  • Writting checks
    • Making a Check
    • Check Sources
    • Pipes
  • Examples
    • Check inspirations
    • Spatie checks
  • Advanced
    • Making sources
    • Making Pipes
    • Formatters
Powered by GitBook
On this page
  1. Advanced

Making sources

It can be possible that you need to create your own source because what you need is not provided by Phecks.

Lucily, it's very easy to make them.

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

New Source

To make a source, make sure to implement the Source interface:

use Juampi92\Phecks\Domain\Contracts\Source;

It will make you implement the run method.

Also, you'll have to define the type of items this Source will return. You do that by using @implements Source<YourMatchType>

/**
 * @implements Source<FileMatch>
 */
class FileSource implements Source
{
    private ?string $dir = null;

    public function __construct(
        private readonly Filesystem $filesystem
    ) {}

    public function directory(string $dir): self
    {
        $this->dir = $dir;

        return $this;
    }

    /**
     * @return MatchCollection<FileMatch>
     */
    public function run(): MatchCollection
    {
        if (!$this->dir) {
            throw new RuntimeException('Please specify a directory using directory(string)');
        }

        /** @var array<SplFileInfo> */
        $files = $this->filesystem->allFiles($this->dir);

        return MatchCollection::fromFiles(
            collect($files)
                ->map(fn (SplFileInfo $fileInfo): FileMatch => new FileMatch($fileInfo->getPathname()))
                ->all(),
        );
    }
}

You can use MatchCollection@fromFiles if you know you will feed an array of FileMatch .

PreviousSpatie checksNextMaking Pipes

Last updated 2 years ago