Completed
Push — master ( e965eb...462290 )
by Freek
01:23
created

BladeXComponent::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent;
7
8
class BladeXComponent
9
{
10
    /** @var string */
11
    public $bladeViewName;
12
13
    /** @var string */
14
    public $name;
15
16
    /** @var string */
17
    public $viewModelClass;
18
19
    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...
20
    {
21
        return new BladeXComponent($bladeViewName, $name = '');
22
    }
23
24
    public function __construct(string $bladeViewName, string $name = '')
25
    {
26
        $bladeViewName = str_replace('.', '/', $bladeViewName);
27
28
        if ($name === '') {
29
            $baseComponentName = explode('/', $bladeViewName);
30
31
            $name = kebab_case(end($baseComponentName));
32
        }
33
34
        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...
35
            throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $name);
36
        }
37
38
        $this->bladeViewName = $bladeViewName;
39
40
        $this->name = $name;
41
    }
42
43
    public function viewModel(string $viewModelClass)
44
    {
45
        if (! class_exists($viewModelClass)) {
46
            throw CouldNotRegisterBladeXComponent::viewModelNotFound($this->name, $viewModelClass);
47
        }
48
49
        if (! is_a($viewModelClass, Arrayable::class, true)) {
50
            throw CouldNotRegisterBladeXComponent::viewModelNotArrayable($this->name, $viewModelClass);
51
        }
52
53
        $this->viewModelClass = $viewModelClass;
54
55
        return $this;
56
    }
57
}
58