Completed
Push — master ( ab024d...ab2ae3 )
by Alexander
20:05 queued 05:06
created

GoAopExtension::getXsdValidationBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Symfony\GoAopBundle\DependencyInjection;
12
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
19
class GoAopExtension extends Extension
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function getNamespace()
25
    {
26
        return 'http://go.aopphp.com/xsd-schema/go-aop-bundle';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getXsdValidationBasePath()
33
    {
34
        return __DIR__ . '/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

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...
35
    }
36
37
    /**
38
     * Loads a specific configuration.
39
     *
40
     * @param array $config An array of configuration values
41
     * @param ContainerBuilder $container A ContainerBuilder instance
42
     *
43
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
44
     *
45
     * @api
46
     */
47
    public function load(array $config, ContainerBuilder $container)
48
    {
49
        $configurator = new Configuration();
50
        $config       = $this->processConfiguration($configurator, $config);
51
52
        $normalizedOptions = array();
53
        foreach ($config['options'] as $optionKey => $value) {
54
            // this will convert 'under_scores' into 'underScores'
55
            $optionKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $optionKey))));
56
            $normalizedOptions[$optionKey] = $value;
57
        }
58
        $container->setParameter('goaop.options', $normalizedOptions);
59
60
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
61
        $loader->load('services.xml');
62
        $loader->load('commands.xml');
63
64
        if ($config['cache_warmer']) {
65
            $definition = $container->getDefinition('goaop.cache.warmer');
66
            $definition->addTag('kernel.cache_warmer');
67
        }
68
69
        if ($config['doctrine_support']) {
70
            $container
71
                ->getDefinition('goaop.bridge.doctrine.metadata_load_interceptor')
72
                ->addTag('doctrine.event_subscriber');
73
        }
74
    }
75
}
76