GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch v2 (a88462)
by Hilari
02:38
created

ChainCache   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 140
Duplicated Lines 12.86 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 1
cbo 3
dl 18
loc 140
rs 10
ccs 52
cts 52
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A pushCache() 0 6 1
A set() 0 9 3
A has() 0 10 3
A get() 0 13 3
A demand() 0 9 2
A delete() 9 9 3
A flush() 9 9 3
A getTimeToLive() 0 11 3
A populatePreviousCaches() 0 6 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
namespace Cmp\Cache\Backend;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\NotFoundException;
7
use Cmp\Cache\TimeToLiveAwareCache;
8
use Cmp\Cache\Traits\MultiCacheTrait;
9
10
/**
11
 * Class ArrayCache
12
 * 
13
 * A simple backend powered by an array in memory
14
 *
15
 * @package Cmp\Cache\Infrastureture\Backend
16
 */
17
class ChainCache implements Cache
18
{
19
    use MultiCacheTrait;
20
21
    /**
22
     * Stored items
23
     * 
24
     * @var Cache[]
25
     */
26
    private $cache = [];
27
28
    /**
29
     * Pushes a cache in the chain
30
     *
31
     * @param Cache $cache
32
     *
33
     * @return $this
34
     */
35 1
    public function pushCache(Cache $cache)
36
    {
37 1
        $this->cache[] = $cache;
38
39 1
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function set($key, $item, $timeToLive = null)
46
    {
47 1
        $success = true;
48 1
        foreach ($this->cache as $cache) {
49 1
            $success = $success && $cache->set($key, $item, $timeToLive);
50 1
        }
51
52 1
        return $success;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 1
    public function has($key)
59
    {
60 1
        foreach ($this->cache as $cache) {
61 1
            if ($cache->has($key)) {
62 1
                return true;
63
            }
64 1
        }
65
66 1
        return false;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function get($key, $default = null)
73
    {
74 1
        foreach ($this->cache as $index => $cache) {
75 1
            $item = $cache->get($key);
76 1
            if ($item) {
77 1
                $this->populatePreviousCaches($index, $key, $item, $cache->getTimeToLive($key));
78
79 1
                return $item;
80
            }
81 1
        }
82
83 1
        return $default;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function demand($key)
90
    {
91 1
        $item = $this->get($key);
92 1
        if (!$item) {
93 1
            throw new NotFoundException($key);
94
        }
95
96 1
        return $item;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 1 View Code Duplication
    public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
103
    {
104 1
        $success = true;
105 1
        foreach ($this->cache as $cache) {
106 1
            $success = $success && $cache->delete($key);
107 1
        }
108
109 1
        return $success;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1 View Code Duplication
    public function flush()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
    {
117 1
        $success = true;
118 1
        foreach ($this->cache as $cache) {
119 1
            $success = $success && $cache->flush();
120 1
        }
121
122 1
        return $success;
123
    }
124
125
    /**
126
     * Gets the remaining time to live for an item
127
     *
128
     * @param $key
129
     *
130
     * @return int|null
131
     */
132 1
    public function getTimeToLive($key)
133
    {
134 1
        foreach ($this->cache as $cache) {
135 1
            $timeToLive = $cache->getTimeToLive($key);
136 1
            if ($timeToLive) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $timeToLive of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
137 1
                return $timeToLive;
138
            }
139 1
        }
140
141 1
        return null;
142
    }
143
144
    /**
145
     * @param string   $index
146
     * @param string   $key
147
     * @param string   $item
148
     * @param int|null $timeToLive
149
     */
150 1
    private function populatePreviousCaches($index, $key, $item, $timeToLive)
151
    {
152 1
        for (--$index; $index >= 0 ; $index--) {
153 1
            $this->cache[$index]->set($key, $item, $timeToLive);
154 1
        }
155 1
    }
156
}
157