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

RemoveTextNodeTrait::removeTextNodesRecursively()   B

Complexity

Conditions 10
Paths 7

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

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\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