Test Failed
Push — master ( a25a4c...498f08 )
by Divine Niiquaye
13:17
created

CoreExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 11 1
A __construct() 0 3 1
A register() 0 9 3
A getAlias() 0 3 1
A dependencies() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Extensions;
19
20
use Rade\DI\AbstractContainer;
21
use Rade\DI\Definition;
22
use Rade\DI\Services\AliasedInterface;
23
use Rade\DI\Services\DependenciesInterface;
24
use Rade\Handler\EventHandler;
25
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
26
use Symfony\Component\Config\Definition\ConfigurationInterface;
27
28
/**
29
 * Rade Core Extension.
30
 *
31
 * @author Divine Niiquaye Ibok <[email protected]>
32
 */
33
class CoreExtension implements AliasedInterface, ConfigurationInterface, DependenciesInterface, ExtensionInterface
34
{
35
    private string $rootDir;
36
37
    public function __construct(string $rootDir)
38
    {
39
        $this->rootDir = \rtrim($rootDir, \DIRECTORY_SEPARATOR);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAlias(): string
46
    {
47
        return 'core';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getConfigTreeBuilder(): TreeBuilder
54
    {
55
        $treeBuilder = new TreeBuilder($this->getAlias());
56
57
        $treeBuilder->getRootNode()
58
            ->children()
59
                ->scalarNode('events_dispatcher')->end()
60
            ->end()
61
        ;
62
63
        return $treeBuilder;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function dependencies(): array
70
    {
71
        return [[ConfigExtension::class, [$this->rootDir]], RoutingExtension::class];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array(Rade\...outingExtension::class) returns the type array<integer,array<inte...string>|string>|string> which is incompatible with the return type mandated by Rade\DI\Services\Depende...terface::dependencies() of Rade\DI\Services\ServiceProviderInterface[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function register(AbstractContainer $container, array $configs = []): void
78
    {
79
        if (!$container->has('events.dispatcher')) {
80
            $eventsDispatcher = $configs['events_dispatcher'] ?? EventHandler::class;
81
82
            if ($container->has($eventsDispatcher)) {
83
                $container->alias('events.dispatcher', $eventsDispatcher);
84
            } else {
85
                $container->autowire('events.dispatcher', new Definition($eventsDispatcher));
86
            }
87
        }
88
    }
89
}
90