Extension   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 137
ccs 49
cts 49
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 4 1
A getNamespace() 0 4 1
A getXsdValidationBasePath() 0 4 1
B load() 0 27 2
A processInjections() 0 14 3
A processFilters() 0 18 4
B processExclusions() 0 30 6
1
<?php
2
/*
3
 * This file is part of the  TraitorBundle, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\Traitor\DependencyInjection;
11
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
15
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
16
17
/**
18
 * Class Extension
19
 *
20
 * @package RunOpenCode\Bundle\Traitor\DependencyInjection
21
 */
22
class Extension extends BaseExtension
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 8
    public function getAlias()
28
    {
29 8
        return 'runopencode_traitor';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 8
    public function getNamespace()
36
    {
37 8
        return 'http://www.runopencode.com/xsd-schema/traitor-bundle';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function getXsdValidationBasePath()
44
    {
45 4
        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...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function load(array $configs, ContainerBuilder $container)
52
    {
53 4
        $configuration = new Configuration();
54
55 4
        $config = $this->processConfiguration($configuration, $configs);
56
57 4
        if ($config['use_common_traits']) {
58
59 1
            $loader = new XmlFileLoader(
60
                $container,
61 1
                new FileLocator(__DIR__.'/../Resources/config')
62
            );
63
64 1
            $loader->load('common-traits.xml');
65
66
            array_map(function($config) use (&$configs) {
67 1
                $configs[] = $config;
68 1
            }, $container->getExtensionConfig($this->getAlias()));
69
70 1
            $config = $this->processConfiguration($configuration, $configs);
71
        }
72
73
        $this
74 4
            ->processInjections($config, $container)
75 4
            ->processFilters($config, $container)
76 4
            ->processExclusions($config, $container);
77 4
    }
78
79
    /**
80
     * @param array $config
81
     * @param ContainerBuilder $container
82
     * @return Extension $this
83
     */
84 4
    protected function processInjections(array $config, ContainerBuilder $container)
85
    {
86 4
        $injection = [];
87
88 4
        foreach ($config['injects'] as $trait => $defintion) {
89 1
            $injection[ltrim($trait, '\\')] = $defintion['calls'];
90
        }
91
92 4
        if (count($injection) > 0) {
93 1
            $container->setParameter('runopencode.traitor.injectables', $injection);
94
        }
95
96 4
        return $this;
97
    }
98
99
    /**
100
     * @param array $config
101
     * @param ContainerBuilder $container
102
     * @return Extension $this
103
     */
104 4
    protected function processFilters(array $config, ContainerBuilder $container)
105
    {
106 4
        if (isset($config['filter'])) {
107
108 1
            if (count($config['filter']['tags']) > 0) {
109 1
                $container->setParameter('runopencode.traitor.filter.tags', $config['filter']['tags']);
110
            }
111
112 1
            if (count($config['filter']['namespaces']) > 0) {
113
114
                $container->setParameter('runopencode.traitor.filter.namespaces', array_map(function($namespace) {
115 1
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
116 1
                }, $config['filter']['namespaces']));
117
            }
118
        }
119
120 4
        return $this;
121
    }
122
123
    /**
124
     * @param array $config
125
     * @param ContainerBuilder $container
126
     * @return Extension $this
127
     */
128 4
    protected function processExclusions(array $config, ContainerBuilder $container)
129
    {
130 4
        if (isset($config['exclude'])) {
131
132 1
            if (count($config['exclude']['services']) > 0) {
133 1
                $container->setParameter('runopencode.traitor.exclude.services', $config['exclude']['services']);
134
            }
135
136 1
            if (count($config['exclude']['namespaces']) > 0) {
137
138
                $container->setParameter('runopencode.traitor.exclude.namespaces', array_map(function($namespace) {
139 1
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
140 1
                }, $config['exclude']['namespaces']));
141
            }
142
143 1
            if (count($config['exclude']['classes']) > 0) {
144
145 1
                $container->setParameter('runopencode.traitor.exclude.classes', array_map(function($fqcn) {
146 1
                    return ltrim($fqcn, '\\');
147 1
                }, $config['exclude']['classes']));
148
            }
149
150 1
            if (count($config['exclude']['tags']) > 0) {
151 1
                $container->setParameter('runopencode.traitor.exclude.tags', $config['exclude']['tags']);
152
            }
153
154
        }
155
156 4
        return $this;
157
    }
158
}
159
160