TaggablePool   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 0
loc 95
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A setItemProperties() 0 7 1
A getMultipleByTag() 0 13 3
A clearByTags() 0 4 1
A setTags() 0 6 1
A getTags() 0 4 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\SimpleCache\PsrSimpleCache;
14
15
use Apix\Cache\Adapter as CacheAdapter;
16
use Psr\Cache\CacheItemInterface as ItemInterface;
17
use Psr\Cache\CacheItemPoolInterface as CacheItemPool;
18
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
19
20
use Apix\Cache\PsrCache\Item as CacheItem;
21
22
class TaggablePool extends Pool
23
{
24
25
    /**
26
     * The tags associated with this pool.
27
     * @var array|null
28
     */
29
    protected $tags = null;
30
31
    /**
32
     * @var CacheAdapter
33
     */
34
    protected $cache_adapter;
35
36
    /**
37
     * Constructor.
38
     */
39 7
    public function __construct(CacheItemPool $cache_item_pool)
40
    {
41 7
        parent::__construct($cache_item_pool);
42
43 7
        $this->cache_adapter = $this->cache_item_pool->getCacheAdapter();
44
45
        $options = array(
46
            'tag_enable' => true // wether to enable tagging
47 7
        );
48
49 7
        $this->cache_adapter->setOptions($options);
50 7
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    protected function setItemProperties(
56
        CacheItem $item, $value, $ttl = null
57
    ) {
58 2
        return $item->set($value)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Apix\Cache\PsrCache\Item as the method setTags() does only exist in the following sub-classes of Apix\Cache\PsrCache\Item: Apix\Cache\PsrCache\TaggableItem. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59 2
                    ->expiresAfter($ttl)
60 2
                    ->setTags($this->tags);
61
    }
62
63
    /**
64
     * Retrieves the cache keys for the given tag.
65
     *
66
     * @param  string $tag The cache tag to retrieve.
67
     * @return array  Returns an array of cache keys.
68
     */
69 2
    public function getMultipleByTag($tag)
70
    {
71 2
        $keys = $this->cache_adapter->loadTag($tag);
72 2
        $items = array();
73 2
        if ($keys) {
74 1
            foreach ($keys as $key) {
75 1
                $k = $this->cache_adapter->removePrefixKey($key);
76 1
                $items[$k] = $this->get($k);
77 1
            }
78 1
        }
79
80 2
        return $items;
81
    }
82
83
    /**
84
     * Removes all the cached entries associated with the given tag names.
85
     *
86
     * @param  array $tags An array of tag names (string).
87
     * @return bool
88
     */
89 2
    public function clearByTags(array $tags)
90
    {
91 2
        return $this->cache_adapter->clean($tags);
92
    }
93
94
    /**
95
     * Sets this pool tags.
96
     *
97
     * @param  array|null   $tags
98
     * @return TaggableItem The invoked object.
99
     */
100 5
    public function setTags(array $tags=null)
101
    {
102 5
        $this->tags = $tags;
103
104 5
        return $this;
105
    }
106
107
    /**
108
     * Returns this pool tags.
109
     *
110
     * @return array|null
111
     */
112 1
    public function getTags()
113
    {
114 1
        return $this->tags;
115
    }
116
}
117