Passed
Push — master ( 50d1ae...1ff9a4 )
by Gabriel
08:59
created

CanRenderTrait::getContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Nip\View\Traits;
4
5
/**
6
 * Class CanRenderTrait
7
 * @package Nip\View\Traits
8
 */
9
trait CanRenderTrait
10
{
11
    /**
12
     * @param bool $condition
13
     * @param $view
14
     * @param array $variables
15
     * @param bool $return
16
     * @return bool|string|void|null
17
     */
18
    public function loadIf($condition, $view, $variables = [], $return = false)
19
    {
20
        if ($condition == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
21
            return;
22
        }
23
        return $this->load($view, $variables, $return);
24
    }
25
26
    /**
27
     * @param $view
28
     * @param array $variables
29
     * @param bool $return
30
     * @return bool|string|void|null
31
     */
32
    public function loadIfExists($view, $variables = [], $return = false)
33
    {
34
        if ($this->existPath($view)) {
0 ignored issues
show
Bug introduced by
It seems like existPath() 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

34
        if ($this->/** @scrutinizer ignore-call */ existPath($view)) {
Loading history...
35
            return;
36
        }
37
        return $this->load($view, $variables, $return);
38
    }
39
40
    /** @noinspection PhpInconsistentReturnPointsInspection
41
     *
42
     * @param $view
43
     * @param array $variables
44
     * @param bool $return
45
     * @return string|boolean
46
     */
47 4
    public function load($view, $variables = [], $return = false)
48
    {
49 4
        $html = $this->getContents($view, $variables);
50
51 4
        if ($return === true) {
52
            return $html;
53
        }
54
55 4
        echo $html;
56
57 4
        return null;
58
    }
59
60
    /**
61
     * @param $view
62
     * @param array $variables
63
     * @return string
64
     */
65 4
    public function getContents($view, $variables = [])
66
    {
67 4
        extract($variables);
68
69 4
        $path = $this->buildPath($view);
0 ignored issues
show
Bug introduced by
It seems like buildPath() 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
        /** @scrutinizer ignore-call */ 
70
        $path = $this->buildPath($view);
Loading history...
70
71 4
        unset($view, $variables);
72 4
        ob_start();
73
        /** @noinspection PhpIncludeInspection */
74 4
        include($path);
75 4
        $html = ob_get_contents();
76 4
        ob_end_clean();
77
78 4
        return $html;
79
    }
80
}
81