Completed
Push — master ( a814e0...392de1 )
by Tobias
24:09
created

ChainFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvider() 0 4 1
A configureOptionResolver() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of php-cache organization.
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 Bazinga\Bundle\GeocoderBundle\ProviderFactory;
13
14
use Geocoder\Provider\Chain\Chain;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
/**
18
 * @author Tobias Nyholm <[email protected]>
19
 */
20
final class ChainFactory extends AbstractFactory
21
{
22
    protected static $dependencies = [
23
        ['requiredClass' => Chain::class, 'packageName' => 'geocoder-php/chain-provider'],
24
    ];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function getProvider(array $config)
30
    {
31
        return new Chain($config['services']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Geocoder\Pro...n($config['services']); (Geocoder\Provider\Chain\Chain) is incompatible with the return type declared by the abstract method Bazinga\Bundle\GeocoderB...actFactory::getProvider of type Geocoder\Provider\Provider.

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...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected static function configureOptionResolver(OptionsResolver $resolver)
38
    {
39
        parent::configureOptionResolver($resolver);
40
41
        $resolver->setRequired('services');
42
        $resolver->setAllowedTypes('services', ['array']);
43
    }
44
}
45