IfAttribute   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
cbo 1
dl 0
loc 58
ccs 34
cts 37
cp 0.9189
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A visit() 0 14 4
A removeWhitespace() 0 10 3
A findNextElement() 0 14 5
A findPrevElement() 0 14 5
1
<?php
2
namespace Goetas\Twital\Attribute;
3
4
use Goetas\Twital\Attribute as AttributeBase;
5
use Goetas\Twital\Compiler;
6
use Goetas\Twital\Twital;
7
8
/**
9
 *
10
 * @author Asmir Mustafic <[email protected]>
11
 *
12
 */
13
class IfAttribute implements AttributeBase
14
{
15 16
    public function visit(\DOMAttr $att, Compiler $context)
16
    {
17 16
        $node = $att->ownerElement;
18 16
        $pi = $context->createControlNode("if " . html_entity_decode($att->value));
19 16
        $node->parentNode->insertBefore($pi, $node);
20
21 16
        if (!($nextElement = self::findNextElement($node)) || (!$nextElement->hasAttributeNS(Twital::NS, 'elseif') && !$nextElement->hasAttributeNS(Twital::NS, 'else'))) {
22 11
            $pi = $context->createControlNode("endif");
23 11
            $node->parentNode->insertBefore($pi, $node->nextSibling); // insert after
24 11
        } else {
25 5
            self::removeWhitespace($node);
26
        }
27 16
        $node->removeAttributeNode($att);
28 16
    }
29
30 5
    public static function removeWhitespace(\DOMElement $element)
31
    {
32 5
        while ($el = $element->nextSibling) {
33 5
            if ($el instanceof \DOMText) {
34 2
                $element->parentNode->removeChild($el);
35 2
            } else {
36 5
                break;
37
            }
38 2
        }
39 5
    }
40
41 16
    public static function findNextElement(\DOMElement $element)
42
    {
43 16
        $next = $element;
44 16
        while ($next = $next->nextSibling) {
45 10
            if ($next instanceof \DOMText && trim($next->textContent)) {
46 5
                return null;
47
            }
48 5
            if ($next instanceof \DOMElement) {
49 5
                return $next;
50
            }
51 2
        }
52
53 9
        return null;
54
    }
55
56 5
    public static function findPrevElement(\DOMElement $element)
57
    {
58 5
        $prev = $element;
59 5
        while ($prev = $prev->previousSibling) {
60 5
            if ($prev instanceof \DOMText && trim($prev->textContent)) {
61
                return null;
62
            }
63 5
            if ($prev instanceof \DOMElement) {
64 5
                return $prev;
65
            }
66
        }
67
68
        return null;
69
    }
70
}
71