Completed
Push — master ( 2a3466...2685c9 )
by Mikael
03:29
created

View   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 35.7%

Importance

Changes 0
Metric Value
dl 112
loc 112
ccs 15
cts 42
cp 0.357
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
B set() 32 32 6
B render() 30 30 7
A sortOrder() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\View;
4
5
use \Anax\View\ViewRenderFile;
6
use \Anax\View\Exception;
7
8
/**
9
 * A view connected to a template file.
10
 */
11 View Code Duplication
class View
0 ignored issues
show
Duplication introduced by
This class 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...
12
{
13
    /**
14
     * @var $template     Template file or array
15
     * @var $templateData Data to send to template file
16
     * @var $sortOrder    For sorting views
17
     * @var $type         Type of view
18
     */
19
    private $template;
20
    private $templateData = [];
21
    private $sortOrder;
22
    private $type;
23
24
25
26
    /**
27
     * Set values for the view.
28
     *
29
     * @param array|string $template the template file, or array
30
     * @param array        $data     variables to make available to the
31
     *                               view, default is empty
32
     * @param integer      $sort     which order to display the views,
33
     *                               if suitable
34
     * @param string       $type     which type of view
35
     *
36
     * @return self
37
     */
38 3
    public function set($template, $data = [], $sort = 0, $type = "file")
39
    {
40 3
        if (is_array($template)) {
41
            if (isset($template["callback"])) {
42
                $type = "callback";
43
                $this->template = $template;
44
            } else {
45
                $this->template = $template["template"];
46
            }
47
48
            $this->templateData = isset($template["data"])
49
                ? $template["data"]
50
                : $data;
51
52
            $this->sortOrder = isset($template["sort"])
53
                ? $template["sort"]
54
                : $sort;
55
56
            $this->type = isset($template["type"])
57
                ? $template["type"]
58
                : $type;
59
60
            return;
61
        }
62
63 3
        $this->template     = $template;
64 3
        $this->templateData = $data;
65 3
        $this->sortOrder    = $sort;
66 3
        $this->type         = $type;
67
68 3
        return $this;
69
    }
70
71
72
73
    /**
74
     * Render the view.
75
     *
76
     * @param object $app optional with access to the framework resources.
77
     *
78
     * @return void
79
     */
80 3
    public function render($app = null)
81
    {
82 3
        switch ($this->type) {
83
            case "file":
84 1
                if (!$app) {
85 1
                    throw new Exception("View missing \$app.");
86
                }
87
                $viewRender = new ViewRenderFile();
88
                $viewRender->setApp($app);
89
                $viewRender->render($this->template, $this->templateData);
0 ignored issues
show
Bug introduced by
It seems like $this->template can also be of type array<string,?,{"callback":"?"}>; however, Anax\View\ViewRenderFile::render() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
                break;
91
92
            case "callback":
93
                if (!isset($this->template["callback"]) || !is_callable($this->template["callback"])) {
94
                    throw new Exception("View missing callback.");
95
                }
96
97
                echo call_user_func($this->template["callback"]);
98
99
                break;
100
101
            case "string":
102 1
                echo $this->template;
103
104 1
                break;
105
106
            default:
107 1
                throw new Exception("Not a valid template type: {$this->type}");
108
        }
109 1
    }
110
111
112
113
    /**
114
     * Give the sort order for this view.
115
     *
116
     * @return int
117
     */
118
    public function sortOrder()
119
    {
120
        return $this->sortOrder;
121
    }
122
}
123