Completed
Push — master ( f2713b...6190dc )
by Andrii
02:36
created

Theme::calcPathDirs()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.2
cc 4
eloc 11
nc 6
nop 0
crap 20

1 Method

Rating   Name   Duplication   Size   Complexity  
A Theme::getBaseUrl() 0 8 2
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
        $this->pathMap = ArrayHelper::merge([
79
            $this->getViewPath() => $this->buildViewPaths(),
80
        ], $this->pathMap);
81
        $this->pathMap[$this->getViewPath()] = array_reverse(array_unique(array_values($this->pathMap[$this->getViewPath()])));
82
    }
83
84
    protected $_viewPath;
85
    protected $_widgetPath;
86
87
    public function getViewPath()
88
    {
89
        return $this->_viewPath ?: Yii::$app->viewPath;
90
    }
91
92
    public function getWidgetPath()
93
    {
94
        return $this->_widgetPath ?: preg_replace('/(.*)views/', '$1widgets', $this->getViewPath());
95
    }
96
97
    public function calcParentPaths()
98
    {
99
        $ref = $this->getReflection();
100
        for ($depth = 0;$depth < 10;++$depth) {
101
            $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...
102
            $ref    = $ref->getParentClass();
103
            if (__CLASS__ === $ref->name) {
104
                break;
105
            }
106
        }
107
108
        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...
109
    }
110
111
    public function buildViewPaths()
112
    {
113
        foreach ($this->calcParentPaths() as $dir) {
114
            $res[] = $dir . DIRECTORY_SEPARATOR . 'views';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = 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...
115
        }
116
        foreach ($this->getManager()->viewPaths as $dir) {
117
            $res[] = $dir;
0 ignored issues
show
Bug introduced by
The variable $res 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...
118
        }
119
120
        return $res;
121
    }
122
123
    protected $_baseUrl;
124
125
    /**
126
     * @return string the base URL (without ending slash) for this theme.
127
     *                All resources of this theme are considered to be under this base URL.
128
     */
129
    public function getBaseUrl()
130
    {
131
        if (!$this->_baseUrl) {
132
            $this->_baseUrl = '@web/themes/' . $this->name;
133
        }
134
135
        return $this->_baseUrl;
136
    }
137
138
    protected $_reflection;
139
140
    public function getReflection()
141
    {
142
        if (!$this->_reflection) {
143
            $this->_reflection = new ReflectionClass($this);
144
        }
145
146
        return $this->_reflection;
147
    }
148
149
    private $_settings;
150
151
    /**
152
     * @param $settings string theme settings model class name
153
     */
154
    public function setSettings($settings)
155
    {
156
        $this->_settings = $settings;
157
    }
158
159
    public function getSettings()
160
    {
161
        if (!is_object($this->_settings)) {
162
            if (!$this->_settings) {
163
                $this->_settings = static::findSettingsClass(get_called_class());
164
            }
165
            $this->_settings = Yii::createObject($this->_settings);
166
            $this->_settings->load();
167
        }
168
169
        return $this->_settings;
170
    }
171
172
    public static function calcSettingsClass($class)
173
    {
174
        return substr($class, 0, strrpos($class, '\\')) . '\\models\\Settings';
175
    }
176
177
    public static function findSettingsClass($class)
178
    {
179
        $res = static::calcSettingsClass($class);
180
181
        return class_exists($res) ? $res : static::findSettingsClass(get_parent_class($class));
182
    }
183
}
184