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

src/BladeXComponent.php (3 issues)

Check for mismatching type of a variable passed in as a parameter

Bug Major

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 Closure;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent;
8
9
class BladeXComponent
10
{
11
    /** @var string */
12
    public $bladeViewName;
13
14
    /** @var string */
15
    public $name;
16
17
    /** @var string */
18
    public $viewModel;
19
20
    public static function make(string $bladeViewName, string $name = '')
21
    {
22
        return new BladeXComponent($bladeViewName, $name = '');
23
    }
24
25
    public function __construct(string $bladeViewName, string $name = '')
26
    {
27
        $bladeViewName = str_replace('.', '/', $bladeViewName);
28
29
        if ($name === '') {
30
            $baseComponentName = explode('/', $bladeViewName);
31
32
            $name = kebab_case(end($baseComponentName));
33
        }
34
35
        if (! view()->exists($bladeViewName)) {
36
            throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $name);
37
        }
38
39
        $this->bladeViewName = $bladeViewName;
40
41
        $this->name = $name;
42
    }
43
44
    /**
45
     * @param string|\Closure $viewModel
46
     *
47
     * @return $this
48
     */
49
    public function viewModel($viewModel)
50
    {
51
        if (is_callable($viewModel)) {
52
            $this->viewModel = $this->createClosureViewModel($viewModel);
0 ignored issues
show
It seems like $viewModel defined by parameter $viewModel on line 49 can also be of type string; however, Spatie\BladeX\BladeXComp...reateClosureViewModel() does only seem to accept object<Closure>, 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...
53
54
            return $this;
55
        }
56
57
        if (! class_exists($viewModel)) {
58
            throw CouldNotRegisterBladeXComponent::viewModelNotFound($this->name, $viewModel);
0 ignored issues
show
It seems like $viewModel defined by parameter $viewModel on line 49 can also be of type object<Closure>; however, Spatie\BladeX\Exceptions...nt::viewModelNotFound() 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...
59
        }
60
61
        if (! is_a($viewModel, Arrayable::class, true)) {
62
            throw CouldNotRegisterBladeXComponent::viewModelNotArrayable($this->name, $viewModel);
0 ignored issues
show
It seems like $viewModel defined by parameter $viewModel on line 49 can also be of type object<Closure>; however, Spatie\BladeX\Exceptions...viewModelNotArrayable() 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...
63
        }
64
65
        $this->viewModel = $viewModel;
66
67
        return $this;
68
    }
69
70
    protected function createClosureViewModel(Closure $closure): string
71
    {
72
        static $viewModelCounter = 0;
73
74
        $viewModelClassName = 'bladex.viewModel.'.$viewModelCounter++;
75
76
        app()->bind($viewModelClassName, function ($app, $arguments) use ($closure) {
77
            return (new ClosureViewModel($arguments ?? []))
78
                ->withClosure($closure);
79
        });
80
81
        return $viewModelClassName;
82
    }
83
}
84