Completed
Push — master ( 3af88c...d76c98 )
by Mewes
05:10
created

RemoveTextNodeTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B removeTextNodesRecursively() 0 20 10
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Twig\TokenParser\Traits;
4
5
use MewesK\TwigExcelBundle\Twig\Node\XlsCellNode;
6
use MewesK\TwigExcelBundle\Twig\Node\XlsCenterNode;
7
use MewesK\TwigExcelBundle\Twig\Node\XlsLeftNode;
8
use MewesK\TwigExcelBundle\Twig\Node\XlsNode;
9
use MewesK\TwigExcelBundle\Twig\Node\XlsRightNode;
10
use Twig_Node;
11
use Twig_Node_Block;
12
use Twig_Node_BlockReference;
13
use Twig_Node_Text;
14
15
/**
16
 * Class RemoveTextNodeTrait
17
 *
18
 * @package MewesK\TwigExcelBundle\Twig\TokenParser
19
 */
20
trait RemoveTextNodeTrait
21
{
22
    /**
23
     * @param Twig_Node $node
24
     */
25
    protected function removeTextNodesRecursively(Twig_Node $node)
26
    {
27
        foreach ($node->getIterator() as $key => $subNode) {
28
            if ($subNode instanceof Twig_Node_Text) {
29
                // Never delete a block body
30
                if ($key === 'body' && $node instanceof Twig_Node_Block) {
31
                    continue;
32
                }
33
34
                $node->removeNode($key);
35
            } elseif ($subNode instanceof Twig_Node_BlockReference) {
36
                $this->removeTextNodesRecursively($this->parser->getBlock($subNode->getAttribute('name')));
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
            } elseif ($subNode instanceof Twig_Node && $subNode->count() > 0) {
38
                if ($subNode instanceof XlsNode && $subNode->canContainText()) {
39
                    continue;
40
                }
41
                $this->removeTextNodesRecursively($subNode);
42
            }
43
        }
44
    }
45
}
46