Completed
Push — master ( d8b855...902648 )
by
unknown
01:27
created

BladeX::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use Illuminate\Support\Facades\File;
6
use Spatie\BladeX\Exceptions\CouldNotRegisterComponent;
7
use Symfony\Component\DomCrawler\Crawler;
8
9
class BladeX
10
{
11
    /** @var array */
12
    public $registeredComponents = [];
13
14
    public function component(string $componentName, string $classOrView)
15
    {
16
        $component = $this->getComponent($classOrView);
17
18
        if (! $component) {
19
            throw CouldNotRegisterComponent::componentNotFound($componentName, $classOrView);
20
        }
21
22
        $this->registeredComponents[$componentName] = $component;
23
    }
24
25
    public function components(string $directory)
26
    {
27
        if (! File::isDirectory($directory)) {
28
            throw CouldNotRegisterComponent::componentDirectoryNotFound($directory);
29
        }
30
31
        dd(File::allFiles($directory));
32
    }
33
34
    protected function getComponent(string $classOrView): ?object
35
    {
36
        if (class_exists($classOrView)) {
37
            return app($classOrView);
38
        }
39
40
        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...
41
            return new BladeViewComponent($classOrView);
42
        }
43
44
        return null;
45
    }
46
47
    public function compile(string $view): string
48
    {
49
        $crawler = new Crawler($view);
50
51
        foreach($this->registeredComponents as $componentName => $classOrView) {
52
            $crawler
53
                ->filter($componentName)
54
                ->each(function (Crawler $subCrawler) use ($classOrView) {
55
                    $node = $subCrawler->getNode(0);
56
57
                    $node->parentNode->replaceChild(
58
                        $node->ownerDocument->createTextNode("@include({$classOrView})"), // TEMP: @include everything
59
                        $node
60
                    );
61
                });
62
        }
63
64
        return $crawler->html();
65
    }
66
}
67