Issues (19)

src/Cache/FileCache.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata\Cache;
6
7
use Metadata\ClassMetadata;
8
9
class FileCache implements CacheInterface, ClearableCacheInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $dir;
15
16 4
    public function __construct(string $dir)
17
    {
18 4
        if (!is_dir($dir) && false === @mkdir($dir, 0777, true)) {
19
            throw new \InvalidArgumentException(sprintf('Can\'t create directory for cache at "%s"', $dir));
20
        }
21
22 4
        $this->dir = rtrim($dir, '\\/');
23 4
    }
24
25 4
    public function load(string $class): ?ClassMetadata
26
    {
27 4
        $path = $this->getCachePath($class);
28 4
        if (!is_readable($path)) {
29 2
            return null;
30
        }
31
32
        try {
33 4
            $metadata = include $path;
34 3
            if ($metadata instanceof ClassMetadata) {
35 3
                return $metadata;
36
            }
37
38
            // if the file does not return anything, the return value is integer `1`.
39 1
        } catch (\Error $e) {
40
            // ignore corrupted cache
41
        }
42
43 2
        return null;
44
    }
45
46 2
    public function put(ClassMetadata $metadata): void
47
    {
48 2
        if (!is_writable($this->dir)) {
49
            throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $this->dir));
50
        }
51
52 2
        $path = $this->getCachePath($metadata->name);
53
        if (!is_writable(dirname($path))) {
54 2
            throw new \RuntimeException(sprintf('Cache file "%s" is not writable.', $path));
55 2
        }
56
57
        $tmpFile = tempnam($this->dir, 'metadata-cache');
58
        if (false === $tmpFile) {
59
            $this->evict($metadata->name);
60
61 2
            return;
62 2
        }
63
64 2
        $data = '<?php return unserialize(' . var_export(serialize($metadata), true) . ');';
65
        $bytesWritten = file_put_contents($tmpFile, $data);
66
        // use strlen and not mb_strlen. if there is utf8 in the code, it also writes more bytes.
67
        if ($bytesWritten !== strlen($data)) {
68
            @unlink($tmpFile);
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

68
            /** @scrutinizer ignore-unhandled */ @unlink($tmpFile);

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...
69
            // also evict the cache to not use an outdated version.
70
            $this->evict($metadata->name);
71
72
            return;
73 2
        }
74
75 2
        // Let's not break filesystems which do not support chmod.
76 2
        @chmod($tmpFile, 0666 & ~umask());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). 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

76
        /** @scrutinizer ignore-unhandled */ @chmod($tmpFile, 0666 & ~umask());

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...
77
78
        $this->renameFile($tmpFile, $path);
79
    }
80
81 2
    /**
82
     * Renames a file with fallback for windows
83 2
     */
84
    private function renameFile(string $source, string $target): void
85
    {
86
        if (false === @rename($source, $target)) {
87
            if (defined('PHP_WINDOWS_VERSION_BUILD')) {
88
                if (false === copy($source, $target)) {
89
                    throw new \RuntimeException(sprintf('(WIN) Could not write new cache file to %s.', $target));
90
                }
91
92
                if (false === unlink($source)) {
93
                    throw new \RuntimeException(sprintf('(WIN) Could not delete temp cache file to %s.', $source));
94
                }
95
            } else {
96 2
                throw new \RuntimeException(sprintf('Could not write new cache file to %s.', $target));
97
            }
98 2
        }
99
    }
100 2
101 2
    public function evict(string $class): void
102 2
    {
103
        $path = $this->getCachePath($class);
104 2
        if (file_exists($path)) {
105
            @unlink($path);
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

105
            /** @scrutinizer ignore-unhandled */ @unlink($path);

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...
106
        }
107
    }
108
109
    public function clear(): bool
110 4
    {
111
        $result = true;
112 4
        $files = glob($this->dir . '/*.cache.php');
113
        foreach ($files as $file) {
114
            if (is_file($file)) {
115
                $result = $result && @unlink($file);
116
            }
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * This function computes the cache file path.
124
     *
125
     * If anonymous class is to be cached, it contains invalid path characters that need to be removed/replaced
126
     * Example of anonymous class name: class@anonymous\x00/app/src/Controller/DefaultController.php0x7f82a7e026ec
127
     */
128
    private function getCachePath(string $key): string
129
    {
130
        $fileName = str_replace(['\\', "\0", '@', '/', '$', '{', '}', ':'], '-', $key);
131
132
        return $this->dir . '/' . $fileName . '.cache.php';
133
    }
134
}
135