Passed
Push — master ( ada723...49af47 )
by Caen
03:13 queued 12s
created

FileCollection::getMediaFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Kernel;
6
7
use Hyde\Foundation\Concerns\BaseFoundationCollection;
8
use Hyde\Framework\Services\DiscoveryService;
9
use Hyde\Pages\Concerns\HydePage;
10
use Hyde\Support\Filesystem\MediaFile;
11
use Hyde\Support\Filesystem\ProjectFile;
12
use Hyde\Support\Filesystem\SourceFile;
13
14
/**
15
 * The FileCollection contains all the discovered source and media files,
16
 * and thus has an integral role in the Hyde Auto Discovery process.
17
 *
18
 * This class is stored as a singleton in the HydeKernel.
19
 * You would commonly access it via one of the facades:
20
 *
21
 * @see \Hyde\Foundation\Facades\FileCollection
22
 * @see \Hyde\Hyde::files()
23
 */
24
final class FileCollection extends BaseFoundationCollection
25
{
26
    /**
27
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>|null  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>|null.
Loading history...
28
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
29
     */
30
    public function getSourceFiles(?string $pageClass = null): self
31
    {
32
        return ! $pageClass ? $this->getAllSourceFiles() : $this->getSourceFilesFor($pageClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $pageClass ? $t...rceFilesFor($pageClass) returns the type Hyde\Foundation\Kernel\FileCollection which is incompatible with the documented return type Hyde\Foundation\FileCollection.
Loading history...
33
    }
34
35
    /**
36
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
37
     * @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile>
38
     */
39
    public function getSourceFilesFor(string $pageClass): self
40
    {
41
        return $this->getAllSourceFiles()->where(fn (SourceFile $file): bool => $file->model === $pageClass);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAllSour...ion(...) { /* ... */ }) returns the type Hyde\Foundation\Kernel\FileCollection which is incompatible with the documented return type Hyde\Foundation\FileCollection.
Loading history...
42
    }
43
44
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\SourceFile> */
0 ignored issues
show
Bug introduced by
The type Hyde\Foundation\FileCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
    public function getAllSourceFiles(): self
46
    {
47
        return $this->where(fn (ProjectFile $file): bool => $file instanceof SourceFile);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->where(function(...) { /* ... */ }) returns the type Hyde\Foundation\Kernel\FileCollection which is incompatible with the documented return type Hyde\Foundation\FileCollection.
Loading history...
48
    }
49
50
    /** @return \Hyde\Foundation\FileCollection<\Hyde\Support\Filesystem\MediaFile> */
51
    public function getMediaFiles(): self
52
    {
53
        return $this->where(fn (ProjectFile $file): bool => $file instanceof MediaFile);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->where(function(...) { /* ... */ }) returns the type Hyde\Foundation\Kernel\FileCollection which is incompatible with the documented return type Hyde\Foundation\FileCollection.
Loading history...
54
    }
55
56
    protected function runDiscovery(): self
57
    {
58
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
59
            $this->discoverFilesFor($pageClass);
60
        }
61
62
        /** @var class-string<\Hyde\Foundation\Concerns\HydeExtension> $extension */
63
        foreach ($this->kernel->getRegisteredExtensions() as $extension) {
64
            $extension::discoverFiles($this);
65
        }
66
67
        $this->discoverMediaAssetFiles();
68
69
        return $this;
70
    }
71
72
    /** @param class-string<HydePage> $pageClass */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<HydePage>.
Loading history...
73
    protected function discoverFilesFor(string $pageClass): void
74
    {
75
        // Scan the source directory, and directories therein, for files that match the model's file extension.
76
        foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $filepath) {
77
            if (! str_starts_with(basename((string) $filepath), '_')) {
78
                $this->put($this->kernel->pathToRelative($filepath), SourceFile::make($filepath, $pageClass));
79
            }
80
        }
81
    }
82
83
    protected function discoverMediaAssetFiles(): void
84
    {
85
        foreach (DiscoveryService::getMediaAssetFiles() as $filepath) {
86
            $this->put($this->kernel->pathToRelative($filepath), MediaFile::make($filepath));
87
        }
88
    }
89
}
90