Completed
Pull Request — develop (#122)
by Vladimir
01:44
created

MarkupEngineManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 72%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 63
ccs 18
cts 25
cp 0.72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addMarkupEngines() 0 7 2
A addMarkupEngine() 0 19 3
A getEngineByTag() 0 9 2
A getEngineByExtension() 0 9 2
A getTemplateTags() 0 4 1
A getSupportedExtensions() 0 4 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\MarkupEngine;
9
10
use __;
11
use allejo\stakx\Exception\UnsupportedMarkupException;
12
13
class MarkupEngineManager
14
{
15
    private $enginesByTags = [];
16
    private $enginesByExtension = [];
17
18 76
    public function addMarkupEngines(/*iterable*/ $markupEngines)
19
    {
20 76
        foreach ($markupEngines as $markupEngine)
21
        {
22 76
            $this->addMarkupEngine($markupEngine);
23
        }
24 76
    }
25
26 76
    public function addMarkupEngine(MarkupEngineInterface $markupEngine)
27
    {
28 76
        $extensions = $markupEngine->getExtensions();
29 76
        $primaryExt = __::first($extensions);
0 ignored issues
show
Documentation introduced by
$extensions is of type array<integer,string>, but the function expects a object<__\iterable>.

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...
30
31 76
        foreach ($extensions as $k => $extension)
32
        {
33 76
            if ($k === 0)
34
            {
35 76
                $this->enginesByExtension[$extension] = $markupEngine;
36
            }
37
            else
38
            {
39 76
                $this->enginesByExtension[$extension] = &$this->enginesByExtension[$primaryExt];
40
            }
41
        }
42
43 76
        $this->enginesByTags[$markupEngine->getTemplateTag()] = &$this->enginesByExtension[$primaryExt];
44 76
    }
45
46
    public function getEngineByTag($tag)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        if (isset($this->enginesByTags[$tag]))
49
        {
50
            return $this->enginesByTags[$tag];
51
        }
52
53
        throw new UnsupportedMarkupException($tag, 'There is no support to handle this markup format.');
54
    }
55
56 64
    public function getEngineByExtension($extension)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
57
    {
58 64
        if (isset($this->enginesByExtension[$extension]))
59
        {
60 64
            return $this->enginesByExtension[$extension];
61
        }
62
63
        throw new UnsupportedMarkupException($extension, 'There is no support to handle this markup format.');
64
    }
65
66
    public function getTemplateTags()
67
    {
68
        return array_filter(array_keys($this->enginesByTags));
69
    }
70
71 36
    public function getSupportedExtensions()
72
    {
73 36
        return array_keys($this->enginesByExtension);
74
    }
75
}
76