Completed
Pull Request — master (#9)
by Eugene
03:48
created

DeferredExtension::getNodeVisitors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Twig\DeferredExtension;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\Template;
7
8
final class DeferredExtension extends AbstractExtension
9
{
10
    private $blocks = [];
11
12
    public function getTokenParsers()
13
    {
14
        return [new DeferredTokenParser()];
15
    }
16
17
    public function getNodeVisitors()
18
    {
19
        return [new DeferredNodeVisitor()];
20
    }
21
22
    public function defer(Template $template, $blockName)
23
    {
24
        $templateName = $template->getTemplateName();
25
        $this->blocks[$templateName][] = $blockName;
26
        ob_start();
27
    }
28
29
    public function resolve(Template $template, array $context, array $blocks)
30
    {
31
        $templateName = $template->getTemplateName();
32
        if (empty($this->blocks[$templateName])) {
33
            return;
34
        }
35
36
        while ($blockName = array_pop($this->blocks[$templateName])) {
37
            $buffer = ob_get_clean();
38
39
            $blocks[$blockName] = [$template, 'block_'.$blockName.'_deferred'];
40
            $template->displayBlock($blockName, $context, $blocks);
41
42
            echo $buffer;
43
        }
44
45
        if ($parent = $template->getParent($context)) {
46
            $this->resolve($parent, $context, $blocks);
0 ignored issues
show
Bug introduced by
It seems like $parent defined by $template->getParent($context) on line 45 can also be of type object<Twig\TemplateWrapper>; however, Twig\DeferredExtension\D...redExtension::resolve() does only seem to accept object<Twig\Template>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
        }
48
    }
49
}
50