Completed
Push — master ( 6f5389...c76b00 )
by Eugene
01:08
created

DeferredExtension::defer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the rybakit/twig-deferred-extension package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Twig\DeferredExtension;
15
16
use Twig\Extension\AbstractExtension;
17
use Twig\Template;
18
19
final class DeferredExtension extends AbstractExtension
20
{
21
    private $blocks = [];
22
23
    public function getTokenParsers() : array
24
    {
25
        return [new DeferredTokenParser()];
26
    }
27
28
    public function getNodeVisitors() : array
29
    {
30
        return [new DeferredNodeVisitor()];
31
    }
32
33
    public function defer(Template $template, string $blockName) : void
34
    {
35
        $templateName = $template->getTemplateName();
36
        $this->blocks[$templateName][] = $blockName;
37
        \ob_start();
38
    }
39
40
    public function resolve(Template $template, array $context, array $blocks) : void
41
    {
42
        $templateName = $template->getTemplateName();
43
        if (empty($this->blocks[$templateName])) {
44
            return;
45
        }
46
47
        while ($blockName = \array_pop($this->blocks[$templateName])) {
48
            $buffer = \ob_get_clean();
49
50
            $blocks[$blockName] = [$template, 'block_'.$blockName.'_deferred'];
51
            $template->displayBlock($blockName, $context, $blocks);
52
53
            echo $buffer;
54
        }
55
56
        if ($parent = $template->getParent($context)) {
57
            $this->resolve($parent, $context, $blocks);
0 ignored issues
show
Bug introduced by
It seems like $parent defined by $template->getParent($context) on line 56 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...
58
        }
59
    }
60
}
61