Issues (53)

src/Traits/CanRenderTrait.php (4 issues)

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
     * @param array $variables
19
     * @param bool $return
20 2
     *
21 2
     * @return string|bool
22 2
     */
23
    public function load($view, $variables = [], $return = false)
24 2
    {
25
        $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

25
        $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...
26
27
        if (true === $return) {
28
            return $html;
29
        }
30
31
        echo $html;
32
33 1
        return null;
34
    }
35 1
36
    /**
37
     * @return string
38
     *
39
     * @deprecated use render($view, $variables)
40
     */
41
    public function getContents($view, array $variables = [])
42
    {
43
        return $this->render($view, $variables);
44
    }
45 4
46
    /**
47 4
     * @param string $block
48
     */
49 4
    public function render($name, array $data = [])
50
    {
51
        if ($this->isBlock($name)) {
52
            $name = '/' . $this->blocks[$name];
53 4
        }
54
55 4
        return parent::render($name, $data);
56
    }
57
58
    /**
59
     * Builds path for including
60
     * If $view starts with / the path will be relative to the root of the views folder.
61
     * Otherwise to caller file location.
62
     *
63 4
     * @param string $view
64
     *
65 4
     * @return string
66
     */
67 4
    public function buildPath($view)
68
    {
69 4
        return $this->getResolveTemplatePath()->find($view);
0 ignored issues
show
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

69
        return $this->/** @scrutinizer ignore-call */ getResolveTemplatePath()->find($view);
Loading history...
70 4
    }
71
72 4
    /**
73 4
     * @return mixed
74 4
     */
75
    public function getBlock($name)
76 4
    {
77
        return $this->blocks[$name];
78
    }
79
80
    public function setBlock($name, $block)
81
    {
82
        $this->blocks[$name] = $block;
83
    }
84
85
    /**
86
     * @param string $block
87
     */
88
    public function isBlock($block = 'default'): bool
89
    {
90
        if (!is_string($block)) {
0 ignored issues
show
The condition is_string($block) is always true.
Loading history...
91
            return false;
92
        }
93
94
        return isset($this->blocks[$block]) && !empty($this->blocks[$block]);
95
    }
96
97
    /**
98
     * Create a new template.
99
     *
100
     * @param string $name
101
     *
102
     * @return Template
103
     */
104
    public function make($name, array $data = [])
105
    {
106
        $template = new  \Nip\View\Template\Template($this, $name);
0 ignored issues
show
$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

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