Completed
Push — 186-data ( d4ee1e )
by
unknown
08:25 queued 10s
created

FileSystemRenderTemplate::renderTemplate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 1
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\RenderTemplate;
4
5
use League\Plates;
6
use Throwable;
7
use Exception;
8
9
final class FileSystemRenderTemplate implements Plates\RenderTemplate
10
{
11
    private $hydrate;
12
    private $include;
13
    private $create_render_context;
14
    private $render_context_var_name;
15
16
    public function __construct(
17
        Plates\HydrateTemplate $hydrate = null,
18
        $include = null
19
    ) {
20
        $this->hydrate = $hydrate ?: Plates\HydrateTemplate\CallableHydrateTemplate::stub();
21
        $this->include = $include ?: Plates\Template\phpInclude();
22
    }
23
24
    public function renderTemplate(Plates\Template $template) {
25
        $this->hydrate->hydrateTemplate($template);
26
27
        $ctx = call_user_func($this->create_render_context, $this, $template);
28
29
        if (array_key_exists($this->render_context_var_name, $data)) {
0 ignored issues
show
Bug introduced by
The variable $data 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...
30
            throw new Plates\Exception\PlatesException('Cannot set render context because a variable already exists as ' . $this->render_context_var_name);
31
        }
32
        $data[$this->render_context_var_name] = $ctx;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
33
34
        // this is done for BC reasons}
35
        $include = $this->include->bindTo($ctx);
36
37
        try {
38
            return $include($path, $data);
0 ignored issues
show
Bug introduced by
The variable $path 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...
39
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
40
41
        } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
43
        }
44
45
        throw new Plates\Exception\PlatesException(
46
            'An exception occurred while rendering Template ' . $template->name . '.',
47
            0,
48
            $e
49
        );
50
    }
51
}
52