Completed
Push — v5 ( e7c877...af97c1 )
by Georges
02:33
created

ExtendedCacheItemPoolTrait   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 210
Duplicated Lines 53.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 112
loc 210
rs 8.3999
wmc 38
lcom 1
cbo 1

12 Methods

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

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
 *
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 string $tagName
23
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
24
     * @throws InvalidArgumentException
25
     */
26
    public function getItemsByTag($tagName)
27
    {
28
        if (is_string($tagName)) {
29
            $driverResponse = $this->driverRead($this->getTagKey($tagName));
2 ignored issues
show
Bug introduced by
It seems like getTagKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like driverRead() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
            if ($driverResponse) {
31
                $items = (array) $this->driverUnwrapData($driverResponse);
32
33
                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 getItemsByTag()?

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...
34
            } else {
35
                return [];
36
            }
37
        } else {
38
            throw new InvalidArgumentException('$tagName must be a string');
39
        }
40
    }
41
42
    /**
43
     * @param array $tagNames
44
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
45
     * @throws InvalidArgumentException
46
     */
47
    public function getItemsByTags(array $tagNames)
48
    {
49
        $items = [];
50
        foreach (array_unique($tagNames) as $tagName) {
51
            $items = array_merge($items, $this->getItemsByTag($tagName));
52
        }
53
54
        return $items;
55
    }
56
57
    /**
58
     * @param string $tagName
59
     * @return bool|null
60
     * @throws InvalidArgumentException
61
     */
62
    public function deleteItemsByTag($tagName)
63
    {
64
        if (is_string($tagName)) {
65
            $return = null;
66
            foreach ($this->getItemsByTag($tagName) as $item) {
67
                $result = $this->driverDelete($item);
1 ignored issue
show
Bug introduced by
It seems like driverDelete() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68
                if ($return !== false) {
69
                    $return = $result;
70
                }
71
            }
72
73
            return $return;
74
        } else {
75
            throw new InvalidArgumentException('$tagName must be a string');
76
        }
77
    }
78
79
    /**
80
     * @param array $tagNames
81
     * @return bool|null
82
     * @throws InvalidArgumentException
83
     */
84 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...
85
    {
86
        $return = null;
87
        foreach ($tagNames as $tagName) {
88
            $result = $this->deleteItemsByTag($tagName);
89
            if ($return !== false) {
90
                $return = $result;
91
            }
92
        }
93
94
        return $return;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 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...
101
    {
102
        if (is_string($tagName) && is_int($step)) {
103
            foreach ($this->getItemsByTag($tagName) as $item) {
104
                $item->increment($step);
105
                $this->saveDeferred($item);
1 ignored issue
show
Bug introduced by
It seems like saveDeferred() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
106
            }
107
            return $this->commit();
1 ignored issue
show
Bug introduced by
It seems like commit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
108
        } else {
109
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
110
        }
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 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...
117
    {
118
        $return = null;
119
        foreach ($tagNames as $tagName)
120
        {
121
            $result = $this->incrementItemsByTag($tagName, $step);
122
            if ($return !== false) {
123
                $return = $result;
124
            }
125
        }
126
127
        return $return;
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133 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...
134
    {
135
        if (is_string($tagName) && is_int($step)) {
136
            foreach ($this->getItemsByTag($tagName) as $item) {
137
                $item->decrement($step);
138
                $this->saveDeferred($item);
1 ignored issue
show
Bug introduced by
It seems like saveDeferred() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
139
            }
140
            return $this->commit();
1 ignored issue
show
Bug introduced by
It seems like commit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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 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...
150
    {
151
        $return = null;
152
        foreach ($tagNames as $tagName)
153
        {
154
            $result = $this->decrementItemsByTag($tagName, $step);
155
            if ($return !== false) {
156
                $return = $result;
157
            }
158
        }
159
160
        return $return;
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166 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...
167
    {
168
        if (is_string($tagName)) {
169
            foreach ($this->getItemsByTag($tagName) as $item) {
170
                $item->append($data);
171
                $this->saveDeferred($item);
1 ignored issue
show
Bug introduced by
It seems like saveDeferred() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
172
            }
173
            return $this->commit();
1 ignored issue
show
Bug introduced by
It seems like commit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
174
        } else {
175
            throw new InvalidArgumentException('$tagName must be a string');
176
        }
177
    }
178
179
    /**
180
     * @inheritdoc
181
     */
182 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...
183
    {
184
        $return = null;
185
        foreach ($tagNames as $tagName)
186
        {
187
            $result = $this->decrementItemsByTag($tagName, $data);
188
            if ($return !== false) {
189
                $return = $result;
190
            }
191
        }
192
193
        return $return;
194
    }
195
196
    /**
197
     * @inheritdoc
198
     */
199 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...
200
    {
201
        if (is_string($tagName)) {
202
            foreach ($this->getItemsByTag($tagName) as $item) {
203
                $item->prepend($data);
204
                $this->saveDeferred($item);
1 ignored issue
show
Bug introduced by
It seems like saveDeferred() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
205
            }
206
            return $this->commit();
1 ignored issue
show
Bug introduced by
It seems like commit() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
207
        } else {
208
            throw new InvalidArgumentException('$tagName must be a string');
209
        }
210
    }
211
212
    /**
213
     * @inheritdoc
214
     */
215 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...
216
    {
217
        $return = null;
218
        foreach ($tagNames as $tagName)
219
        {
220
            $result = $this->decrementItemsByTag($tagName, $data);
221
            if ($return !== false) {
222
                $return = $result;
223
            }
224
        }
225
226
        return $return;
227
    }
228
}