Completed
Push — master ( aa3e09...85e896 )
by Mikołaj
04:06
created

Model::getList()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 0
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Appearance\Roll\Admin;
4
5
use Rudolf\Framework\Model\AdminModel;
6
7
class Model extends AdminModel
8
{
9
    private $themes;
10
11
    public function __construct()
12
    {
13
        parent::__construct();
14
15
        $this->themes = $this->getThemesList();
16
    }
17
18
    private function getThemesList()
19
    {
20
        return array_diff(scandir(THEMES_ROOT.'/front'), array('.', '..'));
21
    }
22
23
    public function getTotalNumber()
24
    {
25
        return count($this->themes);
26
    }
27
28
    public function getList()
29
    {
30
        if (empty($this->themes)) {
31
            return false;
32
        }
33
34
        $i = 1;
35
        foreach ($this->themes as $key => $value) {
36
            $path = THEMES_ROOT.'/front/'.$value.'/theme.php';
37
38
            if (file_exists($path)) {
39
                include_once $path;
40
            }
41
42
            $array[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$array was never initialized. Although not strictly required by PHP, it is generally a good practice to add $array = 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...
43
                'id' => $i++,
44
                'path' => str_replace(['/theme.php', 'public/'], '', $path),
45
                'name' => $value,
46
                'description' => $value::DESCRIPTION,
47
                'full-name' => $value::NAME,
48
                'author' => $value::AUTHOR,
49
                'version' => $value::VERSION,
50
                'active' => FRONT_THEME == $value,
51
            ];
52
        }
53
54
        return $array;
0 ignored issues
show
Bug introduced by
The variable $array 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...
55
    }
56
}
57