MongoDBFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 37.21 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 16
loc 43
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapter() 0 13 2
A configureOptionResolver() 16 16 1

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 php-cache organization.
5
 *
6
 * (c) 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\AdapterBundle\Factory;
13
14
use Cache\Adapter\MongoDB\MongoDBCachePool;
15
use MongoDB\Driver\Manager;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 * @author Aaron Scherer <[email protected]>
21
 */
22
final class MongoDBFactory extends AbstractDsnAdapterFactory
23
{
24
    protected static $dependencies = [
25
        ['requiredClass' => 'Cache\Adapter\MongoDB\MongoDBCachePool', 'packageName' => 'cache/mongodb-adapter'],
26
    ];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getAdapter(array $config)
32
    {
33
        $dsn = $this->getDsn();
34
        if (empty($dsn)) {
35
            $manager = new Manager(sprintf('mongodb://%s:%s', $config['host'], $config['port']));
36
        } else {
37
            $manager = new Manager($dsn->getDsn());
38
        }
39
40
        $collection = MongoDBCachePool::createCollection($manager, $config['namespace']);
41
42
        return new MongoDBCachePool($collection);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cache\Adapte...CachePool($collection); (Cache\Adapter\MongoDB\MongoDBCachePool) is incompatible with the return type declared by the abstract method Cache\AdapterBundle\Fact...pterFactory::getAdapter of type Psr\Cache\CacheItemPoolInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 View Code Duplication
    protected static function configureOptionResolver(OptionsResolver $resolver)
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...
49
    {
50
        parent::configureOptionResolver($resolver);
51
52
        $resolver->setDefaults(
53
            [
54
                'host' => '127.0.0.1',
55
                'port' => 11211,
56
                'namespace' => 'cache',
57
            ]
58
        );
59
60
        $resolver->setAllowedTypes('host', ['string']);
61
        $resolver->setAllowedTypes('port', ['string', 'int']);
62
        $resolver->setAllowedTypes('namespace', ['string']);
63
    }
64
}
65