Completed
Push — master ( 7a1697...ce1f82 )
by
unknown
01:13
created

BladeX::getPrefix()   A

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\Facades\File;
6
use Illuminate\Support\Facades\View;
7
use Symfony\Component\Finder\SplFileInfo;
8
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
9
10
class BladeX
11
{
12
    /** @var array */
13
    public $registeredComponents = [];
14
15
    /** @var string */
16
    protected $prefix = '';
17
18
    public function component(string $bladeViewName, string $bladeXComponentName = null)
19
    {
20
        $bladeViewName = str_replace('.', '/', $bladeViewName);
21
22
        if (is_null($bladeXComponentName)) {
23
            $baseComponentName = explode('/', $bladeViewName);
24
25
            $bladeXComponentName = kebab_case(end($baseComponentName));
26
        }
27
28
        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...
29
            throw CouldNotRegisterComponent::viewNotFound($bladeViewName, $bladeXComponentName);
30
        }
31
32
        $this->registeredComponents[] = new BladeXComponent($bladeXComponentName, $bladeViewName);
33
    }
34
35
    public function getRegisteredComponents(): array
36
    {
37
        return $this->registeredComponents;
38
    }
39
40
    public function components(string $directory)
41
    {
42
        if (! File::isDirectory($directory)) {
43
            throw CouldNotRegisterComponent::componentDirectoryNotFound($directory);
44
        }
45
46
        collect(File::allFiles($directory))
47
            ->filter(function (SplFileInfo $file) {
48
                return ends_with($file->getFilename(), '.blade.php');
49
            })
50
            ->each(function (SplFileInfo $fileInfo) {
51
                $viewName = $this->getViewName($fileInfo->getPathname());
52
53
                $componentName = str_replace_last('.blade.php', '', $fileInfo->getFilename());
54
55
                $componentName = kebab_case($componentName);
56
57
                $this->component($viewName, $componentName);
58
            });
59
    }
60
61
    public function prefix(string $prefix = ''): self
62
    {
63
        $this->prefix = $prefix;
64
65
        return $this;
66
    }
67
68
    public function getPrefix(): string
69
    {
70
        return empty($this->prefix) ? '' : str_finish($this->prefix, '-');
71
    }
72
73
    protected function getViewName(string $pathName): string
74
    {
75
        foreach (View::getFinder()->getPaths() as $registeredViewPath) {
76
            $pathName = str_replace(realpath($registeredViewPath).'/', '', $pathName);
77
        }
78
79
        $viewName = str_replace_last('.blade.php', '', $pathName);
80
81
        return $viewName;
82
    }
83
}
84