CViewContainer::add()   B
last analyzed

Complexity

Conditions 9
Paths 11

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
nc 11
nop 4
dl 0
loc 37
ccs 0
cts 25
cp 0
crap 90
rs 7.7724
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\View;
4
5
/**
6
 * A view container, store all views per region, render at will.
7
 *
8
 */
9
class CViewContainer implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\TConfigure,
12
        \Anax\DI\TInjectionAware;
13
14
15
16
    /**
17
     * Properties
18
     *
19
     */
20
    private $views = []; // Array for all views
21
22
23
24
    /**
25
     * Convert template to path to template file.
26
     *
27
     * @param string $template the name of the template file to include
28
     *
29
     * @throws Anax\View\Exception when template file is missing
30
     *
31
     * @return string as path to the template file
32
     */
33
    public function getTemplateFile($template)
34
    {
35
        $paths  = $this->config["path"];
36
        $suffix = $this->config["suffix"];
37
38
        foreach ($paths as $path) {
39
            $file = $path . "/" . $template . $suffix;
40
            if (is_file($file)) {
41
                return $file;
42
            }
43
        }
44
45
        throw new Exception("Could not find template file '$template'.");
46
    }
47
48
49
50
    /**
51
     * Add a view to be included as a template file.
52
     *
53
     * @param string $template the name of the template file to include
54
     * @param array  $data     variables to make available to the view, default is empty
55
     * @param string $region   which region to attach the view
56
     * @param int    $sort     which order to display the views
57
     *
58
     * @return $this
59
     */
60
    public function add($template, $data = [], $region = "main", $sort = 0)
61
    {
62
        if (empty($template)) {
63
            return $this;
64
        }
65
66
        $view = $this->di->get("view");
67
68
        if (is_string($template)) {
69
            $tpl = $this->getTemplateFile($template);
70
            $type = "file";
71
        } elseif (is_array($template)) {
72
            // Can be array with complete view or array with callback
73
            $tpl = $template;
74
            $type = null;
75
            $region = isset($tpl["region"])
76
                ? $tpl["region"]
77
                : $region;
78
79
            if (isset($tpl["callback"])) {
80
                // Need to test the callback!
81
                $tpl["template"] = $template;
82
                $tpl["type"] = "callback";
83
            } elseif (isset($tpl["template"])) {
84
                if (!isset($tpl["type"]) || $tpl["type"] === "file") {
85
                    $tpl["type"] = "file";
86
                    $tpl["template"] = $this->getTemplateFile($tpl["template"]);
87
                }
88
            }
89
        }
90
91
        $view->set($tpl, $data, $sort, $type);
0 ignored issues
show
Bug introduced by
The variable $tpl does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
92
        $view->setDI($this->di);
93
        $this->views[$region][] = $view;
94
95
        return $this;
96
    }
97
98
99
100
    /**
101
     * Add a callback to be rendered as a view.
102
     *
103
     * @param string $callback function to call to get the content of the view
104
     * @param array  $data     variables to make available to the view, default is empty
105
     * @param string $region   which region to attach the view
106
     * @param int    $sort     which order to display the views
107
     *
108
     * @return $this
109
     */
110 View Code Duplication
    public function addCallback($callback, $data = [], $region = "main", $sort = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $view = $this->di->get("view");
113
        $view->set(["callback" => $callback], $data, $sort, "callback");
114
        $view->setDI($this->di);
115
        $this->views[$region][] = $view;
116
117
        return $this;
118
    }
119
120
121
122
    /**
123
     * Add a string as a view.
124
     *
125
     * @param string $content the content
126
     * @param string $region  which region to attach the view
127
     * @param int    $sort    which order to display the views
128
     *
129
     * @return $this
130
     */
131 View Code Duplication
    public function addString($content, $region = "main", $sort = 0)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $view = $this->di->get("view");
134
        $view->set($content, [], $sort, "string");
135
        $view->setDI($this->di);
136
        $this->views[$region][] = $view;
137
        
138
        return $this;
139
    }
140
141
142
143
    /**
144
     * Check if a region has views to render.
145
     *
146
     * @param string $region which region to check
147
     *
148
     * @return $this
149
     */
150
    public function hasContent($region)
151
    {
152
        return isset($this->views[$region]);
153
    }
154
155
156
157
    /**
158
     * Render all views for a specific region.
159
     *
160
     * @param string $region which region to use
161
     *
162
     * @return $this
163
     */
164
    public function render($region = "main")
165
    {
166
        if (!isset($this->views[$region])) {
167
            return $this;
168
        }
169
170
        mergesort($this->views[$region], function ($a, $b) {
171
            $sa = $a->sortOrder();
172
            $sb = $b->sortOrder();
173
174
            if ($sa == $sb) {
175
                return 0;
176
            }
177
178
            return $sa < $sb ? -1 : 1;
179
        });
180
181
        foreach ($this->views[$region] as $view) {
182
            $view->render();
183
        }
184
185
        return $this;
186
    }
187
}
188