Completed
Push — master ( 6190dc...40907e )
by Andrii
02:54
created

Theme::buildWidgetPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * Theme Manager for Yii2
5
 *
6
 * @link      https://github.com/hiqdev/yii2-thememanager
7
 * @package   yii2-thememanager
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\thememanager;
13
14
use ReflectionClass;
15
use Yii;
16
use yii\helpers\ArrayHelper;
17
18
/**
19
 * Theme class.
20
 */
21
class Theme extends \yii\base\Theme implements \hiqdev\yii2\collection\ItemWithNameInterface
22
{
23
    use GetManagerTrait;
24
25
    /**
26
     * @var string theme name
27
     */
28
    public $name;
29
30
    /**
31
     * @var string theme label
32
     */
33
    public $label;
34
35
    /**
36
     * @var array assets to be registered for this theme
37
     */
38
    public $assets = [];
39
40
    private $_view;
41
42
    /**
43
     * Returns the view object that can be used to render views or view files.
44
     * The [[render()]] and [[renderFile()]] methods will use
45
     * this view object to implement the actual view rendering.
46
     * If not set, it will default to the "view" application component.
47
     *
48
     * @return \yii\web\View the view object that can be used to render views or view files.
49
     */
50
    public function getView()
51
    {
52
        if ($this->_view === null) {
53
            $this->_view = $this->getManager()->getView();
54
        }
55
56
        return $this->_view;
57
    }
58
59
    /**
60
     * Sets the view object to be used.
61
     *
62
     * @param View $view the view object that can be used to render views or view files.
63
     */
64
    public function setView($view)
65
    {
66
        $this->_view = $view;
67
    }
68
69
    /**
70
     * Getter for pathMap.
71
     */
72
    public function init()
73
    {
74
        parent::init();
75
        if (!is_array($this->pathMap)) {
76
            $this->pathMap = [];
77
        }
78
79
        $this->pathMap = ArrayHelper::merge([
80
            $this->getViewPath()    => $this->buildViewPaths(),
81
            $this->getWidgetPath()  => $this->buildWidgetPaths(),
82
        ], $this->pathMap);
83
84
        foreach ($this->pathMap as $key => &$paths) {
85
            $paths = array_reverse(array_unique(array_values($paths)));
86
        }
87
    }
88
89
    protected $_viewPath;
90
    protected $_widgetPath;
91
92
    public function getViewPath()
93
    {
94
        return $this->_viewPath ?: Yii::$app->viewPath;
95
    }
96
97
    public function getWidgetPath()
98
    {
99
        return $this->_widgetPath ?: __DIR__ . '/widgets/views';
100
    }
101
102
    public function calcParentPaths()
103
    {
104
        $ref = $this->getReflection();
105
        for ($depth = 0;$depth < 10;++$depth) {
106
            $dirs[] = dirname($ref->getFilename());
0 ignored issues
show
Coding Style Comprehensibility introduced by
$dirs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dirs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
            $ref    = $ref->getParentClass();
108
            if (__CLASS__ === $ref->name) {
109
                break;
110
            }
111
        }
112
113
        return $dirs;
0 ignored issues
show
Bug introduced by
The variable $dirs 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...
114
    }
115
116
    public function buildViewPaths()
117
    {
118
        $res = [];
119
        foreach ($this->calcParentPaths() as $dir) {
120
            $res[] = $dir . DIRECTORY_SEPARATOR . 'views';
121
        }
122
        foreach ($this->getManager()->viewPaths as $dir) {
123
            $res[] = $dir;
124
        }
125
126
        return $res;
127
    }
128
129
    public function buildWidgetPaths()
130
    {
131
        $res = [];
132
        foreach ($this->buildViewPaths() as $dir) {
133
            $res[] = $dir . DIRECTORY_SEPARATOR . 'widgets';
134
        }
135
136
        return $res;
137
    }
138
139
    protected $_baseUrl;
140
141
    /**
142
     * @return string the base URL (without ending slash) for this theme.
143
     *                All resources of this theme are considered to be under this base URL.
144
     */
145
    public function getBaseUrl()
146
    {
147
        if (!$this->_baseUrl) {
148
            $this->_baseUrl = '@web/themes/' . $this->name;
149
        }
150
151
        return $this->_baseUrl;
152
    }
153
154
    protected $_reflection;
155
156
    public function getReflection()
157
    {
158
        if (!$this->_reflection) {
159
            $this->_reflection = new ReflectionClass($this);
160
        }
161
162
        return $this->_reflection;
163
    }
164
165
    private $_settings;
166
167
    /**
168
     * @param $settings string theme settings model class name
169
     */
170
    public function setSettings($settings)
171
    {
172
        $this->_settings = $settings;
173
    }
174
175
    public function getSettings()
176
    {
177
        if (!is_object($this->_settings)) {
178
            if (!$this->_settings) {
179
                $this->_settings = static::findSettingsClass(get_called_class());
180
            }
181
            $this->_settings = Yii::createObject($this->_settings);
182
            $this->_settings->load();
183
        }
184
185
        return $this->_settings;
186
    }
187
188
    public static function calcSettingsClass($class)
189
    {
190
        return substr($class, 0, strrpos($class, '\\')) . '\\models\\Settings';
191
    }
192
193
    public static function findSettingsClass($class)
194
    {
195
        $res = static::calcSettingsClass($class);
196
197
        return class_exists($res) ? $res : static::findSettingsClass(get_parent_class($class));
198
    }
199
}
200