Completed
Push — master ( 816e5a...57fb58 )
by Restu
11:24
created

BasicTemplate::render()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 14
c 1
b 0
f 1
nc 4
nop 0
dl 0
loc 20
rs 9.4285
1
<?php
2
namespace JayaCode\Framework\Core\View\Template;
3
4
/**
5
 * Class BasicTemplate
6
 * @package JayaCode\Framework\Core\View\Template
7
 */
8
class BasicTemplate extends Template
9
{
10
    /**
11
     * @var
12
     */
13
    protected $parent;
14
15
    /**
16
     * @return string
17
     */
18
    public function buildScript()
19
    {
20
        $fileCache = $this->cacheDir.str_replace(["/", "."], "_", $this->fileTemplate);
21
        if ($this->cacheDir && file_exists($fileCache) && empty($this->contentParent)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cacheDir of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
22
            return $this->script = file_get_contents($fileCache);
0 ignored issues
show
Documentation Bug introduced by
It seems like file_get_contents($fileCache) of type string is incompatible with the declared type null of property $script.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
        }
24
25
        $this->script = $this->converter->build(file_get_contents($this->locTemplate));
26
        
27
        if ($this->cacheDir && empty($this->contentParent)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->cacheDir of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
28
            file_put_contents($fileCache, $this->script);
29
        }
30
    }
31
32
    /**
33
     * @return mixed|string
34
     */
35
    public function render()
36
    {
37
        if (!$this->script) {
38
            $this->buildScript();
39
        }
40
41
        extract($this->vars->all());
0 ignored issues
show
Bug introduced by
$this->vars->all() cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
42
        ob_start();
43
        eval("?>" . $this->script);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
44
        $result = ob_get_contents();
45
        ob_end_clean();
46
        
47
        if ($this->parent) {
48
            $fileParent = $this->parent.Template::$extension;
49
            $parent = new BasicTemplate($this->locView, $fileParent, $this->variableCollector, $this->converter);
50
            $parent->setContentParent($this->contentParent);
51
            return $parent->render();
52
        }
53
        return $result;
54
    }
55
56
    /**
57
     * @param $name
58
     */
59
    public function setParent($name)
60
    {
61
        $this->parent = $name;
62
    }
63
64
    /**
65
     * @var array
66
     */
67
    protected $contentParent = [];
68
69
    /**
70
     * @return array
71
     */
72
    public function getContentParent()
73
    {
74
        return $this->contentParent;
75
    }
76
77
    /**
78
     * @param array $contentParent
79
     */
80
    public function setContentParent($contentParent)
81
    {
82
        $this->contentParent = $contentParent;
83
    }
84
}
85