Completed
Push — master ( 89e42c...5439f4 )
by Loban
02:21
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 96
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A getValue() 0 15 2
A setValue() 0 15 3
A deleteValue() 0 6 1
A getFile() 0 4 1
1
<?php
2
/**
3
 * @link https://github.com/LAV45/yii2-settings
4
 * @copyright Copyright (c) 2016 LAV45
5
 * @author Alexey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\settings\storage;
10
11
use Yii;
12
use yii\base\Object;
13
use yii\helpers\FileHelper;
14
15
/**
16
 * Class FileStorage
17
 * @package lav45\settings\storage
18
 */
19
class FileStorage extends Object implements StorageInterface
20
{
21
    /**
22
     * @var string the directory to store settings files. You may use path alias here.
23
     * If not set, it will use the "settings" subdirectory under the application runtime path.
24
     */
25
    public $path = '@runtime/settings';
26
    /**
27
     * @var int the permission to be set for newly created cache files.
28
     * This value will be used by PHP chmod() function. No umask will be applied.
29
     * If not set, the permission will be determined by the current environment.
30
     */
31
    public $fileMode;
32
    /**
33
     * @var int the permission to be set for newly created directories.
34
     * This value will be used by PHP chmod() function. No umask will be applied.
35
     * Defaults to 0775, meaning the directory is read-writable by owner, but read-only for other users.
36
     */
37
    public $dirMode = 0755;
38
    /**
39
     * @var string settings file suffix. Defaults to '.bin'.
40
     */
41
    public $fileSuffix = '.bin';
42
43
    /**
44
     * Initializes this component by ensuring the existence of the settings path.
45
     */
46 2
    public function init()
47
    {
48 2
        parent::init();
49 2
        $this->path = Yii::getAlias($this->path);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->path) can also be of type boolean. However, the property $path is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
50 2
        FileHelper::createDirectory($this->path, $this->dirMode, true);
0 ignored issues
show
Bug introduced by
It seems like $this->path can also be of type boolean; however, yii\helpers\BaseFileHelper::createDirectory() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
51 2
    }
52
53
    /**
54
     * @param string $key
55
     * @return false|null|string
56
     */
57 1
    public function getValue($key)
58
    {
59 1
        $file = $this->getFile($key);
60
61 1
        $fp = @fopen($file, 'r');
62 1
        if ($fp !== false) {
63 1
            @flock($fp, LOCK_SH);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
64 1
            $value = @stream_get_contents($fp);
65 1
            @flock($fp, LOCK_UN);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66 1
            @fclose($fp);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
67 1
            return $value;
68
        }
69
70 1
        return false;
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param string $value
76
     * @return bool
77
     */
78 2
    public function setValue($key, $value)
79
    {
80 2
        $file = $this->getFile($key);
81
82 2
        if (@file_put_contents($file, $value, LOCK_EX) !== false) {
83 1
            if ($this->fileMode !== null) {
84 1
                @chmod($file, $this->fileMode);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
85 1
            }
86 1
            return true;
87
        } else {
88 1
            $error = error_get_last();
89 1
            Yii::warning("Unable to write file '{$file}': {$error['message']}", __METHOD__);
90 1
            return false;
91
        }
92
    }
93
94
    /**
95
     * @param string $key
96
     * @return bool
97
     */
98 1
    public function deleteValue($key)
99
    {
100 1
        $file = $this->getFile($key);
101
102 1
        return @unlink($file);
103
    }
104
105
    /**
106
     * Returns the storage file path given the key.
107
     * @param string $key
108
     * @return string the file path
109
     */
110 2
    protected function getFile($key)
111
    {
112 2
        return $this->path . DIRECTORY_SEPARATOR . $key . $this->fileSuffix;
113
    }
114
}