Issues (36)

lib/Doctrine/Common/Cache/FilesystemCache.php (2 issues)

1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use const PHP_EOL;
6
use function fclose;
7
use function fgets;
8
use function fopen;
9
use function is_file;
10
use function serialize;
11
use function time;
12
use function unserialize;
13
14
/**
15
 * Filesystem cache driver.
16
 */
17
class FilesystemCache extends FileCache
18
{
19
    public const EXTENSION = '.doctrinecache.data';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 83
    public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
25
    {
26 83
        parent::__construct($directory, $extension, $umask);
27 83
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 81
    protected function doFetch($id)
33
    {
34 81
        $data     = '';
35 81
        $lifetime = -1;
36 81
        $filename = $this->getFilename($id);
37
38 81
        if (! is_file($filename)) {
39 81
            return false;
40
        }
41
42 68
        $resource = fopen($filename, 'r');
43 68
        $line     = fgets($resource);
0 ignored issues
show
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

43
        $line     = fgets(/** @scrutinizer ignore-type */ $resource);
Loading history...
44
45 68
        if ($line !== false) {
46 68
            $lifetime = (int) $line;
47
        }
48
49 68
        if ($lifetime !== 0 && $lifetime < time()) {
50
            fclose($resource);
51
52
            return false;
53
        }
54
55 68
        while (($line = fgets($resource)) !== false) {
56 68
            $data .= $line;
57
        }
58
59 68
        fclose($resource);
60
61 68
        return unserialize($data);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 72
    protected function doContains($id)
68
    {
69 72
        $lifetime = -1;
70 72
        $filename = $this->getFilename($id);
71
72 72
        if (! is_file($filename)) {
73 51
            return false;
74
        }
75
76 67
        $resource = fopen($filename, 'r');
77 67
        $line     = fgets($resource);
0 ignored issues
show
It seems like $resource can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

77
        $line     = fgets(/** @scrutinizer ignore-type */ $resource);
Loading history...
78
79 67
        if ($line !== false) {
80 67
            $lifetime = (int) $line;
81
        }
82
83 67
        fclose($resource);
84
85 67
        return $lifetime === 0 || $lifetime > time();
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 79
    protected function doSave($id, $data, $lifeTime = 0)
92
    {
93 79
        if ($lifeTime > 0) {
94 3
            $lifeTime = time() + $lifeTime;
95
        }
96
97 79
        $data     = serialize($data);
98 79
        $filename = $this->getFilename($id);
99
100 79
        return $this->writeFile($filename, $lifeTime . PHP_EOL . $data);
101
    }
102
}
103