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
|
|||||
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
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
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.');
}
![]() |
|||||
79 | } |
||||
80 | } |
||||
81 |
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 theid
property of an instance of theAccount
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.