Completed
Push — master ( 0f341b...3e6c76 )
by Tobias
21:32
created

TaggablePool::invalidateTag()   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\Cache\Recording;
13
14
use Cache\TagInterop\TaggableCacheItemPoolInterface;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class TaggablePool extends CachePool implements TaggableCacheItemPoolInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 View Code Duplication
    public function invalidateTag($tag)
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...
25
    {
26
        $event = $this->start(__FUNCTION__, $tag);
27
        try {
28
            return $event->result = $this->pool->invalidateTag($tag);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Cache\CacheItemPoolInterface as the method invalidateTag() does only exist in the following implementations of said interface: Cache\Adapter\Common\AbstractCachePool, Cache\Adapter\PHPArray\ArrayCachePool, Cache\CacheBundle\Cache\FixedTaggingCachePool, Cache\CacheBundle\Cache\...ierarchyAndTaggablePool, Cache\CacheBundle\Cache\Recording\TaggablePool, Cache\Taggable\TaggablePSR6PoolAdapter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
29
        } finally {
30
            $event->end = microtime(true);
31
        }
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 View Code Duplication
    public function invalidateTags(array $tags)
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...
38
    {
39
        $event = $this->start(__FUNCTION__, $tags);
40
        try {
41
            return $event->result = $this->pool->invalidateTags($tags);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Cache\CacheItemPoolInterface as the method invalidateTags() does only exist in the following implementations of said interface: Cache\Adapter\Common\AbstractCachePool, Cache\Adapter\PHPArray\ArrayCachePool, Cache\CacheBundle\Cache\FixedTaggingCachePool, Cache\CacheBundle\Cache\...ierarchyAndTaggablePool, Cache\CacheBundle\Cache\Recording\TaggablePool, Cache\Taggable\TaggablePSR6PoolAdapter, Symfony\Component\Cache\Adapter\TagAwareAdapter.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
42
        } finally {
43
            $event->end = microtime(true);
44
        }
45
    }
46
}
47