Completed
Push — master ( 42d7d9...a7190b )
by Nikola
03:37
created

Extension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * This file is part of the Twig Bufferized Template package, 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\Twig\BufferizedTemplate\Bridge\Symfony\DependencyInjection;
11
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
15
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
16
17
/**
18
 * Class Extension
19
 *
20
 * @package RunOpenCode\Twig\BufferizedTemplate\Bridge\Symfony\DependencyInjection
21
 */
22
class Extension extends BaseExtension
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function getAlias()
28
    {
29 4
        return "runopencode_twig_bufferized_template";
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 4
    public function getNamespace()
36
    {
37 4
        return 'http://www.runopencode.com/xsd-schema/twig-bufferized-template-bundle';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 2
    public function getXsdValidationBasePath()
44
    {
45 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...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function load(array $config, ContainerBuilder $container)
52
    {
53 1
        $configuration = new Configuration();
54 1
        $config = $this->processConfiguration($configuration, $config);
55
56 1
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
57 1
        $loader->load('twig.extension.xml');
58
59
60
        $container
61 1
            ->getDefinition('runopencode.twig.bufferized_template')
62 1
            ->setArguments([
63 1
                $config
64
            ]);
65 1
    }
66
}
67