StorageFile::save()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\microDb;
4
5
class StorageFile implements StorageInterface
6
{
7
8
    /**
9
     * @var string
10
     */
11
    protected $file;
12
    /**
13
     * @var resource
14
     */
15
    protected $res;
16
17
    /**
18
     * @param string $file
19
     */
20
    public function __construct(string $file)
21
    {
22
        $this->file = $file;
23
    }
24
25
    /**
26
     * @param bool $lock
27
     *
28
     * @return array
29
     * @throws \WebComplete\microDb\Exception
30
     */
31
    public function load(bool $lock): array
32
    {
33
        \clearstatcache();
34
        if (!\file_exists($this->file)) {
35
            $dir = \dirname($this->file);
36
            if (!\file_exists($dir)) {
37
                \mkdir($dir, 0700);
38
            }
39
            \touch($this->file);
40
        }
41
42
        if (!$this->res = \fopen($this->file, 'rb+')) {
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($this->file, 'rb+') can also be of type false. However, the property $res is declared as type resource. 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...
43
            throw new Exception('Cannot open file: ' . $this->file);
44
        }
45
46
        if ($lock) {
47
            \flock($this->res, \LOCK_EX);
48
        }
49
        $content = null;
50
        if ($fileSize = \filesize($this->file)) {
51
            $content = \fread($this->res, $fileSize);
52
        }
53
54
        $lock ? \rewind($this->res) : \fclose($this->res);
55
56
        $data = \unserialize($content, ['allow_classes' => false]);
57
        return \is_array($data) ? $data : [];
58
    }
59
60
    /**
61
     * @param array $collectionData
62
     */
63
    public function save(array $collectionData)
64
    {
65
        \fwrite($this->res, \serialize($collectionData));
66
        \flock($this->res, \LOCK_UN);
67
        \fclose($this->res);
68
    }
69
70
    /**
71
     */
72
    public function drop()
73
    {
74
        if (\is_resource($this->res)) {
75
            \flock($this->res, \LOCK_UN);
76
            \fclose($this->res);
77
        }
78
        @\unlink($this->file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

78
        /** @scrutinizer ignore-unhandled */ @\unlink($this->file);

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...
79
    }
80
}
81