Completed
Push — master ( 563e8f...89e42c )
by Loban
02:06
created

FileStorage::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
crap 2
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
    public $path = '@settings';
22
23
    public $fileMode;
24
25
    public $dirMode = 0755;
26
27
    public $fileSuffix = '.bin';
28
29 1
    public function init()
30
    {
31 1
        parent::init();
32 1
        $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...
33 1
        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...
34 1
    }
35
36
    /**
37
     * @param string $key
38
     * @return false|null|string
39
     */
40 1
    public function getValue($key)
41
    {
42 1
        $file = $this->getFile($key);
43
44 1
        $fp = @fopen($file, 'r');
45 1
        if ($fp !== false) {
46 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...
47 1
            $value = @stream_get_contents($fp);
48 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...
49 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...
50 1
            return $value;
51
        }
52
53 1
        return false;
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param string $value
59
     * @return bool
60
     */
61 1
    public function setValue($key, $value)
62
    {
63 1
        $file = $this->getFile($key);
64
65 1
        if (@file_put_contents($file, $value, LOCK_EX) !== false) {
66 1
            if ($this->fileMode !== null) {
67 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...
68 1
            }
69 1
            return true;
70
        } else {
71
            $error = error_get_last();
72
            Yii::warning("Unable to write file '{$file}': {$error['message']}", __METHOD__);
73
            return false;
74
        }
75
    }
76
77
    /**
78
     * @param string $key
79
     * @return bool
80
     */
81 1
    public function deleteValue($key)
82
    {
83 1
        $file = $this->getFile($key);
84
85 1
        return @unlink($file);
86
    }
87
88
    /**
89
     * Returns the storage file path given the key.
90
     * @param string $key
91
     * @return string the file path
92
     */
93 1
    protected function getFile($key)
94
    {
95 1
        return $this->path . DIRECTORY_SEPARATOR . $key . $this->fileSuffix;
96
    }
97
}