DeferExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 4
Metric Value
wmc 15
c 5
b 2
f 4
lcom 2
cbo 3
dl 0
loc 81
ccs 38
cts 38
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A contains() 0 4 2
A getName() 0 4 1
A getTokenParsers() 0 6 1
A getFunctions() 0 8 1
B cache() 0 19 6
A retrieve() 0 13 3
1
<?php
2
namespace Boekkooi\Bundle\TwigJackBundle\Twig\Extension;
3
4
use Boekkooi\Bundle\TwigJackBundle\Twig\TokenParser;
5
6
/**
7
 * @author Warnar Boekkooi <[email protected]>
8
 */
9
class DeferExtension extends \Twig_Extension
10
{
11
    protected $references = array();
12
13
    protected $subReferences = array();
14
15
    protected $deferBlockPrefix;
16
17 6
    public function __construct($deferBlockPrefix = '_defer_ref_')
18
    {
19 6
        $this->deferBlockPrefix = $deferBlockPrefix;
20 6
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 1
    public function getTokenParsers()
26
    {
27
        return array(
28 1
            new TokenParser\Defer($this->deferBlockPrefix)
29 1
        );
30
    }
31
32 1
    public function getFunctions()
33
    {
34
        return array(
35 1
            new \Twig_SimpleFunction('defer', null, array(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
36 1
                'node_class' => 'Boekkooi\Bundle\TwigJackBundle\Twig\Node\Expression\DeferReference',
37 1
            ))
38 1
        );
39
    }
40
41 5
    public function cache($type, $content, $name = null, $offset = null)
42
    {
43 5
        if (!isset($this->references[$type])) {
44 5
            $this->references[$type] = array();
45 5
            $this->subReferences[$type] = array();
46 5
        }
47
48 5
        if ($offset === null || $offset >= count($this->references[$type])) {
49 5
            $this->references[$type][] = $content;
50 5
        } elseif ($offset <= 0) {
51 1
            array_unshift($this->references[$type], $content);
52 1
        } else {
53 1
            array_splice($this->references[$type], $offset, 0, $content);
54
        }
55
56 5
        if ($name !== null) {
57 5
            $this->subReferences[$type][] = $name;
58 5
        }
59 5
    }
60
61 1
    public function contains($type, $name)
62
    {
63 1
        return isset($this->subReferences[$type]) && in_array($name, $this->subReferences[$type], true);
64
    }
65
66 4
    public function retrieve($type, $clear = true)
67
    {
68 4
        if (!isset($this->references[$type])) {
69 1
            return array();
70
        }
71
72 4
        $rtn = $this->references[$type];
73 4
        if ($clear) {
74 4
            unset($this->references[$type]);
75 4
        }
76
77 4
        return $rtn;
78
    }
79
80
    /**
81
     * Returns the name of the extension.
82
     *
83
     * @return string The extension name
84
     */
85 1
    public function getName()
86
    {
87 1
        return 'defer';
88
    }
89
}
90