Passed
Branch master (79d24d)
by Johannes
02:27
created

FileCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 20.78 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 2 Features 2
Metric Value
wmc 13
c 10
b 2
f 2
lcom 1
cbo 1
dl 16
loc 77
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A loadClassMetadataFromCache() 9 9 2
A putClassMetadataInCache() 0 12 1
B renameFile() 0 14 5
A evictClassMetadataFromCache() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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($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 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...
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->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...
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 putClassMetadataInCache(ClassMetadata $metadata)
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
Concat operator must be surrounded by spaces.
Loading history...
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
Concat operator must be surrounded by spaces.
Loading history...
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
        // 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
        if (false === @rename($source, $target)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
60
            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...
61
                if (false === copy($source, $target)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
62
                    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...
63
                }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
64
                if (false === unlink($source)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
65
                    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...
66
                }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
67
            } else {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
68
                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...
69
            }
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
Doc comment for parameter "$class" missing
Loading history...
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
74
     * {@inheritDoc}
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
75
     */
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...
76 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...
77
    {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
78
        $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...
79
        if (file_exists($path)) {
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
80
            unlink($path);
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
    }
0 ignored issues
show
introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
83
}
84