DeferredNodeVisitor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 12
c 0
b 0
f 0
dl 0
loc 28
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enterNode() 0 7 3
A leaveNode() 0 10 3
A getPriority() 0 3 1
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\Environment;
17
use Twig\Node\ModuleNode;
18
use Twig\Node\Node;
19
use Twig\NodeVisitor\NodeVisitorInterface;
20
21
final class DeferredNodeVisitor implements NodeVisitorInterface
22
{
23
    private $hasDeferred = false;
24
25
    public function enterNode(Node $node, Environment $env) : Node
26
    {
27
        if (!$this->hasDeferred && $node instanceof DeferredBlockNode) {
28
            $this->hasDeferred = true;
29
        }
30
31
        return $node;
32
    }
33
34
    public function leaveNode(Node $node, Environment $env) : ?Node
35
    {
36
        if ($this->hasDeferred && $node instanceof ModuleNode) {
37
            $node->getNode('constructor_end')->setNode('deferred_initialize', new DeferredInitializeNode());
38
            $node->getNode('display_end')->setNode('deferred_resolve', new DeferredResolveNode());
39
            $node->getNode('class_end')->setNode('deferred_declare', new DeferredDeclareNode());
40
            $this->hasDeferred = false;
41
        }
42
43
        return $node;
44
    }
45
46
    public function getPriority() : int
47
    {
48
        return 0;
49
    }
50
}
51