Test Failed
Push — master ( be2c75...8a8b2d )
by Gabriel
19:12 queued 10s
created

CanRenderTrait::loadIf()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 4
c 1
b 0
f 1
nc 4
nop 4
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
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|Callable $condition
13
     * @param $view
14
     * @param array $variables
15
     * @param bool $return
16
     * @return bool|string|void|null
17
     */
18 2
    public function loadIf($condition, $view, $variables = [], $return = false)
19
    {
20 2
        $condition = is_callable($condition) ? $condition() : $condition;
21 2
        if ($condition == false) {
22 2
            return;
23
        }
24 2
        return $this->load($view, $variables, $return);
25
    }
26
27
    /**
28
     * @param $view
29
     * @param array $variables
30
     * @param bool $return
31
     * @return bool|string|void|null
32
     */
33 1
    public function loadIfExists($view, $variables = [], $return = false)
34
    {
35 1
        return $this->loadIf($this->existPath($view), $view, $variables, $return);
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

35
        return $this->loadIf($this->/** @scrutinizer ignore-call */ existPath($view), $view, $variables, $return);
Loading history...
36
    }
37
38
    /** @noinspection PhpInconsistentReturnPointsInspection
39
     *
40
     * @param $view
41
     * @param array $variables
42
     * @param bool $return
43
     * @return string|boolean
44
     */
45 4
    public function load($view, $variables = [], $return = false)
46
    {
47 4
        $html = $this->getContents($view, $variables);
48
49 4
        if ($return === true) {
50
            return $html;
51
        }
52
53 4
        echo $html;
54
55 4
        return null;
56
    }
57
58
    /**
59
     * @param $view
60
     * @param array $variables
61
     * @return string
62
     */
63 4
    public function getContents($view, $variables = [])
64
    {
65 4
        extract($variables);
66
67 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

67
        /** @scrutinizer ignore-call */ 
68
        $path = $this->buildPath($view);
Loading history...
68
69 4
        unset($view, $variables);
70 4
        ob_start();
71
        /** @noinspection PhpIncludeInspection */
72 4
        include($path);
73 4
        $html = ob_get_contents();
74 4
        ob_end_clean();
75
76 4
        return $html;
77
    }
78
}
79