Completed
Push — master ( 783cc3...112ad4 )
by
unknown
04:56 queued 03:47
created

BladeX::components()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\File;
7
use Symfony\Component\Finder\SplFileInfo;
8
use Spatie\BladeX\ComponentDirectory\RegularDirectory;
9
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
10
use Spatie\BladeX\ComponentDirectory\NamespacedDirectory;
11
12
class BladeX
13
{
14
    /** @var array */
15
    public $registeredComponents = [];
16
17
    /** @var string */
18
    protected $prefix = '';
19
20
    /**
21
     * @param string|string[] $view
22
     * @param string $tag
23
     *
24
     * @return null|\Spatie\BladeX\Component
25
     */
26
    public function component($view, string $tag = ''): ?Component
27
    {
28
        if (is_iterable($view)) {
29
            $this->registerViews($view);
0 ignored issues
show
Bug introduced by
It seems like $view defined by parameter $view on line 26 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...
30
31
            return null;
32
        }
33
34
        if ($view instanceof Component) {
35
            $this->registeredComponents[] = $view;
36
37
            return $view;
38
        }
39
40
        if (! is_string($view)) {
41
            throw CouldNotRegisterComponent::invalidArgument();
42
        }
43
44
        if (Str::endsWith($view, '*')) {
45
            $this->registerComponents($view);
46
47
            return null;
48
        }
49
50
        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...
51
            throw CouldNotRegisterComponent::viewNotFound($view, $tag);
52
        }
53
54
        $component = new Component($view, $tag);
55
56
        $this->registeredComponents[] = $component;
57
58
        return $component;
59
    }
60
61
    /**
62
     * @param string|string[] $viewDirectory
63
     *
64
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
65
     */
66
    public function components($viewDirectory): ComponentCollection
67
    {
68
        if (is_iterable($viewDirectory)) {
69
            $components = new ComponentCollection();
70
71
            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...
72
                if (Str::endsWith($singleViewDirectory, '*')) {
73
                    $components = $components->merge($this->registerComponents($singleViewDirectory));
74
                } else {
75
                    $components->push($this->component($singleViewDirectory));
76
                }
77
            }
78
79
            return $components;
80
        }
81
82
        return $this->registerComponents($viewDirectory);
0 ignored issues
show
Bug introduced by
It seems like $viewDirectory defined by parameter $viewDirectory on line 66 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...
83
    }
84
85
    /**
86
     * @return \Spatie\BladeX\Component[]
87
     */
88
    public function registeredComponents(): array
89
    {
90
        return collect($this->registeredComponents)->reverse()->unique(function (Component $component) {
91
            return $component->getTag();
92
        })->reverse()->values()->all();
93
    }
94
95
    public function prefix(string $prefix = ''): self
96
    {
97
        $this->prefix = $prefix;
98
99
        return $this;
100
    }
101
102
    public function getPrefix(): string
103
    {
104
        return empty($this->prefix) ? '' : Str::finish($this->prefix, '-');
105
    }
106
107
    /**
108
     * @internal
109
     *
110
     * @param string $viewDirectory
111
     *
112
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
113
     */
114
    public function registerComponents(string $viewDirectory): ComponentCollection
115
    {
116
        if (! Str::endsWith($viewDirectory, '*')) {
117
            throw CouldNotRegisterComponent::viewDirectoryWithoutWildcard($viewDirectory);
118
        }
119
120
        $componentDirectory = Str::contains($viewDirectory, '::')
121
            ? new NamespacedDirectory($viewDirectory)
122
            : new RegularDirectory($viewDirectory);
123
124
        return $this->registerViews(
125
            ComponentCollection::make(File::files($componentDirectory->getAbsoluteDirectory()))
126
                ->filter(function (SplFileInfo $file) {
127
                    return Str::endsWith($file->getFilename(), '.blade.php');
128
                })
129
                ->map(function (SplFileInfo $file) use ($componentDirectory) {
130
                    return $componentDirectory->getViewName($file);
131
                })
132
        );
133
    }
134
135
    /**
136
     * @param iterable|string[] $views
137
     *
138
     * @return \Spatie\BladeX\ComponentCollection|\Spatie\BladeX\Component[]
139
     */
140
    protected function registerViews(iterable $views): ComponentCollection
141
    {
142
        return ComponentCollection::make($views)->map(function (string $viewName) {
143
            return $this->component($viewName);
144
        });
145
    }
146
}
147