Completed
Push — master ( 690b6b...6753b6 )
by Nikola
05:20
created

Extension::configureUnderscoredBundlePrefixNamer()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 2
crap 4
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 6
    public function getAlias()
24
    {
25 6
        return "runopencode_doctrine_naming_strategy";
26
    }
27
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 6
    public function getNamespace()
33
    {
34 6
        return 'http://www.runopencode.com/xsd-schema/doctrine-naming-strategy-bundle';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function getXsdValidationBasePath()
41
    {
42 2
        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 3
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix');
76
77 3
            $config['underscored_bundle_prefix']['case'] = ('uppercase' === $config['underscored_bundle_prefix']['case']) ? CASE_UPPER : CASE_LOWER;
78
79 3
            $args = $definition->getArguments();
80 3
            $args[1] = $config['underscored_bundle_prefix'];
81
82 3
            $definition->setArguments($args);
83
        }
84
85 3
        return $this;
86
    }
87
88
    /**
89
     * Configure 'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix' naming strategy.
90
     *
91
     * @param ContainerBuilder $container
92
     * @param array $config
93
     * @return Extension $this
94
     */
95 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...
96
    {
97
        if (
98 3
            $container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix')
99
            &&
100 3
            isset($config['underscored_class_namespace_prefix'])
101
        ) {
102 3
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix');
103
104 3
            $config['underscored_class_namespace_prefix']['case'] = ('uppercase' === $config['underscored_class_namespace_prefix']['case']) ? CASE_UPPER : CASE_LOWER;
105
106 3
            $args = $definition->getArguments();
107 3
            $args[0] = $config['underscored_class_namespace_prefix'];
108
109 3
            $definition->setArguments($args);
110
        }
111
112 3
        return $this;
113
    }
114
115
    /**
116
     * Configure 'runopencode.doctrine.orm.naming_strategy.underscored_namer_collection' naming strategy.
117
     *
118
     * @param ContainerBuilder $container
119
     * @param array $config
120
     * @return Extension $this
121
     */
122 3
    private function configureNamerCollection(ContainerBuilder $container, array $config)
123
    {
124
        if (
125 3
            $container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection')
126
            &&
127 3
            isset($config['underscored_namer_collection'])
128
        ) {
129 3
            $definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection');
130
131 3
            $definition->setArguments(array(
132 3
                new Reference($config['underscored_namer_collection']['default']),
133 3
                array_map(function ($namerId) {
134 3
                    return new Reference($namerId);
135 3
                }, $config['underscored_namer_collection']['namers']),
136
                array(
137 3
                    'join_table_field_suffix' => $config['underscored_namer_collection']['join_table_field_suffix'],
138
                )
139
            ));
140
        }
141
142 3
        return $this;
143
    }
144
}
145