Completed
Push — master ( 739744...ed27c8 )
by Basil
02:40
created

BaseDevCommand::getConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace luya\dev;
4
5
use luya\console\Command;
6
use luya\helpers\FileHelper;
7
use yii\helpers\Json;
8
9
/**
10
 * BaseDevCommand Controller.
11
 * 
12
 * Provides the IO for the configuration storage.
13
 * 
14
 * @author Basil Suter <[email protected]>
15
 */
16
class BaseDevCommand extends Command
17
{
18
	/**
19
	 * @var string The location of the devconfig json where data is stored.
20
	 */
21
    public $configFile = '@appFolder/devconfig.json';
22
    
23
    /**
24
     * Display config data and location.
25
     * 
26
     * @return boolean|void
27
     */
28
    public function actionConfigInfo()
29
    {
30
    	$this->outputInfo("dev config file: " . Yii::getAlias($this->configFile));
31
    	
32
    	$config = $this->readConfig();
33
    	
34
    	if (!$config) {
35
    		return $this->outputError("Unable to open config file.");
36
    	}
37
    	
38
    	foreach ($config as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $config of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
39
    		$this->output("{$key} => {$value}");
40
    	}
41
    }
42
    
43
    /**
44
     * Read entire config and return as array.
45
     * 
46
     * @return array|boolean
47
     */
48
    protected function readConfig()
49
    {
50
        $data = FileHelper::getFileContent($this->configFile);
51
        
52
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
            return Json::decode($data);
54
        }
55
        
56
        return false;
57
    }
58
    
59
    /**
60
     * Get a specific value for a given key.
61
     * 
62
     * @param string $key
63
     * @return boolean
64
     */
65
    protected function getConfig($key)
66
    {
67
        $config = $this->readConfig();
68
        
69
        return isset($config[$key]) ? $config[$key] : false;
70
    }
71
    
72
    /**
73
     * Save a value in the config for a given key.
74
     * 
75
     * @param string $key
76
     * @param mixed $value
77
     * @return mixed
78
     */
79
    protected function saveConfig($key, $value)
80
    {
81
        $content = $this->readConfig();
82
     
83
        if (!$content) {
84
            $content = [];
85
        }
86
        
87
        $content[$key] = $value;
88
        
89
        $save = FileHelper::writeFile($this->configFile, Json::encode($content));
90
        
91
        if (!$save) {
92
            return $this->outputError("Unable to find config file " . $this->configFile. ". Please create and provide Permissions.");
93
        }
94
        
95
        return $value;
96
    }
97
}