Test Failed
Push — master ( ed648f...76cdf4 )
by Gabriel
10:26
created

CanRenderTrait::loadIf()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 7
rs 10
ccs 1
cts 1
cp 1
cc 3
nc 4
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\View\Traits;
6
7
use League\Plates\Template\Template;
8
use function is_string;
9
10
/**
11
 * Class CanRenderTrait.
12
 */
13
trait CanRenderTrait
14
{
15
    protected $blocks = [];
16
17
    /** @noinspection PhpInconsistentReturnPointsInspection
18 2
     *
19
     * @param array $variables
20 2
     * @param bool $return
21 2
     *
22 2
     * @return string|bool
23
     */
24 2
    public function load($view, $variables = [], $return = false)
25
    {
26
        $html = $this->getContents($view, $variables);
0 ignored issues
show
Deprecated Code introduced by
The function Nip\View\Traits\CanRenderTrait::getContents() has been deprecated: use render($view, $variables) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

26
        $html = /** @scrutinizer ignore-deprecated */ $this->getContents($view, $variables);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
27
28
        if (true === $return) {
29
            return $html;
30
        }
31
32
        echo $html;
33 1
34
        return null;
35 1
    }
36
37
    /**
38
     * @return string
39
     *
40
     * @deprecated use render($view, $variables)
41
     */
42
    public function getContents($view, array $variables = [])
43
    {
44
        return $this->render($view, $variables);
45 4
    }
46
47 4
    /**
48
     * @param string $block
49 4
     */
50
    public function render($name, array $data = [])
51
    {
52
        if ($this->isBlock($name)) {
53 4
            $name = '/' . $this->blocks[$name];
54
        }
55 4
56
        return parent::render($name, $data);
57
    }
58
59
    /**
60
     * Builds path for including
61
     * If $view starts with / the path will be relative to the root of the views folder.
62
     * Otherwise to caller file location.
63 4
     *
64
     * @param string $view
65 4
     *
66
     * @return string
67 4
     */
68
    public function buildPath($view)
69 4
    {
70 4
        return $this->getResolveTemplatePath()->find($view);
0 ignored issues
show
Bug introduced by
It seems like getResolveTemplatePath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        return $this->/** @scrutinizer ignore-call */ getResolveTemplatePath()->find($view);
Loading history...
71
    }
72 4
73 4
    /**
74 4
     * @return mixed
75
     */
76 4
    public function getBlock($name)
77
    {
78
        return $this->blocks[$name];
79
    }
80
81
    public function setBlock($name, $block)
82
    {
83
        $this->blocks[$name] = $block;
84
    }
85
86
    /**
87
     * @param string $block
88
     */
89
    public function isBlock($block = 'default'): bool
90
    {
91
        if (!is_string($block)) {
0 ignored issues
show
introduced by
The condition is_string($block) is always true.
Loading history...
92
            return false;
93
        }
94
95
        return isset($this->blocks[$block]) && !empty($this->blocks[$block]);
96
    }
97
98
    /**
99
     * Create a new template.
100
     *
101
     * @param string $name
102
     *
103
     * @return Template
104
     */
105
    public function make($name)
106
    {
107
        return new \Nip\View\Template\Template($this, $name);
0 ignored issues
show
Bug introduced by
$this of type Nip\View\Traits\CanRenderTrait is incompatible with the type League\Plates\Engine expected by parameter $engine of Nip\View\Template\Template::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return new \Nip\View\Template\Template(/** @scrutinizer ignore-type */ $this, $name);
Loading history...
108
    }
109
}
110