Completed
Push — master ( b7f9bf...48ba4b )
by Lars
02:40
created

AdapterOpCache   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 7
loc 112
ccs 34
cts 38
cp 0.8947
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 6 1
A setExpired() 0 45 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
    $result = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
73
74 8
    return $result;
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80 6
  public function setExpired(string $key, $value, int $ttl = 0): bool
81
  {
82
    $item = [
83 6
        'value' => $value,
84 6
        'ttl'   => $ttl ? $ttl + \time() : 0,
85
    ];
86 6
    $content = \var_export($item, true);
87
88
    $content = '<?php
89
    
90
    static $data = [
91 6
      0 => ' . $content . ',
92
    ];
93
    
94
    $result =& $data;
95
    unset($data);
96
    return $result[0];';
97
98 6
    $cacheFile = $this->getFileName($key);
99
100 6
    $result = (bool)\file_put_contents(
101 6
        $cacheFile,
102 6
        $content,
103 6
        0,
104 6
        $this->getContext()
105
    );
106
107
    if (
108 6
        $result === true
109
        &&
110 6
        self::$hasCompileFileFunction === true
111
    ) {
112
      // opcache will only compile and cache files older than the script execution start.
113
      // set a date before the script execution date, then opcache will compile and cache the generated file.
114
      /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
115
      \touch($cacheFile, \time() - 86400);
116
117
      /** @noinspection PhpComposerExtensionStubsInspection */
118
      \opcache_invalidate($cacheFile);
119
      /** @noinspection PhpComposerExtensionStubsInspection */
120
      \opcache_compile_file($cacheFile);
121
    }
122
123 6
    return $result;
124
  }
125
}
126