Completed
Push — master ( a5847c...c75d22 )
by Andrii
02:34
created

DoController::fetchUniqueIconsList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * 50+ icons for payment systems and methods
4
 *
5
 * @link      https://github.com/hiqdev/payment-icons
6
 * @package   payment-icons
7
 * @license   MIT
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\paymenticons\console;
12
13
use hidev\helpers\FileHelper;
14
use Yii;
15
16
class DoController extends \yii\console\Controller
17
{
18
    private $iconsList;
19
    private $uniqueIconsList;
20
21
    /**
22
     * Returns list of icons.
23
     * @return array file path => name
24
     */
25
    public function getUniqueIconsList()
26
    {
27
        if ($this->uniqueIconsList === null) {
28
            $this->uniqueIconsList = $this->fetchUniqueIconsList();
29
        }
30
31
        return $this->uniqueIconsList;
32
    }
33
34
    /**
35
     * Fetches list of unique icons.
36
     * @return array
37
     */
38
    public function fetchUniqueIconsList()
39
    {
40
        $md5s = [];
41
        foreach ($this->getIconsList() as $path => $name) {
42
            $hash = md5_file($path);
43
            if (in_array($hash, $md5s, true)) {
44
                continue;
45
            }
46
            $md5s[$path] = $hash;
47
            $list[$path] = $name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $list = 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...
48
        }
49
50
        return $list;
0 ignored issues
show
Bug introduced by
The variable $list 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...
51
    }
52
53
    /**
54
     * Returns list of icons.
55
     * @return array file path => name
56
     */
57
    public function getIconsList()
58
    {
59
        if ($this->iconsList === null) {
60
            $this->iconsList = $this->fetchIconsList();
61
        }
62
63
        return $this->iconsList;
64
    }
65
66
    /**
67
     * Scans directory to prepare list of icons.
68
     * @return array
69
     */
70
    public function fetchIconsList()
71
    {
72
        $dir = Yii::getAlias('@hiqdev/paymenticons/assets/png/xs');
73
        $files = scandir($dir);
74
        $list = [];
75
        foreach ($files as $file) {
76
            if ($file[0] === '.') {
77
                continue;
78
            }
79
            $name = pathinfo($file)['filename'];
80
            $list["$dir/$file"] = $name;
81
        }
82
83
        return $list;
84
    }
85
86
    /**
87
     * Generates CSS file.
88
     * @return string
89
     */
90
    public function genCss()
91
    {
92
        $sizes = [
93
            'xs' => 'height: 38px; width: 60px;',
94
            'sm' => 'height: 75px; width: 120px;',
95
            'md' => 'height: 240px; width: 150px;',
96
            'lg' => 'height: 480px; width: 300px;',
97
        ];
98
99
        $res = '.pi { display: inline-block;height: 38px;width: 60px; }' . PHP_EOL;
100
101
        foreach (array_keys($sizes) as $size) {
102
            $res .= ".pi.pi-$size { $sizes[$size] }" . PHP_EOL;
103
        }
104
105
        foreach (array_keys($sizes) as $size) {
106
            foreach ($this->getIconsList() as $name) {
107
                if ($size === 'xs') {
108
                    $res .= ".pi.pi-$size.pi-$name, .pi.pi-$name { background: url('../png/$size/$name.png') no-repeat right; }" . PHP_EOL;
109
                } else {
110
                    $res .= ".pi.pi-$size.pi-$name { background: url('../png/$size/$name.png') no-repeat right; }" . PHP_EOL;
111
                }
112
            }
113
            $res .= PHP_EOL;
114
        }
115
116
        return $res;
117
    }
118
119
    public function actionGenCss()
120
    {
121
        echo $this->genCss();
122
    }
123
124
    public function actionWriteCss()
125
    {
126
        FileHelper::write('@hiqdev/paymenticons/assets/css/payment-icons.css', $this->genCss());
127
    }
128
129
    public function actionWritePreviews()
130
    {
131
        $sizes = ['xs', 'sm', 'md', 'lg'];
132
133
        foreach ($sizes as $size) {
134
            $str = '';
135
            foreach ($this->getUniqueIconsList() as $name) {
136
                $str .= "![$name](https://raw.githubusercontent.com/hiqdev/payment-icons/master/src/assets/png/$size/$name.png)\n";
137
            }
138
            FileHelper::write('@hiqdev/paymenticons/../docs/Preview' . strtoupper($size) . '.md', $str);
139
            if ($size === 'xs') {
140
                $ps = [];
141
                foreach ($sizes as $s) {
142
                    $us = strtoupper($s);
143
                    $ps[] = "[$us](docs/Preview$us.md)";
144
                }
145
                $str .= "\n" . implode(' | ', $ps);
146
                FileHelper::write('@hiqdev/paymenticons/../docs/readme/Preview.md', $str);
147
            }
148
        }
149
    }
150
}
151