Completed
Push — master ( 9da291...9cb0b0 )
by Pedro
9s
created

MemcacheFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 52
ccs 0
cts 17
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModuleName() 0 4 1
A getMemcacheAdapter() 0 4 1
A decorateWithConnectable() 0 10 1
A isValidConfig() 0 13 3
1
<?php
2
3
namespace Pcelta\Doctrine\Cache\Factory;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
use Pcelta\Doctrine\Cache\Entity\Config;
7
8
class MemcacheFactory extends AbstractFactory
9
{
10
    const MODULE_NAME = 'memcache';
11
12
    public function getModuleName()
13
    {
14
        return self::MODULE_NAME;
15
    }
16
17
    /**
18
     * @return \Memcache
19
     */
20
    public function getMemcacheAdapter()
21
    {
22
        return new \Memcache();
23
    }
24
25
    /**
26
     * @param CacheProvider $cacheProvider
27
     *
28
     * @return CacheProvider
29
     */
30
    protected function decorateWithConnectable(CacheProvider $cacheProvider)
31
    {
32
        $memcache = $this->getMemcacheAdapter();
33
        $settings = $this->config->getSettings();
34
        $memcache->connect($settings['host'], $settings['port']);
35
36
        $cacheProvider->setMemcache($memcache);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Doctrine\Common\Cache\CacheProvider as the method setMemcache() does only exist in the following sub-classes of Doctrine\Common\Cache\CacheProvider: Doctrine\Common\Cache\MemcacheCache. 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...
37
38
        return $cacheProvider;
39
    }
40
41
    /**
42
     * @param Config $config
43
     *
44
     * @return bool
45
     */
46
    protected function isValidConfig(Config $config)
47
    {
48
        $settings = $config->getSettings();
49
        if (!isset($settings['host'])) {
50
            return false;
51
        }
52
53
        if (!isset($settings['port'])) {
54
            return false;
55
        }
56
57
        return true;
58
    }
59
}
60