Completed
Push — v5 ( 03de98...68a851 )
by Georges
02:50
created

ExtendedCacheItemPoolTrait   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 242
Duplicated Lines 46.28 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 112
loc 242
rs 8.2608
wmc 40
lcom 1
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getItemsAsJsonString() 0 4 1
A getItemsByTag() 0 15 3
A getItemsByTags() 0 9 2
A getItemsByTagsAsJsonString() 0 4 1
A deleteItemsByTag() 0 16 4
A deleteItemsByTags() 12 12 3
A incrementItemsByTag() 12 13 4
A incrementItemsByTags() 12 12 3
A decrementItemsByTag() 12 13 4
A decrementItemsByTags() 12 12 3
A appendItemsByTag() 12 13 3
A appendItemsByTags() 12 12 3
A prependItemsByTag() 12 13 3
A prependItemsByTags() 12 12 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ExtendedCacheItemPoolTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ExtendedCacheItemPoolTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Core;
16
17
use InvalidArgumentException;
18
19
trait ExtendedCacheItemPoolTrait
20
{
21
    /**
22
     * @param array $keys
23
     * An indexed array of keys of items to retrieve.
24
     * @param int $option json_encode() options
25
     * @param int $depth json_encode() depth
26
     * @return string
27
     * @throws \InvalidArgumentException
28
     */
29
    public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512)
30
    {
31
        return json_encode(array_values($this->getItems($keys)), $option, $depth);
0 ignored issues
show
Bug introduced by
The method getItems() does not exist on phpFastCache\Core\ExtendedCacheItemPoolTrait. Did you maybe mean getItemsAsJsonString()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
32
    }
33
34
    /**
35
     * @param string $tagName
36
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
37
     * @throws InvalidArgumentException
38
     */
39
    public function getItemsByTag($tagName)
40
    {
41
        if (is_string($tagName)) {
42
            $driverResponse = $this->driverRead($this->getTagKey($tagName));
43
            if ($driverResponse) {
44
                $items = (array) $this->driverUnwrapData($driverResponse);
45
46
                return $this->getItems(array_unique(array_keys($items)));
0 ignored issues
show
Bug introduced by
The method getItems() does not exist on phpFastCache\Core\ExtendedCacheItemPoolTrait. Did you maybe mean getItemsAsJsonString()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
            } else {
48
                return [];
49
            }
50
        } else {
51
            throw new InvalidArgumentException('$tagName must be a string');
52
        }
53
    }
54
55
    /**
56
     * @param array $tagNames
57
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
58
     * @throws InvalidArgumentException
59
     */
60
    public function getItemsByTags(array $tagNames)
61
    {
62
        $items = [];
63
        foreach (array_unique($tagNames) as $tagName) {
64
            $items = array_merge($items, $this->getItemsByTag($tagName));
65
        }
66
67
        return $items;
68
    }
69
70
    /**
71
     * Returns A json string that represents an array of items by tags-based.
72
     *
73
     * @param array $tagNames
74
     * An indexed array of keys of items to retrieve.
75
     * @param int $option json_encode() options
76
     * @param int $depth json_encode() depth
77
     *
78
     * @throws InvalidArgumentException
79
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
80
     *   MUST be thrown.
81
     *
82
     * @return string
83
     */
84
    public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512)
85
    {
86
        return json_encode(array_values($this->getItemsByTags($tagNames)), $option, $depth);
87
    }
88
89
    /**
90
     * @param string $tagName
91
     * @return bool|null
92
     * @throws InvalidArgumentException
93
     */
94
    public function deleteItemsByTag($tagName)
95
    {
96
        if (is_string($tagName)) {
97
            $return = null;
98
            foreach ($this->getItemsByTag($tagName) as $item) {
99
                $result = $this->driverDelete($item);
100
                if ($return !== false) {
101
                    $return = $result;
102
                }
103
            }
104
105
            return $return;
106
        } else {
107
            throw new InvalidArgumentException('$tagName must be a string');
108
        }
109
    }
