NodeHelper   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 27
lcom 0
cbo 1
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B fixMacroCallsRecursively() 0 14 5
B removeTextNodesRecursively() 0 20 10
B checkAllowedParents() 0 23 6
B checkContainsXlsNode() 0 13 6
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Twig;
4
5
use MewesK\TwigExcelBundle\Twig\Node\SyntaxAwareNodeInterface;
6
use Twig_Error_Syntax;
7
use Twig_Node;
8
use Twig_Node_Block;
9
use Twig_Node_BlockReference;
10
use Twig_Node_Expression_MethodCall;
11
use Twig_Node_Expression_Name;
12
use Twig_Node_Text;
13
use Twig_Parser;
14
15
/**
16
 * Class NodeHelper
17
 *
18
 * @package MewesK\TwigExcelBundle\Twig\TokenParser
19
 */
20
class NodeHelper
21
{
22
    /**
23
     * Adds the PhpExcel instance as the last parameter to all macro function calls.
24
     *
25
     * @param Twig_Node $node
26
     */
27
    public static function fixMacroCallsRecursively(Twig_Node $node)
28
    {
29
        foreach ($node->getIterator() as $key => $subNode) {
30
            if ($subNode instanceof Twig_Node_Expression_MethodCall) {
0 ignored issues
show
Bug introduced by
The class Twig_Node_Expression_MethodCall does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
31
                /**
32
                 * @var \Twig_Node_Expression_Array $argumentsNode
33
                 */
34
                $argumentsNode = $subNode->getNode('arguments');
35
                $argumentsNode->addElement(new Twig_Node_Expression_Name('phpExcel', null), null);
36
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
0 ignored issues
show
Bug introduced by
The class Twig_Node does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
37
                self::fixMacroCallsRecursively($subNode);
38
            }
39
        }
40
    }
41
42
    /**
43
     * Removes all TextNodes that are in illegal places to avoid echos in unwanted places.
44
     *
45
     * @param Twig_Node $node
46
     * @param Twig_Parser $parser
47
     */
48
    public static function removeTextNodesRecursively(Twig_Node $node, Twig_Parser $parser)
49
    {
50
        foreach ($node->getIterator() as $key => $subNode) {
51
            if ($subNode instanceof Twig_Node_Text) {
0 ignored issues
show
Bug introduced by
The class Twig_Node_Text does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
52
                // Never delete a block body
53
                if ($key === 'body' && $node instanceof Twig_Node_Block) {
0 ignored issues
show
Bug introduced by
The class Twig_Node_Block does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
54
                    continue;
55
                }
56
57
                $node->removeNode($key);
58
            } elseif ($subNode instanceof Twig_Node_BlockReference) {
0 ignored issues
show
Bug introduced by
The class Twig_Node_BlockReference does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
59
                self::removeTextNodesRecursively($parser->getBlock($subNode->getAttribute('name')), $parser);
60
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
0 ignored issues
show
Bug introduced by
The class Twig_Node does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
61
                if ($subNode instanceof SyntaxAwareNodeInterface && $subNode->canContainText()) {
62
                    continue;
63
                }
64
                self::removeTextNodesRecursively($subNode, $parser);
65
            }
66
        }
67
    }
68
69
    /**
70
     * @param SyntaxAwareNodeInterface $node
71
     * @param array $path
72
     * @throws Twig_Error_Syntax
73
     */
74
    public static function checkAllowedParents(SyntaxAwareNodeInterface $node, array $path)
75
    {
76
        $parentName = null;
77
78
        foreach (array_reverse($path) as $className) {
79
            if (strpos($className, 'MewesK\TwigExcelBundle\Twig\Node\Xls') === 0) {
80
                $parentName = $className;
81
                break;
82
            }
83
        }
84
85
        if ($parentName === null) {
86
            return;
87
        }
88
89
        foreach ($node->getAllowedParents() as $className) {
90
            if ($className === $parentName) {
91
                return;
92
            }
93
        }
94
95
        throw new Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', get_class($node), $parentName));
96
    }
97
98
    /**
99
     * @param Twig_Node $node
100
     * @return bool
101
     */
102
    public static function checkContainsXlsNode(Twig_Node $node)
103
    {
104
        foreach ($node->getIterator() as $key => $subNode) {
105
            if ($node instanceof SyntaxAwareNodeInterface) {
106
                return true;
107
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
0 ignored issues
show
Bug introduced by
The class Twig_Node does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
108
                if (self::checkContainsXlsNode($subNode)) {
109
                    return true;
110
                }
111
            }
112
        }
113
        return false;
114
    }
115
}
116