Completed
Push — master ( 730f80...c057de )
by Lars
02:24
created

AdapterOpCache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 5.98 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80.48%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 7
loc 117
ccs 33
cts 41
cp 0.8048
rs 10
c 0
b 0
f 0

4 Methods

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

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 24
    public function __construct($cacheDir = null)
25
    {
26 24
        parent::__construct($cacheDir);
27
28 24
        $this->serializer = new SerializerNo();
29
30 24
        if (self::$hasCompileFileFunction === null) {
31
            /** @noinspection PhpComposerExtensionStubsInspection */
32
            /** @noinspection PhpUsageOfSilenceOperatorInspection */
33
            self::$hasCompileFileFunction = (
34 1
                \function_exists('opcache_compile_file')
35
                &&
36 1
                !empty(@\opcache_get_status())
37
            );
38
        }
39 24
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 25
    public function get(string $key)
45
    {
46 25
        $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 25
            \file_exists($path) === false
50
            ||
51 25
            \filesize($path) === 0
52
        ) {
53 11
            return null;
54
        }
55
56
        /** @noinspection PhpIncludeInspection */
57 16
        $data = include $path;
58
59 16
        if (!$data || !$this->validateDataFromCache($data)) {
60
            return null;
61
        }
62
63 16
        if ($this->ttlHasExpired($data['ttl']) === true) {
64 4
            $this->remove($key);
65
66 4
            return null;
67
        }
68
69 14
        return $data['value'];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 41
    protected function getFileName(string $key): string
76
    {
77 41
        return $this->cacheDir . \DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @noinspection PhpUndefinedClassInspection
84
     * @noinspection PhpUndefinedNamespaceInspection
85
     * @noinspection BadExceptionsProcessingInspection
86
     */
87 22
    public function setExpired(string $key, $value, int $ttl = 0): bool
88
    {
89
        $item = [
90 22
            'value' => $value,
91 22
            'ttl'   => $ttl ? $ttl + \time() : 0,
92
        ];
93 22
        if (\class_exists('\Symfony\Component\VarExporter\VarExporter')) {
94
            try {
95
                $content = \Symfony\Component\VarExporter\VarExporter::export($item);
96
            } catch (\Symfony\Component\VarExporter\Exception\ExceptionInterface $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\VarExp...tion\ExceptionInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
97
                $content = \var_export($item, true);
98
            }
99
        } else {
100 22
            $content = \var_export($item, true);
101
        }
102
103 22
        $content = '<?php return ' . $content . ';';
104
105 22
        $cacheFile = $this->getFileName($key);
106
107 22
        $result = $this->writeFile(
108 22
            $cacheFile,
109 22
            $content
110
        );
111
112
        if (
113 22
            $result === true
114
            &&
115 22
            self::$hasCompileFileFunction === true
116
        ) {
117
            // opcache will only compile and cache files older than the script execution start.
118
            // set a date before the script execution date, then opcache will compile and cache the generated file.
119
            /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
120
            \touch($cacheFile, \time() - 86400);
121
122
            /** @noinspection PhpComposerExtensionStubsInspection */
123
            \opcache_invalidate($cacheFile);
124
            /** @noinspection PhpComposerExtensionStubsInspection */
125
            \opcache_compile_file($cacheFile);
126
        }
127
128 22
        return $result;
129
    }
130
}
131