Completed
Push — master ( 24f568...fe17c6 )
by Freek
01:36
created

BladeX::registerComponents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use InvalidArgumentException;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Facades\View;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent;
10
11
class BladeX
12
{
13
    /** @var array */
14
    public $registeredComponents = [];
15
16
    /** @var string */
17
    protected $prefix = '';
18
19
    public function component(string $bladeViewName, string $bladeXComponentName = null): BladeXComponent
20
    {
21
        $bladeViewName = str_replace('.', '/', $bladeViewName);
22
23
        if (is_null($bladeXComponentName)) {
24
            $baseComponentName = explode('/', $bladeViewName);
25
26
            $bladeXComponentName = kebab_case(end($baseComponentName));
27
        }
28
29
        if (! view()->exists($bladeViewName)) {
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...
30
            throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $bladeXComponentName);
31
        }
32
33
        $newBladeXComponent = new BladeXComponent($bladeXComponentName, $bladeViewName);
34
35
        $this->registeredComponents[$newBladeXComponent->name] = $newBladeXComponent;
36
37
        return $newBladeXComponent;
38
    }
39
40
    public function getRegisteredComponents(): array
41
    {
42
        return array_values($this->registeredComponents);
43
    }
44
45
    public function components($directory)
46
    {
47
        if (is_string($directory)) {
48
            $directory = [$directory];
49
        }
50
51
        if (! is_array($directory)) {
52
            throw new InvalidArgumentException();
53
        }
54
55
        collect($directory)->each(function ($directory) {
56
            $this->registerComponents($directory);
57
        });
58
    }
59
60
    protected function registerComponents(string $directory)
61
    {
62
        if (! File::isDirectory($directory)) {
63
            throw CouldNotRegisterBladeXComponent::componentDirectoryNotFound($directory);
64
        }
65
66
        collect(File::allFiles($directory))
67
            ->filter(function (SplFileInfo $file) {
68
                return ends_with($file->getFilename(), '.blade.php');
69
            })
70
            ->each(function (SplFileInfo $fileInfo) {
71
                $viewName = $this->getViewName($fileInfo->getPathname());
72
73
                $componentName = str_replace_last('.blade.php', '', $fileInfo->getFilename());
74
75
                $componentName = kebab_case($componentName);
76
77
                $this->component($viewName, $componentName);
78
            });
79
    }
80
81
    public function prefix(string $prefix = ''): self
82
    {
83
        $this->prefix = $prefix;
84
85
        return $this;
86
    }
87
88
    public function getPrefix(): string
89
    {
90
        return empty($this->prefix) ? '' : str_finish($this->prefix, '-');
91
    }
92
93
    protected function getViewName(string $pathName): string
94
    {
95
        foreach (View::getFinder()->getPaths() as $registeredViewPath) {
96
            $pathName = str_replace(realpath($registeredViewPath).'/', '', $pathName);
97
        }
98
99
        $viewName = str_replace_last('.blade.php', '', $pathName);
100
101
        return $viewName;
102
    }
103
}
104