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

src/BladeX.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\CouldNotRegisterBladeXComponent;
9
10
class BladeX
11
{
12
    /** @var array */
13
    public $registeredComponents = [];
14
15
    /** @var string */
16
    protected $prefix = '';
17
18
    /**
19
     * @param string|\Spatie\BladeX\BladeXComponent $bladeViewName
20
     * @param string $bladeXComponentName
21
     *
22
     * @return \Spatie\BladeX\BladeXComponent
23
     */
24
    public function component($bladeViewName, string $bladeXComponentName = ''): BladeXComponent
25
    {
26
        $newBladeXComponent = new BladeXComponent($bladeViewName, $bladeXComponentName);
0 ignored issues
show
It seems like $bladeViewName defined by parameter $bladeViewName on line 24 can also be of type object<Spatie\BladeX\BladeXComponent>; however, Spatie\BladeX\BladeXComponent::__construct() 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...
27
28
        $this->registeredComponents[$newBladeXComponent->name] = $newBladeXComponent;
29
30
        return $newBladeXComponent;
31
    }
32
33
    public function getRegisteredComponents(): array
34
    {
35
        return array_values($this->registeredComponents);
36
    }
37
38
    /**
39
     * @param string|array $directory
40
     */
41
    public function components($directory)
42
    {
43
        if (is_string($directory)) {
44
            $directory = [$directory];
45
        }
46
47
        if (! is_array($directory)) {
48
            throw CouldNotRegisterBladeXComponent::invalidArgument();
0 ignored issues
show
The method invalidArgument() does not seem to exist on object<Spatie\BladeX\Exc...egisterBladeXComponent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        }
50
51
        collect($directory)->each(function (string $directory) {
52
            $this->registerComponents($directory);
53
        });
54
    }
55
56
    protected function registerComponents(string $directory)
57
    {
58
        if (! File::isDirectory($directory)) {
59
            throw CouldNotRegisterBladeXComponent::componentDirectoryNotFound($directory);
60
        }
61
62
        collect(File::allFiles($directory))
63
            ->filter(function (SplFileInfo $file) {
64
                return ends_with($file->getFilename(), '.blade.php');
65
            })
66
            ->each(function (SplFileInfo $fileInfo) {
67
                $viewName = $this->getViewName($fileInfo->getPathname());
68
69
                $componentName = str_replace_last('.blade.php', '', $fileInfo->getFilename());
70
71
                $componentName = kebab_case($componentName);
72
73
                $this->component($viewName, $componentName);
74
            });
75
    }
76
77
    public function prefix(string $prefix = ''): self
78
    {
79
        $this->prefix = $prefix;
80
81
        return $this;
82
    }
83
84
    public function getPrefix(): string
85
    {
86
        return empty($this->prefix) ? '' : str_finish($this->prefix, '-');
87
    }
88
89
    protected function getViewName(string $pathName): string
90
    {
91
        $viewPaths = collect(View::getFinder()->getPaths())
92
            ->map(function (string $registeredViewPath) {
93
                return realpath($registeredViewPath);
94
            })
95
            ->filter()
96
            ->toArray();
97
98
        foreach ($viewPaths as $viewPath) {
99
            $pathName = str_replace($viewPath.'/', '', $pathName);
100
        }
101
102
        $viewName = str_replace_last('.blade.php', '', $pathName);
103
104
        return $viewName;
105
    }
106
}
107