Completed
Pull Request — master (#2)
by David
04:51
created

CMSUtilsServiceProvider::createBlockRenderer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
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
        $aggregateThemeFactory = null;
23
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(TheCodingMa...ion(...) { /* ... */ }) returns the type array<string,callable|ca...tainer\Factories\Alias> 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...
24 View Code Duplication
            AggregateThemeFactory::class => function(ContainerInterface $container) use (&$aggregateThemeFactory): AggregateThemeFactory
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
25
            {
26
                if ($aggregateThemeFactory === null) {
27
                    $aggregateThemeFactory = new AggregateThemeFactory([]);
28
                } else {
29
                    return $aggregateThemeFactory;
30
                }
31
                $aggregateThemeFactory = new AggregateThemeFactory();
32
33
                $subThemeFactory = new SubThemeFactory($aggregateThemeFactory);
34
                $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $container->get(BlockRendererInterface::class));
35
36
                $aggregateThemeFactory->addThemeFactory($twigThemeFactory);
37
                $aggregateThemeFactory->addThemeFactory($subThemeFactory);
38
                return $aggregateThemeFactory;
39
            },
40
            ThemeFactoryInterface::class => new Alias(AggregateThemeFactory::class),
41
            BlockRendererInterface::class => new Alias(BlockRenderer::class),
42 View Code Duplication
            BlockRenderer::class => function(ContainerInterface $container) use (&$aggregateThemeFactory): BlockRenderer
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43
            {
44
                if ($aggregateThemeFactory === null) {
45
                    $aggregateThemeFactory = new AggregateThemeFactory([]);
46
                }
47
48
                $blockRenderer = new BlockRenderer($aggregateThemeFactory);
49
50
                $subThemeFactory = new SubThemeFactory($aggregateThemeFactory);
51
                $twigThemeFactory = new TwigThemeFactory($container->get(\Twig_Environment::class), $blockRenderer);
52
53
                $aggregateThemeFactory->addThemeFactory($subThemeFactory);
54
                $aggregateThemeFactory->addThemeFactory($twigThemeFactory);
55
56
                return $blockRenderer;
57
            },
58
59
        ];
60
    }
61
62
    public function getExtensions()
63
    {
64
        return [];
65
    }
66
}
67