BladeX::getPrefix()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Str;
6
use Spatie\BladeX\ComponentDirectory\NamespacedDirectory;
7
use Spatie\BladeX\ComponentDirectory\RegularDirectory;
8
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
9
use Symfony\Component\Finder\SplFileInfo;
10
11
class BladeX
12
{
13
    /** @var array */
14
    public $registeredComponents = [];
15
16
    /** @var string */
17
    protected $prefix = '';
18
19
    /**
20
     * @param string|string[] $view
21
     * @param string $tag
22
     *
23
     * @return null|\Spatie\BladeX\Component
24
     */
25
    public function component($view, string $tag = ''): ?Component
26
    {
27
        if (is_iterable($view)) {
28
            $this->registerViews($view);
0 ignored issues
show
Bug introduced by
It seems like $view defined by parameter $view on line 25 can also be of type string; however, Spatie\BladeX\BladeX::registerViews() does only seem to accept object<Spatie\BladeX\ite...>|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
29
30
            return null;
31
        }
32
33
        if ($view instanceof Component) {
34
            $this->registeredComponents[] = $view;
35
36
            return $view;
37
        }
38
39
        if (! is_string($view)) {
40
            throw CouldNotRegisterComponent::invalidArgument();
41
        }
42
43
        if (Str::endsWith($view, '*')) {
44
            $this->registerComponents($view);
45
46
            return null;
47
        }
48
49
        if (! view()->exists($view)) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
            throw CouldNotRegisterComponent::viewNotFound($view, $tag);
51
        }
52
53
        $component = new Component($view, $tag);
54
55
        $this->registeredComponents[] = $component;
56
57
        return $component;
58
    }
59
60
    /**
61
     * @param string|string[] $viewDirectory
62
     *
63
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
64
     */
65
    public function components($viewDirectory): ComponentCollection
66
    {
67
        if (is_iterable($viewDirectory)) {
68
            $components = new ComponentCollection();
69
70
            foreach ($viewDirectory as $singleViewDirectory) {
0 ignored issues
show
Bug introduced by
The expression $viewDirectory of type string|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
71
                if (Str::endsWith($singleViewDirectory, '*')) {
72
                    $components = $components->merge($this->registerComponents($singleViewDirectory));
73
                } else {
74
                    $components->push($this->component($singleViewDirectory));
75
                }
76
            }
77
78
            return $components;
79
        }
80
81
        return $this->registerComponents($viewDirectory);
0 ignored issues
show
Bug introduced by
It seems like $viewDirectory defined by parameter $viewDirectory on line 65 can also be of type array<integer,string>; however, Spatie\BladeX\BladeX::registerComponents() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82
    }
83
84
    /**
85
     * @return \Spatie\BladeX\Component[]
86
     */
87
    public function registeredComponents(): array
88
    {
89
        return collect($this->registeredComponents)->reverse()->unique(function (Component $component) {
90
            return $component->getTag();
91
        })->reverse()->values()->all();
92
    }
93
94
    public function prefix(string $prefix = ''): self
95
    {
96
        $this->prefix = $prefix;
97
98
        return $this;
99
    }
100
101
    public function getPrefix(): string
102
    {
103
        return empty($this->prefix) ? '' : Str::finish($this->prefix, '-');
104
    }
105
106
    /**
107
     * @internal
108
     *
109
     * @param string $viewDirectory
110
     *
111
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
112
     */
113
    public function registerComponents(string $viewDirectory)
114
    {
115
        if (! Str::endsWith($viewDirectory, '*')) {
116
            throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory);
117
        }
118
119
        $includeSubdirectories = Str::endsWith($viewDirectory, '**.*');
120
121
        $componentDirectory = Str::contains($viewDirectory, '::')
122
            ? new NamespacedDirectory($viewDirectory, $includeSubdirectories)
123
            : new RegularDirectory($viewDirectory, $includeSubdirectories);
124
125
        return $this->registerViews(
126
            ComponentCollection::make($componentDirectory->getFiles())
127
128
                ->filter(function (SplFileInfo $file) {
129
                    return Str::endsWith($file->getFilename(), '.blade.php');
130
                })
131
                ->map(function (SplFileInfo $file) use ($componentDirectory) {
132
                    return $componentDirectory->getViewName($file);
133
                })
134
        );
135
    }
136
137
    /**
138
     * @param iterable|string[] $views
139
     *
140
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
141
     */
142
    protected function registerViews(iterable $views): ComponentCollection
143
    {
144
        return ComponentCollection::make($views)->map(function (string $viewName) {
145
            return $this->component($viewName);
146
        });
147
    }
148
}
149