110
111
    /**
112
     * @param array $tagNames
113
     * @return bool|null
114
     * @throws InvalidArgumentException
115
     */
116 View Code Duplication
    public function deleteItemsByTags(array $tagNames)
1 ignored issue
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...
117
    {
118
        $return = null;
119
        foreach ($tagNames as $tagName) {
120
            $result = $this->deleteItemsByTag($tagName);
121
            if ($return !== false) {
122
                $return = $result;
123
            }
124
        }
125
126
        return $return;
127
    }
128
129
    /**
130
     * @inheritdoc
131
     */
132 View Code Duplication
    public function incrementItemsByTag($tagName, $step = 1)
1 ignored issue
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...
133
    {
134
        if (is_string($tagName) && is_int($step)) {
135
            foreach ($this->getItemsByTag($tagName) as $item) {
136
                $item->increment($step);
137
                $this->saveDeferred($item);
138
            }
139
140
            return $this->commit();
141
        } else {
142
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
143
        }
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 View Code Duplication
    public function incrementItemsByTags(array $tagNames, $step = 1)
1 ignored issue
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...
150
    {
151
        $return = null;
152
        foreach ($tagNames as $tagName) {
153
            $result = $this->incrementItemsByTag($tagName, $step);
154
            if ($return !== false) {
155
                $return = $result;
156
            }
157
        }
158
159
        return $return;
160
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165 View Code Duplication
    public function decrementItemsByTag($tagName, $step = 1)
1 ignored issue
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...
166
    {
167
        if (is_string($tagName) && is_int($step)) {
168
            foreach ($this->getItemsByTag($tagName) as $item) {
169
                $item->decrement($step);
170
                $this->saveDeferred($item);
171
            }
172
173
            return $this->commit();
174
        } else {
175
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
176
        }
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182 View Code Duplication
    public function decrementItemsByTags(array $tagNames, $step = 1)
1 ignored issue
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...
183
    {
184
        $return = null;
185
        foreach ($tagNames as $tagName) {
186
            $result = $this->decrementItemsByTag($tagName, $step);
187
            if ($return !== false) {
188
                $return = $result;
189
            }
190
        }
191
192
        return $return;
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198 View Code Duplication
    public function appendItemsByTag($tagName, $data)
1 ignored issue
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...
199
    {
200
        if (is_string($tagName)) {
201
            foreach ($this->getItemsByTag($tagName) as $item) {
202
                $item->append($data);
203
                $this->saveDeferred($item);
204
            }
205
206
            return $this->commit();
207
        } else {
208
            throw new InvalidArgumentException('$tagName must be a string');
209
        }
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215 View Code Duplication
    public function appendItemsByTags(array $tagNames, $data)
1 ignored issue
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...
216
    {
217
        $return = null;
218
        foreach ($tagNames as $tagName) {
219
            $result = $this->decrementItemsByTag($tagName, $data);
220
            if ($return !== false) {
221
                $return = $result;
222
            }
223
        }
224
225
        return $return;
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231 View Code Duplication
    public function prependItemsByTag($tagName, $data)
1 ignored issue
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...
232
    {
233
        if (is_string($tagName)) {
234
            foreach ($this->getItemsByTag($tagName) as $item) {
235
                $item->prepend($data);
236
                $this->saveDeferred($item);
237
            }
238
239
            return $this->commit();
240
        } else {
241
            throw new InvalidArgumentException('$tagName must be a string');
242
        }
243
    }
244
245
    /**
246
     * @inheritdoc
247
     */
248 View Code Duplication
    public function prependItemsByTags(array $tagNames, $data)
1 ignored issue
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...
249
    {
250
        $return = null;
251
        foreach ($tagNames as $tagName) {
252
            $result = $this->decrementItemsByTag($tagName, $data);
253
            if ($return !== false) {
254
                $return = $result;
255
            }
256
        }
257
258
        return $return;
259
    }
260
}