Passed
Pull Request — master (#2)
by David
02:02
created

CMSUtilsServiceProvider::getFactories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
4
namespace TheCodingMachine\CMS\DI;
5
6
7
use Interop\Container\Factories\Alias;
8
use Interop\Container\ServiceProviderInterface;
9
use Psr\Container\ContainerInterface;
10
use TheCodingMachine\CMS\Block\BlockRenderer;
11
use TheCodingMachine\CMS\Block\BlockRendererInterface;
12
use TheCodingMachine\CMS\Theme\AggregateThemeFactory;
13
use TheCodingMachine\CMS\Theme\SubThemeFactory;
14
use TheCodingMachine\CMS\Theme\ThemeFactoryInterface;
15
use TheCodingMachine\CMS\Theme\TwigThemeFactory;
16
17
class CMSUtilsServiceProvider implements ServiceProviderInterface
18
{
19
20
    public function getFactories()
21
    {
22
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(TheCodingMa...'createBlockRenderer')) returns the type array<string,Interop\Con...|array<integer,string>> which is incompatible with the return type mandated by Interop\Container\Servic...terface::getFactories() of array<mixed,callable>.

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...
23
            AggregateThemeFactory::class => [self::class, 'createAggregateThemeFactory'],
24
            ThemeFactoryInterface::class => new Alias(AggregateThemeFactory::class),
25
            BlockRendererInterface::class => new Alias(BlockRenderer::class),
26
            BlockRenderer::class => [self::class, 'createBlockRenderer'],
27
28
        ];
29
    }
30
31
    public static function createAggregateThemeFactory(ContainerInterface $container): AggregateThemeFactory
32
    {
33
        if (self::$themeFactory === null) {
34
            self::$themeFactory = new AggregateThemeFactory([]);
35
        }
36
        self::$aggregateThemeFactory = new AggregateThemeFactory();
0 ignored issues
show
Bug Best Practice introduced by
The property aggregateThemeFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
38
        $subThemeFactory = new SubThemeFactory(self::$aggregateThemeFactory);
0 ignored issues
show
Unused Code introduced by
The assignment to $subThemeFactory is dead and can be removed.
Loading history...
39
        $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $blockRenderer);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $blockRenderer seems to be never defined.
Loading history...
Unused Code introduced by
The assignment to $twigThemeFactory is dead and can be removed.
Loading history...
40
41
        self::$aggregateThemeFactory->addThemeFactory($container->get(TwigThemeFactory::class));
42
        self::$aggregateThemeFactory->addThemeFactory($container->get(SubThemeFactory::class));
43
        return self::$aggregateThemeFactory;
44
    }
45
46
    private static $themeFactory = null;
47
48
    public static function createBlockRenderer(ContainerInterface $container): BlockRenderer
49
    {
50
        if (self::$themeFactory === null) {
51
            self::$themeFactory = new AggregateThemeFactory([]);
52
        }
53
54
        $blockRenderer = new BlockRenderer(self::$themeFactory);
55
56
        $subThemeFactory = new SubThemeFactory(self::$themeFactory);
57
        $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $blockRenderer);
58
59
        self::$themeFactory->addThemeFactory($subThemeFactory);
60
        self::$themeFactory->addThemeFactory($twigThemeFactory);
61
62
        return $blockRenderer;
63
    }
64
65
    public function getExtensions()
66
    {
67
        return [];
68
    }
69
}
70