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

src/BladeXComponent.php (6 issues)

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 = '')
0 ignored issues
show
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...
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)) {
0 ignored issues
show
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...
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;
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