Issues (28)

src/Nova/ResourceTemplates/TemplateFinder.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace NovaThinKit\Nova\ResourceTemplates;
4
5
use Laravel\Nova\Resource;
6
7
class TemplateFinder
8
{
9
    public static array $templatesMap = [];
10
11 45
    public static function templatesMap(string $modelClass, array $map = null, $merge = true)
12
    {
13 45
        if (is_array($map)) {
14 45
            collect($map)->each(function ($templateClass, $templateName) {
0 ignored issues
show
$map 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

14
            collect(/** @scrutinizer ignore-type */ $map)->each(function ($templateClass, $templateName) {
Loading history...
15 45
                if (!is_string($templateName)) {
16
                    throw new \Exception('Template name should be string');
17
                }
18 45
                if (!is_a($templateClass, ResourceTemplate::class, true)) {
19
                    throw new \Exception('Template class should extends ResourceTemplate');
20
                }
21 45
            });
22 45
            static::$templatesMap[$modelClass] = $merge && (static::$templatesMap[$modelClass] ?? false)
23 45
                ? $map + static::$templatesMap[$modelClass] : $map;
24
        }
25
26 45
        return static::$templatesMap[$modelClass] ?? [];
27
    }
28
29 7
    public static function templatesNames(string $modelClass): array
30
    {
31 7
        return collect(static::templatesMap($modelClass))
0 ignored issues
show
It seems like static::templatesMap($modelClass) can also be of type array; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        return collect(/** @scrutinizer ignore-type */ static::templatesMap($modelClass))
Loading history...
32 7
            ->mapWithKeys(function ($templateClass, $templateName) {
33 7
                return [$templateName => $templateClass::name()];
34 7
            })->all();
35
    }
36
37
38 2
    public static function find(string $template, Resource $resource): ?ResourceTemplate
39
    {
40 2
        $class = static::templatesMap($resource::$model)[$template] ?? null;
41 2
        if ($class
42 2
            && is_string($class)
43 2
            && is_a($class, ResourceTemplate::class, true)) {
44 1
            return new $class($resource);
45
        }
46
47 1
        return null;
48
    }
49
}
50