Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

ViewPublishGroup::publishableFilesMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Helpers;
6
7
use Hyde\Facades\Filesystem;
8
use Hyde\Foundation\Providers\ViewServiceProvider;
9
use Hyde\Hyde;
10
use Illuminate\Support\Str;
11
12
use function Hyde\path_join;
13
use function Hyde\unslash;
14
15
/**
16
 * @internal Helper object for publishable view groups.
17
 */
18
class ViewPublishGroup
19
{
20
    public readonly string $group;
21
22
    public readonly string $name;
23
    public readonly string $description;
24
25
    public readonly string $source;
26
    public readonly string $target;
27
28
    /** @var array<string> The filenames relative to the source directory. */
29
    public readonly array $files;
30
31
    /** @var class-string<\Hyde\Foundation\Providers\ViewServiceProvider> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Found...rs\ViewServiceProvider> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Foundation\Providers\ViewServiceProvider>.
Loading history...
32
    protected static string $provider = ViewServiceProvider::class;
33
34
    protected function __construct(string $group, string $source, string $target, array $files, ?string $name = null, ?string $description = null)
35
    {
36
        $this->group = $group;
0 ignored issues
show
Bug introduced by
The property group is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
37
        $this->source = $source;
0 ignored issues
show
Bug introduced by
The property source is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
38
        $this->target = $target;
0 ignored issues
show
Bug introduced by
The property target is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
39
        $this->files = $files;
0 ignored issues
show
Bug introduced by
The property files is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
40
41
        $this->name = $name ?? Hyde::makeTitle($group);
0 ignored issues
show
Bug introduced by
The property name is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
Bug introduced by
The method makeTitle() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->name = $name ?? Hyde::/** @scrutinizer ignore-call */ makeTitle($group);
Loading history...
42
        $this->description = $description ?? "Publish the '$group' files for customization.";
0 ignored issues
show
Bug introduced by
The property description is declared read-only in Hyde\Console\Helpers\ViewPublishGroup.
Loading history...
43
    }
44
45
    public static function fromGroup(string $group, ?string $name = null, ?string $description = null): static
46
    {
47
        [$source, $target] = static::keyedArrayToTuple(static::$provider::pathsToPublish(static::$provider, $group));
48
        [$source, $target] = [static::normalizePath($source), static::normalizePath($target)];
49
50
        $files = static::findFiles($source);
51
52
        return new static($group, $source, $target, $files, $name, $description);
53
    }
54
55
    /** @return array<string, string> The source file paths mapped to their target file paths. */
56
    public function publishableFilesMap(): array
57
    {
58
        return collect($this->files)->mapWithKeys(fn (string $file): array => [
0 ignored issues
show
Bug introduced by
$this->files of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        return collect(/** @scrutinizer ignore-type */ $this->files)->mapWithKeys(fn (string $file): array => [
Loading history...
59
            path_join($this->source, $file) => path_join($this->target, $file),
60
        ])->all();
61
    }
62
63
    /**
64
     * @param  array<string, string>  $array
65
     * @return list<string>
0 ignored issues
show
Bug introduced by
The type Hyde\Console\Helpers\list 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...
66
     */
67
    protected static function keyedArrayToTuple(array $array): array
68
    {
69
        return [key($array), current($array)];
70
    }
71
72
    /** @return array<string> */
73
    protected static function findFiles(string $source): array
74
    {
75
        return Filesystem::findFiles($source, recursive: true)
76
            ->map(fn (string $file) => static::normalizePath($file))
77
            ->map(fn (string $file) => unslash(Str::after($file, $source)))
78
            ->sort(fn (string $a, string $b): int => substr_count($a, '/') <=> substr_count($b, '/') ?: strcmp($a, $b))
79
            ->all();
80
    }
81
82
    protected static function normalizePath(string $path): string
83
    {
84
        return Hyde::pathToRelative(
0 ignored issues
show
Bug introduced by
The method pathToRelative() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        return Hyde::/** @scrutinizer ignore-call */ pathToRelative(
Loading history...
85
            Filesystem::exists($path) ? realpath($path) : $path
86
        );
87
    }
88
}
89