Completed
Pull Request — master (#62)
by
unknown
06:14
created

FileCache::createDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
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
    /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
10
     * @var string
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
11
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
12
    private $dir;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
13
14
    /**
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
15
     * FileCache constructor.
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
16
     *
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
17
     * @param string $dir
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...
18
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
19
    public function __construct($dir)
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
20
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
21
        if (!is_dir($dir)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
22
            $this->createDirectory($dir);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
23
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
24
25
        $this->dir = rtrim($dir, '\\/');
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
26
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
27
28
    /**
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...
29
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
30
     */
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...
31 View Code Duplication
    public function loadClassMetadataFromCache(\ReflectionClass $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
32
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
33
        $path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php';
0 ignored issues
show
introduced by
Concat operator must be surrounded by spaces.
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
34
        if (!file_exists($path)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
35
            return null;
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
36
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
37
38
        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...
39
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
40
41
    /**
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...
42
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
43
     */
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...
44
    public function putClassMetadataInCache(ClassMetadata $metadata)
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
45
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
46
        $path = $this->getFileName($metadata);
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
47
48
        if (!is_writable(dirname($path))) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
49
            throw new \RuntimeException("Cache file {$path} is not writable.");
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
        if (false === (@file_put_contents($path,
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
53
                '<?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...
54
        )) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
55
            throw new \RuntimeException("Can't not write new cache file to {$path}");
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
56
        };
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
57
58
        // 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...
59
        @chmod($path, 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...
60
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
61
62
    /**
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...
63
     * {@inheritDoc}
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
Missing @return tag in function comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
65 View Code Duplication
    public function evictClassMetadataFromCache(\ReflectionClass $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
66
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
67
        $path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php';
0 ignored issues
show
introduced by
Concat operator must be surrounded by spaces.
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
68
        if (file_exists($path)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
69
            unlink($path);
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
73
    /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
74
     * @param ClassMetadata $metadata
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...
75
     *
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
76
     * @return string
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
77
     */
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
78
    private function getFileName(ClassMetadata $metadata)
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
79
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
80
        return $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...
81
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
82
83
    /**
0 ignored issues
show
introduced by
Missing short description in doc comment
Loading history...
introduced by
Doc comment for parameter "$dir" missing
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
84
     * @param $dir
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
85
     */
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...
86
    private function createDirectory($dir)
0 ignored issues
show
introduced by
Type hint "dir" missing for
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
87
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
88
        if (false === (@mkdir($dir, 0775, true))) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
89
            throw new \RuntimeException("Can't create directory for cache at {$dir}");
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
90
        }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
91
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
92
}
93