Completed
Push — master ( 496bc5...15446f )
by Lars
02:20
created

AdapterOpCache   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 2
dl 7
loc 77
ccs 24
cts 25
cp 0.96
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B get() 7 27 6
A getFileName() 0 6 1
A setExpired() 0 20 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
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
 * @package voku\cache
15
 */
16
class AdapterOpCache extends AdapterFile
17
{
18
  /**
19
   * {@inheritdoc}
20
   */
21 11
  public function __construct($cacheDir = null)
22
  {
23 11
    parent::__construct($cacheDir);
24
25 11
    $this->serializer = new SerializerNo();
26 11
  }
27
28
  /**
29
   * {@inheritdoc}
30
   */
31 6
  public function get(string $key)
32
  {
33 6
    $path = $this->getFileName($key);
34
35 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...
36 6
        \file_exists($path) === false
37
        ||
38 6
        \filesize($path) === 0
39
    ) {
40 1
      return null;
41
    }
42
43
    /** @noinspection PhpIncludeInspection */
44 6
    $data = include $path;
45
46 6
    if (!$data || !$this->validateDataFromCache($data)) {
47
      return null;
48
    }
49
50 6
    if ($this->ttlHasExpired($data['ttl']) === true) {
51 2
      $this->remove($key);
52
53 2
      return null;
54
    }
55
56 5
    return $data['value'];
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 8
  protected function getFileName(string $key): string
63
  {
64 8
    $result = $this->cacheDir . DIRECTORY_SEPARATOR . self::CACHE_FILE_PREFIX . $key . '.php';
65
66 8
    return $result;
67
  }
68
69
  /**
70
   * {@inheritdoc}
71
   */
72 6
  public function setExpired(string $key, $value, int $ttl = 0): bool
73
  {
74
    $item = [
75 6
        'value' => $value,
76 6
        'ttl'   => $ttl ? $ttl + \time() : 0,
77
    ];
78 6
    $content = \var_export($item, true);
79
80
    $content = '<?php
81
    
82
    static $data = [
83 6
      0 => ' . $content . ',
84
    ];
85
    
86
    $result =& $data;
87
    unset($data);
88
    return $result[0];';
89
90 6
    return (bool)\file_put_contents($this->getFileName($key), $content);
91
  }
92
}
93