Completed
Push — master ( 48ba4b...58336c )
by Lars
02:26 queued 12s
created

AdapterOpCache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 6.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.19%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 7
loc 104
ccs 33
cts 37
cp 0.8919
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
B get() 7 27 6
A getFileName() 0 4 1
A setExpired() 0 39 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 11
    public function __construct($cacheDir = null)
25
    {
26 11
        parent::__construct($cacheDir);
27
28 11
        $this->serializer = new SerializerNo();
29
30 11
        if (self::$hasCompileFileFunction === null) {
31
            /** @noinspection PhpComposerExtensionStubsInspection */
32 1
            self::$hasCompileFileFunction = \function_exists('opcache_compile_file') && !empty(\opcache_get_status());
33
        }
34 11
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 6
    public function get(string $key)
40
    {
41 6
        $path = $this->getFileName($key);
42
43 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...
44 6
        \file_exists($path) === false
45
        ||
46 6
        \filesize($path) === 0
47
    ) {
48 1
            return null;
49
        }
50
51
        /** @noinspection PhpIncludeInspection */
52 6
        $data = include $path;
53
54 6
        if (!$data || !$this->validateDataFromCache($data)) {
55
            return null;
56
        }
57
58 6
        if ($this->ttlHasExpired($data['ttl']) === true) {
59 2
            $this->remove($key);
60
61 2
            return null;
62
        }
63
64 5
        return $data['value'];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 8
    protected function getFileName(string $key): string
71
    {
72 8
        return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 6
    public function setExpired(string $key, $value, int $ttl = 0): bool
79
    {
80
        $item = [
81 6
            'value' => $value,
82 6
            'ttl'   => $ttl ? $ttl + \time() : 0,
83
        ];
84 6
        $content = \var_export($item, true);
85
86
        $content = '<?php 
87 6
    return ' . $content . ';
88
    ';
89
90 6
        $cacheFile = $this->getFileName($key);
91
92 6
        $result = (bool) \file_put_contents(
93 6
        $cacheFile,
94 6
        $content,
95 6
        0,
96 6
        $this->getContext()
97
    );
98
99
        if (
100 6
        $result === true
101
        &&
102 6
        self::$hasCompileFileFunction === true
103
    ) {
104
            // opcache will only compile and cache files older than the script execution start.
105
            // set a date before the script execution date, then opcache will compile and cache the generated file.
106
            /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
107
            \touch($cacheFile, \time() - 86400);
108
109
            /** @noinspection PhpComposerExtensionStubsInspection */
110
            \opcache_invalidate($cacheFile);
111
            /** @noinspection PhpComposerExtensionStubsInspection */
112
            \opcache_compile_file($cacheFile);
113
        }
114
115 6
        return $result;
116
    }
117
}
118