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

MemcachedFactory::getModuleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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 MemcachedFactory extends AbstractFactory
9
{
10
    const MODULE_NAME = 'memcached';
11
12
    /**
13
     * @return string
14
     */
15
    public function getModuleName()
16
    {
17
        return self::MODULE_NAME;
18
    }
19
20
    /**
21
     * @return \Memcache
22
     */
23
    public function getMemcachedAdapter()
24
    {
25
        return new \Memcached();
26
    }
27
28
    /**
29
     * @param CacheProvider $cacheProvider
30
     *
31
     * @return CacheProvider
32
     */
33
    protected function decorateWithConnectable(CacheProvider $cacheProvider)
34
    {
35
        $memcached = $this->getMemcachedAdapter();
36
        $settings = $this->config->getSettings();
37
        $memcached->addserver($settings['host'], $settings['port']);
38
39
        $cacheProvider->setMemcached($memcached);
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 setMemcached() does only exist in the following sub-classes of Doctrine\Common\Cache\CacheProvider: Doctrine\Common\Cache\MemcachedCache. 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...
40
41
        return $cacheProvider;
42
    }
43
44
    /**
45
     * @param Config $config
46
     *
47
     * @return bool
48
     */
49
    protected function isValidConfig(Config $config)
50
    {
51
        $settings = $config->getSettings();
52
        if (!isset($settings['host'])) {
53
            return false;
54
        }
55
56
        if (!isset($settings['port'])) {
57
            return false;
58
        }
59
60
        return true;
61
    }
62
}
63