Completed
Push — master ( 23f69a...13261c )
by Nikola
03:25
created

Extension::getNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
14
15
class Extension extends BaseExtension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    public function getAlias()
21
    {
22 2
        return 'runopencode_traitor';
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function getNamespace()
29
    {
30 2
        return 'http://www.runopencode.com/xsd-schema/traitor-bundle';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getXsdValidationBasePath()
37
    {
38
        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...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function load(array $configs, ContainerBuilder $container)
45
    {
46 2
        $configuration = new Configuration();
47 2
        $config = $this->processConfiguration($configuration, $configs);
48
49
        $this
50 2
            ->setInjectionMapAsContainerParameter($config, $container)
51 2
            ->setFiltersAsContainerParameter($config, $container)
52 2
            ->setExclusionsAsContainerParameter($config, $container)
53
            ;
54 2
    }
55
56
    /**
57
     * @param array $config
58
     * @param ContainerBuilder $container
59
     * @return Extension $this
60
     */
61 2
    protected function setInjectionMapAsContainerParameter(array $config, ContainerBuilder $container)
62
    {
63 2
        $injection = array();
64
65 2
        foreach ($config['inject'] as $trait => $injection) {
66
            $injection[ltrim($trait, '\\')] = $injection;
67
        }
68
69 2
        if ($config['use_common_traits']) {
70
71 1
            $injection = array_merge($injection, array(
72 1
                'Symfony\Component\DependencyInjection\ContainerAwareTrait' => array('setContainer', array('@service_container')),
73
                'Psr\Log\LoggerAwareTrait' => array('setLogger', array('@logger')),
74
                'RunOpenCode\Bundle\Traitor\Traits\DoctrineAwareTrait' => array('setDoctrine', array('@doctrine')),
75
                'RunOpenCode\Bundle\Traitor\Traits\EventDispatcherAwareTrait' => array('setEventDispatcher', array('@event_dispatcher')),
76
                'RunOpenCode\Bundle\Traitor\Traits\FilesystemAwareTrait' => array('setFilesystem', array('@filesystem')),
77
                'RunOpenCode\Bundle\Traitor\Traits\KernelAwareTrait' => array('setKernel', array('@kernel')),
78
                'RunOpenCode\Bundle\Traitor\Traits\MailerAwareInterface' => array('setMailer', array('@mailer')),
79
                'RunOpenCode\Bundle\Traitor\Traits\PropertyAccessorAwareTrait' => array('setPropertyAccessor', array('@property_accessor')),
80
                'RunOpenCode\Bundle\Traitor\Traits\RequestStackAwareTrait' => array('setRequestStack', array('@request_stack')),
81
                'RunOpenCode\Bundle\Traitor\Traits\RouterAwareTrait' => array('setRouter', array('@router')),
82
                'RunOpenCode\Bundle\Traitor\Traits\AuthorizationCheckerAwareTrait' => array('setAuthorizationChecker', array('@security.authorization_checker')),
83
                'RunOpenCode\Bundle\Traitor\Traits\SessionAwareTrait' => array('setSession', array('@session')),
84
                'RunOpenCode\Bundle\Traitor\Traits\TwigAwareTrait' => array('setTwig', array('@twig')),
85
                'RunOpenCode\Bundle\Traitor\Traits\TranslatorAwareTrait' => array('setTranslator', array('@translator')),
86
                'RunOpenCode\Bundle\Traitor\Traits\ValidatorAwareTrait' => array('setValidator', array('@validator')),
87
                'RunOpenCode\Bundle\Traitor\Traits\TokenStorageAwareTrait' => array('setTokenStorage', array('@security.token_storage'))
88
            ));
89
        }
90
91 2
        $container->setParameter('runopencode.traitor.injection_map', $injection);
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * @param array $config
98
     * @param ContainerBuilder $container
99
     * @return Extension $this
100
     */
101 2
    protected function setFiltersAsContainerParameter(array $config, ContainerBuilder $container)
102
    {
103 2
        if (isset($config['filters'])) {
104
105 1
            if (count($config['filters']['tags']) > 0) {
106 1
                $container->setParameter('runopencode.traitor.filter.tags', $config['filters']['tags']);
107
            }
108
109 1 View Code Duplication
            if (count($config['filters']['namespaces']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110
111
                $container->setParameter('runopencode.traitor.filter.namespaces', array_map(function($namespace) {
112 1
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
113 1
                }, $config['filters']['namespaces']));
114
            }
115
        }
116
117 2
        return $this;
118
    }
119
120
    /**
121
     * @param array $config
122
     * @param ContainerBuilder $container
123
     * @return Extension $this
124
     */
125 2
    protected function setExclusionsAsContainerParameter(array $config, ContainerBuilder $container)
126
    {
127 2
        if (isset($config['exclude'])) {
128
129
            if (count($config['exclude']['services']) > 0) {
130
                $container->setParameter('runopencode.traitor.exclude.services', $config['exclude']['services']);
131
            }
132
133 View Code Duplication
            if (count($config['exclude']['namespaces']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
134
135
                $container->setParameter('runopencode.traitor.exclude.namespaces', array_map(function($namespace) {
136
                    return rtrim(ltrim($namespace, '\\'), '\\') . '\\';
137
                }, $config['exclude']['namespaces']));
138
            }
139
140
            if (count($config['exclude']['classes']) > 0) {
141
142
                $container->setParameter('runopencode.traitor.exclude.classes', array_map(function($fqcn) {
143
                    return ltrim($fqcn, '\\');
144
                }, $config['exclude']['classes']));
145
            }
146
        }
147
148 2
        return $this;
149
    }
150
}
151
152