Completed
Branch master (6cd78a)
by Mewes
04:43 queued 01:43
created

NodeHelper::removeTextNodesRecursively()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 7.2765
cc 10
eloc 12
nc 7
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MewesK\TwigExcelBundle\Twig;
4
5
use MewesK\TwigExcelBundle\Twig\Node\XlsNode;
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
     * @param Twig_Node $node
24
     */
25
    public static function fixMacroCallsRecursively(Twig_Node $node)
26
    {
27
        foreach ($node->getIterator() as $key => $subNode) {
28
            if ($subNode instanceof Twig_Node_Expression_MethodCall) {
29
                /**
30
                 * @var \Twig_Node_Expression_Array $argumentsNode
31
                 */
32
                $argumentsNode = $subNode->getNode('arguments');
33
                $argumentsNode->addElement(new Twig_Node_Expression_Name('phpExcel', null), null);
34
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
35
                self::fixMacroCallsRecursively($subNode);
36
            }
37
        }
38
    }
39
40
    /**
41
     * @param Twig_Node $node
42
     * @param Twig_Parser $parser
43
     */
44
    public static function removeTextNodesRecursively(Twig_Node $node, Twig_Parser $parser)
45
    {
46
        foreach ($node->getIterator() as $key => $subNode) {
47
            if ($subNode instanceof Twig_Node_Text) {
48
                // Never delete a block body
49
                if ($key === 'body' && $node instanceof Twig_Node_Block) {
50
                    continue;
51
                }
52
53
                $node->removeNode($key);
54
            } elseif ($subNode instanceof Twig_Node_BlockReference) {
55
                self::removeTextNodesRecursively($parser->getBlock($subNode->getAttribute('name')), $parser);
56
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
57
                if ($subNode instanceof XlsNode && $subNode->canContainText()) {
58
                    continue;
59
                }
60
                self::removeTextNodesRecursively($subNode, $parser);
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param XlsNode $node
67
     * @param array $path
68
     * @throws Twig_Error_Syntax
69
     */
70
    public static function checkAllowedParents(XlsNode $node, array $path)
71
    {
72
        $parentName = null;
73
74
        foreach (array_reverse($path) as $className) {
75
            if (strpos($className, 'MewesK\TwigExcelBundle\Twig\Node\Xls') === 0) {
76
                $parentName = $className;
77
                break;
78
            }
79
        }
80
81
        if ($parentName === null) {
82
            return;
83
        }
84
85
        foreach ($node->getAllowedParents() as $className) {
86
            if ($className === $parentName) {
87
                return;
88
            }
89
        }
90
91
        throw new Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', get_class($node), $parentName));
92
    }
93
94
    /**
95
     * @param Twig_Node $node
96
     * @return bool
97
     */
98
    public static function checkContainsXlsNode(Twig_Node $node)
99
    {
100
        foreach ($node->getIterator() as $key => $subNode) {
101
            if ($node instanceof XlsNode) {
102
                return true;
103
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
104
                if (self::checkContainsXlsNode($subNode)) {
105
                    return true;
106
                }
107
            }
108
        }
109
        return false;
110
    }
111
}
112