TemplateNameParser   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.36%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 4
dl 0
loc 43
ccs 19
cts 22
cp 0.8636
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C parse() 0 40 7
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Templating;
3
4
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as BaseTemplateNameParser;
5
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference as FrameworkTemplateReference;
6
use Symfony\Component\Templating\TemplateReferenceInterface;
7
8
/**
9
 * @author Warnar Boekkooi <[email protected]>
10
 */
11
class TemplateNameParser extends BaseTemplateNameParser
12
{
13 15
    public function parse($name)
14
    {
15 15
        if ($name instanceof TemplateReferenceInterface) {
16 1
            return $name;
17 14
        } elseif (isset($this->cache[$name])) {
18 2
            return $this->cache[$name];
19
        }
20
21
        // Not a override
22 14
        if ($name[0] !== '!') {
23 12
            return parent::parse($name);
24
        }
25
26
        // normalize name (see TemplateNameParser line 52)
27 2
        $name = substr($name, 1);
28 2
        $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
29 2
        if (isset($this->cache['!' . $name])) {
30
            return $this->cache['!' . $name];
31
        }
32
33
        // Get the parent reference
34 2
        $reference = parent::parse($name);
35 2
        if (!$reference instanceof FrameworkTemplateReference) {
36
            return $reference;
37
        }
38
39 2
        $bundle = $reference->get('bundle');
40 2
        if (empty($bundle)) {
41
            return $reference;
42
        }
43
44
        // Resolve the base template path
45 2
        $path = $reference->getPath();
46 2
        $relativePath = substr($path, strlen($bundle) + 1);
47
48 2
        $bundles = $this->kernel->getBundle($bundle, false);
49 2
        $relativePath = end($bundles)->getPath() . $relativePath;
50
51 2
        return $this->cache['!' . $name] = new TemplateReference($relativePath, $reference);
52
    }
53
}
54