Completed
Push — master ( e54e2c...44461e )
by Nikola
05:06
created

Extension::configureNamerCollection()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 2
nop 2
crap 3
1
<?php
2
/*
3
 * This file is part of the Doctrine Naming Strategy Bundle, 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\DoctrineNamingStrategy\DependencyInjection;
11
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension as BaseExtension;
16
use Symfony\Component\Config\FileLocator;
17
18
class Extension extends BaseExtension
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 3
    public function getAlias()
24
    {
25 3
        return "runopencode_doctrine_naming_strategy";
26
    }
27
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function getNamespace()
33
    {
34 3
        return 'http://www.runopencode.com/xsd-schema/doctrine-naming-strategy-bundle';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getXsdValidationBasePath()
41
    {
42
        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...
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 3
    public function load(array $config, ContainerBuilder $container)
49
    {
50 3
        $configuration = new Configuration();
51 3
        $config = $this->processConfiguration($configuration, $config);
52
53 3
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
54 3
        $loader->load('services.xml');
55
56 3
        $this->configureUnderscoredBundlePrefixNamer($container, $config);
57 3
        $this->configureUnderscoredClassNamespacePrefixNamer($container, $config);
58 3
        $this->configureNamerCollection($container, $config);
59 3
    }
60
61
    /**
62
     * Configure 'runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix' naming strategy.
63
     *
64
     * @param ContainerBuilder $container
65
     * @param array $config
66
     * @return Extension $this
67
     */
68 3 View Code Duplication
    private function configureUnderscoredBundlePrefixNamer(ContainerBuilder $container, array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        if (
71 3
            $container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix')
72
            &&
73 3
            isset($config['underscored_bundle_prefix'])
74
        ) {
75 1
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix');
76
77 1
            if ('uppercase' === $config['underscored_bundle_prefix']['case']) {
78
                $config['underscored_bundle_prefix']['case'] = CASE_UPPER;
79
            } else {
80 1
                $config['underscored_bundle_prefix']['case'] = CASE_LOWER;
81
            }
82
83 1
            $args = $definition->getArguments();
84 1
            $args[1] = $config['underscored_bundle_prefix'];
85
86 1
            $definition->setArguments($args);
87
        }
88
89 3
        return $this;
90
    }
91
92
    /**
93
     * Configure 'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix' naming strategy.
94
     *
95
     * @param ContainerBuilder $container
96
     * @param array $config
97
     * @return Extension $this
98
     */
99 3 View Code Duplication
    private function configureUnderscoredClassNamespacePrefixNamer(ContainerBuilder $container, array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
100
    {
101
        if (
102 3
            $container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix')
103
            &&
104 3
            isset($config['underscored_class_namespace_prefix'])
105
        ) {
106 1
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix');
107
108 1
            if ('uppercase' === $config['underscored_class_namespace_prefix']['case']) {
109
                $config['underscored_class_namespace_prefix']['case'] = CASE_UPPER;
110
            } else {
111 1
                $config['underscored_class_namespace_prefix']['case'] = CASE_LOWER;
112
            }
113
114 1
            $args = $definition->getArguments();
115 1
            $args[0] = $config['underscored_class_namespace_prefix'];
116
117 1
            $definition->setArguments($args);
118
        }
119
120 3
        return $this;
121
    }
122
123
    /**
124
     * Configure 'runopencode.doctrine.orm.naming_strategy.namer_collection' naming strategy.
125
     *
126
     * @param ContainerBuilder $container
127
     * @param array $config
128
     * @return Extension $this
129
     */
130 3
    private function configureNamerCollection(ContainerBuilder $container, array $config)
131
    {
132
        if (
133 3
            $container->hasDefinition('runopencode.doctrine.orm.naming_strategy.namer_collection')
134
            &&
135 3
            isset($config['namer_collection'])
136
        ) {
137 1
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.namer_collection');
138
139 1
            $definition->setArguments(array(
140 1
                new Reference($config['namer_collection']['default']),
141 1
                array_map(function($namerId) {
142 1
                    return new Reference($namerId);
143 1
                }, $config['namer_collection']['namers']),
144
                array(
145 1
                    'concatenation' => $config['namer_collection']['concatenation'],
146 1
                    'joinTableFieldSuffix' => $config['namer_collection']['joinTableFieldSuffix']
147
                )
148
            ));
149
        }
150
151 3
        return $this;
152
    }
153
}
154