GoAopExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 7
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespace() 0 4 1
A getXsdValidationBasePath() 0 4 1
A load() 0 35 5
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 Go\Aop\Aspect;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
20
class GoAopExtension extends Extension
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getNamespace()
26
    {
27
        return 'http://go.aopphp.com/xsd-schema/go-aop-bundle';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getXsdValidationBasePath()
34
    {
35
        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...
36
    }
37
38
    /**
39
     * Loads a specific configuration.
40
     *
41
     * @param array $config An array of configuration values
42
     * @param ContainerBuilder $container A ContainerBuilder instance
43
     *
44
     * @throws \InvalidArgumentException When provided tag is not defined in this extension
45
     *
46
     * @api
47
     */
48
    public function load(array $config, ContainerBuilder $container)
49
    {
50
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
51
        $loader->load('services.xml');
52
        $loader->load('commands.xml');
53
54
        $configurator = new Configuration();
55
        $config       = $this->processConfiguration($configurator, $config);
56
57
        $normalizedOptions = array();
58
        foreach ($config['options'] as $optionKey => $value) {
59
            // this will convert 'under_scores' into 'underScores'
60
            $optionKey = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $optionKey))));
61
            $normalizedOptions[$optionKey] = $value;
62
        }
63
        $container->setParameter('goaop.options', $normalizedOptions);
64
65
        if ($config['cache_warmer']) {
66
            $definition = $container->getDefinition('goaop.cache.warmer');
67
            $definition->addTag('kernel.cache_warmer');
68
        }
69
70
        if ($config['doctrine_support']) {
71
            $container
72
                ->getDefinition('goaop.bridge.doctrine.metadata_load_interceptor')
73
                ->addTag('doctrine.event_subscriber');
74
        }
75
76
        // Service autoconfiguration is available in Symfony 3.3+
77
        if (method_exists($container, 'registerForAutoconfiguration')) {
78
            $container
79
                ->registerForAutoconfiguration(Aspect::class)
80
                ->addTag('goaop.aspect');
81
        }
82
    }
83
}
84