Completed
Push — master ( 5621e4...94f5e8 )
by Lars
16:34
created

AdapterOpCache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 6.54 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.74%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 7
loc 107
ccs 35
cts 39
cp 0.8974
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
B get() 7 27 6
A getFileName() 0 4 1
A setExpired() 0 37 4

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
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterOpCache: PHP-OPcache
9
 *
10
 * OPcache improves PHP performance by storing precompiled script bytecode
11
 * in shared memory, thereby removing the need for PHP to load and
12
 * parse scripts on each request.
13
 */
14
class AdapterOpCache extends AdapterFileSimple
15
{
16
    /**
17
     * @var bool
18
     */
19
    private static $hasCompileFileFunction;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 12
    public function __construct($cacheDir = null)
25
    {
26 12
        parent::__construct($cacheDir);
27
28 12
        $this->serializer = new SerializerNo();
29
30 12
        if (self::$hasCompileFileFunction === null) {
31
            /** @noinspection PhpComposerExtensionStubsInspection */
32
            /** @noinspection PhpUsageOfSilenceOperatorInspection */
33 1
            self::$hasCompileFileFunction = (
34 1
                \function_exists('opcache_compile_file')
35
                &&
36 1
                !empty(@\opcache_get_status())
37
            );
38
        }
39 12
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 18
    public function get(string $key)
45
    {
46 18
        $path = $this->getFileName($key);
47
48 View Code Duplication
        if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49 18
            \file_exists($path) === false
50
            ||
51 18
            \filesize($path) === 0
52
        ) {
53 10
            return null;
54
        }
55
56
        /** @noinspection PhpIncludeInspection */
57 9
        $data = include $path;
58
59 9
        if (!$data || !$this->validateDataFromCache($data)) {
60
            return null;
61
        }
62
63 9
        if ($this->ttlHasExpired($data['ttl']) === true) {
64 2
            $this->remove($key);
65
66 2
            return null;
67
        }
68
69 8
        return $data['value'];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 32
    protected function getFileName(string $key): string
76
    {
77 32
        return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 15
    public function setExpired(string $key, $value, int $ttl = 0): bool
84
    {
85
        $item = [
86 15
            'value' => $value,
87 15
            'ttl'   => $ttl ? $ttl + \time() : 0,
88
        ];
89 15
        $content = \var_export($item, true);
90
91 15
        $content = '<?php return ' . $content . ';';
92
93 15
        $cacheFile = $this->getFileName($key);
94
95 15
        $result = (bool) \file_put_contents(
96 15
            $cacheFile,
97 15
            $content,
98 15
            0,
99 15
            $this->getContext()
100
        );
101
102
        if (
103 15
            $result === true
104
            &&
105 15
            self::$hasCompileFileFunction === true
106
        ) {
107
            // opcache will only compile and cache files older than the script execution start.
108
            // set a date before the script execution date, then opcache will compile and cache the generated file.
109
            /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
110
            \touch($cacheFile, \time() - 86400);
111
112
            /** @noinspection PhpComposerExtensionStubsInspection */
113
            \opcache_invalidate($cacheFile);
114
            /** @noinspection PhpComposerExtensionStubsInspection */
115
            \opcache_compile_file($cacheFile);
116
        }
117
118 15
        return $result;
119
    }
120
}
121