Completed
Push — master ( c92e65...c6e439 )
by Freek
01:30 queued 10s
created

BladeX::getComponent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Spatie\BladeX\Exceptions\InvalidComponent;
6
7
class BladeX
8
{
9
    /** @var array */
10
    public $registeredComponents = [];
11
12
    public function component(string $componentName, string $classOrView)
13
    {
14
        $component = $this->getComponent($classOrView);
15
16
        if (! $component) {
17
            throw InvalidComponent::notFound($componentName, $classOrView);
18
        }
19
20
        $this->registeredComponents[$componentName] = $component;
21
    }
22
23
    public function components()
24
    {
25
26
    }
27
28
    protected function getComponent(string $classOrView): ?object
29
    {
30
        if (class_exists($classOrView)) {
31
            return app($classOrView);
32
        }
33
34
        if (view()->exists($classOrView)) {
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
            return new BladeViewComponent(view($classOrView));
0 ignored issues
show
Bug introduced by
It seems like view($classOrView) targeting view() can also be of type object<Illuminate\Contracts\View\Factory>; however, Spatie\BladeX\BladeViewComponent::__construct() does only seem to accept object<Illuminate\View\View>, maybe add an additional type check?

This check looks at variables that 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...
36
        }
37
38
        return null;
39
    }
40
}
41