ImportNode::visit()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.1979

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 17
cp 0.8235
rs 8.9297
c 0
b 0
f 0
cc 6
nc 7
nop 2
crap 6.1979
1
<?php
2
namespace Goetas\Twital\Node;
3
4
use Goetas\Twital\Compiler;
5
use Goetas\Twital\Exception;
6
use Goetas\Twital\Node;
7
8
/**
9
 *
10
 * @author Asmir Mustafic <[email protected]>
11
 *
12
 */
13
class ImportNode implements Node
14
{
15 6
    public function visit(\DOMElement $node, Compiler $context)
16
    {
17 6
        if ($node->hasAttribute("from-exp")) {
18
            $filename = $node->getAttribute("from-exp");
19 6
        } elseif ($node->hasAttribute("from")) {
20 6
            $filename = '"' . $node->getAttribute("from") . '"';
21 6
        } else {
22
            throw new Exception("The 'from' or 'from-exp' attribute is required");
23
        }
24
25 6
        if ($node->hasAttribute("as")) {
26 3
            $code = "import $filename as " . $node->getAttribute("as");
27 3
            $context->createControlNode("import " . ($node->getAttribute("fro-exp") ? $node->getAttribute("name-exp") : ("'" . $node->getAttribute("name") . "'")) . " as " . $node->getAttribute("as"));
28 6
        } elseif ($node->hasAttribute("aliases")) {
29 3
            $code = "from $filename import " . $node->getAttribute("aliases");
30 3
        } else {
31
            throw new Exception("As or Alias attribute is required");
32
        }
33
34 6
        $pi = $context->createControlNode($code);
35
36 6
        $node->parentNode->replaceChild($pi, $node);
37 6
    }
38
}
39