Passed
Push — master ( 6215ec...1255dc )
by Darío
01:47
created

View   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 6 2
A __construct() 0 4 1
A getName() 0 3 1
A setName() 0 3 1
A getContents() 0 13 3
A getPath() 0 3 1
A setPath() 0 3 1
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
/**
14
 * View class
15
 *
16
 * This class manages views and parameters
17
 */
18
class View
19
{
20
    use \Drone\Util\ParamTrait;
21
22
    /**
23
     * View name
24
     *
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * The path where views are located.
31
     *
32
     * @var string
33
     */
34
    protected $path;
35
36
    /**
37
     * Returns the view name
38
     *
39
     * @return string
40
     */
41
    public function getName()
42
    {
43
        return $this->name;
44
    }
45
46
    /**
47
     * Returns the path
48
     *
49
     * @return string
50
     */
51
    public function getPath()
52
    {
53
        return $this->path;
54
    }
55
56
    /**
57
     * Sets the view name
58
     *
59
     * @param string
60
     *
61
     * @return null
62
     */
63
    public function setName($name)
64
    {
65
        $this->name = $name;
66
    }
67
68
    /**
69
     * Sets the path
70
     *
71
     * @param string
72
     *
73
     * @return string
74
     */
75
    public function setPath($path)
76
    {
77
        $this->path = $path;
78
    }
79
80
    /**
81
     * Constructor
82
     *
83
     * @param string $name
84
     * @param array  $parameters
85
     */
86
    public function __construct($name, Array $parameters = [])
87
    {
88
        $this->name  = $name;
89
        $this->setParams($parameters);
90
    }
91
92
    /**
93
     * Get the contents of the view
94
     *
95
     * @throws Exception\ViewNotFoundException
96
     *
97
     * @return  null
98
     */
99
    public function getContents()
100
    {
101
        $_view = $this->path . DIRECTORY_SEPARATOR . $this->name . '.phtml';
102
103
        if (!file_exists($_view))
104
            throw new Exception\ViewNotFoundException("The view '" .$this->name. "' does not exists");
105
106
        $contents = file_get_contents($_view);
107
108
        if ($contents === false)
109
            throw new Exception\ViewGetContentsErrorException("The view '" .$this->name. "' does not exists");
110
111
        return $contents;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $contents returns the type string which is incompatible with the documented return type null.
Loading history...
112
    }
113
114
    /**
115
     * Loads the view
116
     *
117
     * @return  null
118
     */
119
    public function render()
120
    {
121
        if (count($this->getParams()))
122
            extract($this->getParams(), EXTR_SKIP);
123
124
        include $this->getContents();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getContents() targeting Drone\Mvc\View::getContents() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
    }
126
}