Completed
Push — master ( c424e3...5a04f7 )
by Freek
01:37
created

BladeXCompiler::__construct()   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 1
1
<?php
2
3
namespace Spatie\BladeX;
4
5
use SimpleXMLElement;
6
7
class BladeXCompiler
8
{
9
    /** @var \Spatie\BladeX\BladeX */
10
    protected $bladeX;
11
12
    public function __construct(BladeX $bladeX)
13
    {
14
        return $this->bladeX = $bladeX;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
15
    }
16
17
    public function compile(string $view): string
18
    {
19
        foreach ($this->bladeX->getRegisteredComponents() as $componentName => $classOrView) {
20
            $pattern = '/<\s*'.$componentName.'[^>]*>((.|\n)*?)<\s*\/\s*'.$componentName.'>/m';
21
22
            $view = preg_replace_callback($pattern, function ($result) use ($classOrView) {
23
                [$component, $contents] = $result;
0 ignored issues
show
Bug introduced by
The variable $component does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $contents seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
24
25
                $xml = new SimpleXMLElement($component);
26
27
                $data = '[';
28
29
                foreach ($xml->attributes() as $attribute => $value) {
30
                    $value = str_replace("'","\\'", $value);
31
                    $data .= "'{$attribute}' => '{$value}',";
32
                }
33
34
                $data .= ']';
35
36
                $pattern = '/<\s*slot[^>]*name=[\'"](.*)[\'"][^>]*>((.|\n)*?)<\s*\/\s*slot>/m';
37
38
                $contents = preg_replace_callback($pattern, function ($result) {
39
                    [$slot, $name, $contents] = $result;
0 ignored issues
show
Bug introduced by
The variable $slot does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $contents does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
41
                    return "@slot('{$name}'){$contents}@endslot";
42
                }, $contents);
0 ignored issues
show
Bug introduced by
The variable $contents seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
43
44
                return "@component('{$classOrView}', {$data})".$contents."@endcomponent";
45
            }, $view);
46
        }
47
48
        return $view;
49
    }
50
}