# Check Sources

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 [advanced section](/phecks/advanced/making-sources.md).

### GrepSource

Perhaps the most used one. It allows you to specify a `pattern` and `files`. This class is a wrapper around the [`grep` command.](https://man7.org/linux/man-pages/man1/grep.1.html)

```php
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.

```php
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.

```php
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.

```php
use Juampi92\Phecks\Domain\Sources\ConfigSource;

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

This is the example of the MatchCollection to array.

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

### RouteCommandSource

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

```php
use Juampi92\Phecks\Domain\Sources\RouteCommandSource;

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

This is the example of the MatchCollection to array.

```php
[
    RouteInfo({
        +name: "api.documents.show",
        +uri: "api/documents/{documentId}"
    }),
    ...
]
```


---

# 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/writting-checks/check-sources.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.
