Completed
Push — master ( d0a237...a1b9c8 )
by Freek
9s
created

BladeX::component()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.4106
c 0
b 0
f 0
cc 7
nc 7
nop 2
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Facades\View;
7
use Spatie\BladeX\ComponentDirectory\NamespacedDirectory;
8
use Spatie\BladeX\ComponentDirectory\RegularDirectory;
9
use Symfony\Component\Finder\SplFileInfo;
10
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
11
12
class BladeX
13
{
14
    /** @var array */
15
    public $registeredComponents = [];
16
17
    /** @var string */
18
    protected $prefix = '';
19
20
    /**
21
     * @param string|array $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_array($view)) {
29
            foreach ($view as $singleView) {
30
                $this->component($singleView);
31
            }
32
33
            return null;
34
        }
35
36
        if ($view instanceof Component) {
37
            $this->registeredComponents[$view->tag] = $view;
38
39
            return $view;
40
        }
41
42
        if (!is_string($view)) {
43
            throw CouldNotRegisterComponent::invalidArgument();
44
        }
45
46
        if (ends_with($view, '*')) {
47
            $this->registerComponents($view);
48
49
            return null;
50
        }
51
52
        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...
53
            throw CouldNotRegisterComponent::viewNotFound($view, $tag);
54
        }
55
56
        $component = new Component($view, $tag);
57
58
        $this->registeredComponents[$component->tag] = $component;
59
60
        return $component;
61
    }
62
63
    public function registeredComponents(): array
64
    {
65
        return array_values($this->registeredComponents);
66
    }
67
68
    public function prefix(string $prefix = ''): self
69
    {
70
        $this->prefix = $prefix;
71
72
        return $this;
73
    }
74
75
    public function getPrefix(): string
76
    {
77
        return empty($this->prefix) ? '' : str_finish($this->prefix, '-');
78
    }
79
80
    public function registerComponents(string $viewDirectory)
81
    {
82
        $componentDirectory = str_contains($viewDirectory, '::')
83
            ? new NamespacedDirectory($viewDirectory)
84
            : new RegularDirectory($viewDirectory);
85
86
        collect(File::files($componentDirectory->getAbsoluteDirectory()))
87
            ->filter(function (SplFileInfo $file) {
88
                return ends_with($file->getFilename(), '.blade.php');
89
            })
90
            ->map(function (SplFileInfo $file) use ($componentDirectory) {
91
                return $componentDirectory->getViewName($file);
92
            })
93
            ->each(function (string $viewName) {
94
                $this->component($viewName);
95
            });
96
    }
97
}
98