✅
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
  • List of Sources
  • GrepSource
  • ClassSource
  • FileSource
  • ConfigSource
  • RouteCommandSource
  1. Writting checks

Check Sources

PreviousMaking a CheckNextPipes

Last updated 2 years ago

Each Check runs independently from each other.

This means that every Pheck is responsible for getting a "source" of files or "input", and then process each one of them to see if it can find a violation.

These Source classes are useful for extracting the input we want to analyze.

The `run()` method

All Sources implement a run method. It will return a MatchCollection.

A match collection is a collection of the pair value|FileMatch. This means that your collection will know the File (and sometimes line) and the context.

List of Sources

If you're interested in creating your own source, take a look at the .

GrepSource

Perhaps the most used one. It allows you to specify a pattern and files. This class is a wrapper around the

use Juampi92\Phecks\Domain\Sources\GrepSource;

class ResourcesMustImportOurLocalResourceCheck implements Check
{
    public function __construct(
        // You can inject the source in the Constructor since
        // Checks are instantiated by the container.
        private readonly GrepSource $source
    ) {}

    public function getMatches(): MatchCollection
    {
        return $this->source
            // Specify a folder (or folder pattern) to analyze.
            ->files('./app/Http/Resources')
            // Specify a grep regex to run against each file.
            ->pattern("use Illuminate\Http\Resources\Json\JsonResource;")
            // Add flags depending on the regex and results that you are looking for,
            ->addFlags([GrepFlags::IGNORE_CASE, GrepFlags::FIXED_STRINGS, GrepFlags::EXTENDED_REGEXP])
            // Run.
            ->run();
    }

ClassSource

Given a directory, it will return the Classes (with Full Qualifier Name) inside. It can be recursive.

use Juampi92\Phecks\Domain\Sources\ClassSource;

return resolve(ClassSource::class)
    ->directory('./app/Modules/*/Actions')
    ->recursive()
    ->run();

FileSource

Given a directory, it will return the Files inside. It can be recursive.

use Juampi92\Phecks\Domain\Sources\FileSource;

return resolve(FileSource::class)
    ->directory('./app/Modules/*/Actions')
    ->recursive()
    ->run();

ConfigSource

Outputs the entire configuration.

Note: the configuration is dotted.

use Juampi92\Phecks\Domain\Sources\ConfigSource;

return resolve(ConfigSource::class)->run();

This is the example of the MatchCollection to array.

[
    {"key": "app.locale", "value": "en-us"},
    ...
]

RouteCommandSource

Outputs the routes. It runs php artisan route:list --json behind the scenes.

use Juampi92\Phecks\Domain\Sources\RouteCommandSource;

return resolve(RouteCommandSource::class)->run();

This is the example of the MatchCollection to array.

[
    RouteInfo({
        +name: "api.documents.show",
        +uri: "api/documents/{documentId}"
    }),
    ...
]
advanced section
grep command.