Test Setup Failed
Push — master ( fbf53c...9e9e3d )
by Asmir
03:17
created

FileCache::evict()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Metadata\Cache;
4
5
use Metadata\ClassMetadata;
0 ignored issues
show
introduced by
Copyright notice missing
Loading history...
6
7
class FileCache implements CacheInterface
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
8
{
9
    private $dir;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
10
11
    public function __construct(string $dir)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
12
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
13
        if (!is_dir($dir)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
14
            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
15
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
16
        if (!is_writable($dir)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
17
            throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $dir));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
18
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
19
20
        $this->dir = rtrim($dir, '\\/');
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
21
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
22
23
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$class" missing
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
24
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
25
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
26
    public function load(string $class): ?ClassMetadata
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
27
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
28
        $path = $this->dir . '/' . strtr($class, '\\', '-') . '.cache.php';
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
29
        if (!file_exists($path)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
30
            return null;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
31
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
32
33
        return include $path;
0 ignored issues
show
introduced by
Including files with "include" is not allowed; use "require_once" instead
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
34
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
35
36
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$metadata" missing
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
37
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
38
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
39
    public function put(ClassMetadata $metadata): void
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
40
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
41
        $path = $this->dir . '/' . strtr($metadata->name, '\\', '-') . '.cache.php';
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
42
43
        $tmpFile = tempnam($this->dir, 'metadata-cache');
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
44
        file_put_contents($tmpFile, '<?php return unserialize(' . var_export(serialize($metadata), true) . ');');
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
45
46
        // Let's not break filesystems which do not support chmod.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
47
        @chmod($tmpFile, 0666 & ~umask());
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...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
48
49
        $this->renameFile($tmpFile, $path);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
50
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
51
52
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
53
     * Renames a file with fallback for windows
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
54
     *
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
55
     * @param string $source
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
56
     * @param string $target
0 ignored issues
show
introduced by
Missing parameter comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
57
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
58
    private function renameFile($source, $target)
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
59
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
60
        if (false === @rename($source, $target)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
61
            if (defined('PHP_WINDOWS_VERSION_BUILD')) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
62
                if (false === copy($source, $target)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
63
                    throw new \RuntimeException(sprintf('(WIN) Could not write new cache file to %s.', $target));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
64
                }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
65
                if (false === unlink($source)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
66
                    throw new \RuntimeException(sprintf('(WIN) Could not delete temp cache file to %s.', $source));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
67
                }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
68
            } else {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
69
                throw new \RuntimeException(sprintf('Could not write new cache file to %s.', $target));
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
70
            }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
71
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
72
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
73
74
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$class" missing
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
75
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
76
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
77
    public function evict(string $class): void
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
78
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
79
        $path = $this->dir . '/' . strtr($class, '\\', '-') . '.cache.php';
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
80
        if (file_exists($path)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
81
            unlink($path);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
82
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
83
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
84
}
85