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

src/BladeXComponent.php (1 issue)

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);
53
54
            return $this;
55
        }
56
57
        if (! class_exists($viewModel)) {
58
            throw CouldNotRegisterBladeXComponent::viewModelNotFound($this->name, $viewModel);
59
        }
60
61
        if (! is_a($viewModel, Arrayable::class, true)) {
62
            throw CouldNotRegisterBladeXComponent::viewModelNotArrayable($this->name, $viewModel);
63
        }
64
65
        $this->viewModel = $viewModel;
0 ignored issues
show
Documentation Bug introduced by
It seems like $viewModel can also be of type object<Closure>. However, the property $viewModel is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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