Completed
Push — master ( 3c0a55...3afa22 )
by Maxime
02:42 queued 01:12
created

ElaoEnumExtension::prepend()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the "elao/enum" package.
5
 *
6
 * Copyright (C) Elao
7
 *
8
 * @author Elao <[email protected]>
9
 */
10
11
namespace Elao\Enum\Bridge\Symfony\Bundle\DependencyInjection;
12
13
use Elao\Enum\Bridge\Doctrine\DBAL\Types\TypesDumper;
14
use Elao\Enum\Bridge\Symfony\HttpKernel\Controller\ArgumentResolver\EnumValueResolver;
15
use Elao\Enum\Bridge\Symfony\Serializer\Normalizer\EnumNormalizer;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
class ElaoEnumExtension extends Extension implements PrependExtensionInterface
23
{
24
    public function prepend(ContainerBuilder $container)
25
    {
26
        $bundles = $container->getParameter('kernel.bundles');
27
        if (!isset($bundles['DoctrineBundle'])) {
28
            return;
29
        }
30
31
        $configs = $container->getExtensionConfig($this->getAlias());
32
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
34
        if (!($types = $config['doctrine']['types'] ?? false)) {
35
            return;
36
        }
37
38
        $doctrineTypesConfig = [];
39
        foreach ($types as $class => $value) {
40
            $doctrineTypesConfig[$value['name']] = TypesDumper::getTypeClassname($class);
41
        }
42
43
        $container->prependExtensionConfig('doctrine', [
44
            'dbal' => [
45
                'types' => $doctrineTypesConfig,
46
            ],
47
        ]);
48
    }
49
50
    public function load(array $configs, ContainerBuilder $container)
51
    {
52
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
53
        $loader->load('services.xml');
54
55
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfiguration($configs, $container) is of type null|object, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57
        if (!$this->isConfigEnabled($container, $config['argument_value_resolver'])) {
58
            $container->removeDefinition(EnumValueResolver::class);
59
        }
60
61
        if (!$this->isConfigEnabled($container, $config['serializer'])) {
62
            $container->removeDefinition(EnumNormalizer::class);
63
        }
64
65
        if ($types = $config['doctrine']['types'] ?? false) {
66
            $container->setParameter('.elao_enum.doctrine_types', array_map(static function (string $k, array $v): array {
67
                return [$k, $v['type'], $v['name']];
68
            }, array_keys($types), $types));
69
        }
70
    }
71
72
    public function getNamespace()
73
    {
74
        return 'http://elao.com/schema/dic/elao_enum';
75
    }
76
77
    public function getXsdValidationBasePath()
78
    {
79
        return __DIR__ . '/../Resources/config/schema';
80
    }
81
}
82