Completed
Push — master ( 83945e...947b9c )
by Freek
02:01
created

BladeXComponent::createClosureViewModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Closure;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Support\Str;
8
use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent;
9
10
class BladeXComponent
11
{
12
    /** @var string */
13
    public $bladeViewName;
14
15
    /** @var string */
16
    public $name;
17
18
    /** @var string */
19
    public $viewModel;
20
21
    public static function make(string $bladeViewName, string $name = '')
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        return new BladeXComponent($bladeViewName, $name = '');
24
    }
25
26
    public function __construct(string $bladeViewName, string $name = '')
27
    {
28
        $bladeViewName = str_replace('.', '/', $bladeViewName);
29
30
        if ($name === '') {
31
            $baseComponentName = explode('/', $bladeViewName);
32
33
            $name = kebab_case(end($baseComponentName));
34
        }
35
36
        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...
37
            throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $name);
38
        }
39
40
        $this->bladeViewName = $bladeViewName;
41
42
        $this->name = $name;
43
    }
44
45
    /**
46
     * @param string|\Closure $viewModel
47
     *
48
     * @return $this
49
     */
50
    public function viewModel($viewModel)
51
    {
52
        if (is_callable($viewModel)) {
53
            $this->viewModel = $this->createClosureViewModel($viewModel);
0 ignored issues
show
Bug introduced by
It seems like $viewModel defined by parameter $viewModel on line 50 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...
54
55
            return $this;
56
        }
57
58
        if (!class_exists($viewModel)) {
59
            throw CouldNotRegisterBladeXComponent::viewModelNotFound($this->name, $viewModel);
0 ignored issues
show
Bug introduced by
It seems like $viewModel defined by parameter $viewModel on line 50 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...
60
        }
61
62
        if (!is_a($viewModel, Arrayable::class, true)) {
63
            throw CouldNotRegisterBladeXComponent::viewModelNotArrayable($this->name, $viewModel);
0 ignored issues
show
Bug introduced by
It seems like $viewModel defined by parameter $viewModel on line 50 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...
64
        }
65
66
        $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...
67
68
        return $this;
69
    }
70
71
    protected function createClosureViewModel(Closure $closure): string
72
    {
73
        $viewModelClassName = 'bladex.viewModel.'.Str::uuid();
74
75
        app()->bind($viewModelClassName, function ($app, $arguments) use ($closure) {
76
            return (new ClosureViewModel($arguments ?? []))
77
                ->withClosure($closure);
78
        });
79
80
        return $viewModelClassName;
81
    }
82
}
83