BuilderAbstract   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
c 2
b 0
f 0
lcom 1
cbo 12
dl 0
loc 121
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setModifier() 0 4 1
A normalizeNode() 0 9 3
B normalizeName() 0 19 6
C normalizeValue() 0 40 12
A normalizeDocComment() 0 9 3
1
<?php
2
3
namespace PhpParser;
4
5
use PhpParser\Node\Name;
6
use PhpParser\Node\Expr;
7
use PhpParser\Node\Stmt;
8
use PhpParser\Node\Scalar;
9
use PhpParser\Comment;
10
11
abstract class BuilderAbstract implements Builder {
12
    /**
13
     * Normalizes a node: Converts builder objects to nodes.
14
     *
15
     * @param Node|Builder $node The node to normalize
16
     *
17
     * @return Node The normalized node
18
     */
19
    protected function normalizeNode($node) {
20
        if ($node instanceof Builder) {
21
            return $node->getNode();
22
        } elseif ($node instanceof Node) {
23
            return $node;
24
        }
25
26
        throw new \LogicException('Expected node or builder object');
27
    }
28
29
    /**
30
     * Normalizes a name: Converts plain string names to PhpParser\Node\Name.
31
     *
32
     * @param Name|string $name The name to normalize
33
     *
34
     * @return Name The normalized name
35
     */
36
    protected function normalizeName($name) {
37
        if ($name instanceof Name) {
38
            return $name;
39
        } elseif (is_string($name)) {
40
            if (!$name) {
41
                throw new \LogicException('Name cannot be empty');
42
            }
43
44
            if ($name[0] == '\\') {
45
                return new Name\FullyQualified(substr($name, 1));
46
            } elseif (0 === strpos($name, 'namespace\\')) {
47
                return new Name\Relative(substr($name, strlen('namespace\\')));
48
            } else {
49
                return new Name($name);
50
            }
51
        }
52
53
        throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name');
54
    }
55
56
    /**
57
     * Normalizes a value: Converts nulls, booleans, integers,
58
     * floats, strings and arrays into their respective nodes
59
     *
60
     * @param mixed $value The value to normalize
61
     *
62
     * @return Expr The normalized value
63
     */
64
    protected function normalizeValue($value) {
65
        if ($value instanceof Node) {
66
            return $value;
67
        } elseif (is_null($value)) {
68
            return new Expr\ConstFetch(
69
                new Name('null')
70
            );
71
        } elseif (is_bool($value)) {
72
            return new Expr\ConstFetch(
73
                new Name($value ? 'true' : 'false')
74
            );
75
        } elseif (is_int($value)) {
76
            return new Scalar\LNumber($value);
77
        } elseif (is_float($value)) {
78
            return new Scalar\DNumber($value);
79
        } elseif (is_string($value)) {
80
            return new Scalar\String_($value);
81
        } elseif (is_array($value)) {
82
            $items = array();
83
            $lastKey = -1;
84
            foreach ($value as $itemKey => $itemValue) {
85
                // for consecutive, numeric keys don't generate keys
86
                if (null !== $lastKey && ++$lastKey === $itemKey) {
87
                    $items[] = new Expr\ArrayItem(
88
                        $this->normalizeValue($itemValue)
89
                    );
90
                } else {
91
                    $lastKey = null;
92
                    $items[] = new Expr\ArrayItem(
93
                        $this->normalizeValue($itemValue),
94
                        $this->normalizeValue($itemKey)
95
                    );
96
                }
97
            }
98
99
            return new Expr\Array_($items);
100
        } else {
101
            throw new \LogicException('Invalid value');
102
        }
103
    }
104
105
    /**
106
     * Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc.
107
     *
108
     * @param Comment\Doc|string $docComment The doc comment to normalize
109
     *
110
     * @return Comment\Doc The normalized doc comment
111
     */
112
    protected function normalizeDocComment($docComment) {
113
        if ($docComment instanceof Comment\Doc) {
114
            return $docComment;
115
        } else if (is_string($docComment)) {
116
            return new Comment\Doc($docComment);
117
        } else {
118
            throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
119
        }
120
    }
121
122
    /**
123
     * Sets a modifier in the $this->type property.
124
     *
125
     * @param int $modifier Modifier to set
126
     */
127
    protected function setModifier($modifier) {
128
        Stmt\Class_::verifyModifier($this->type, $modifier);
0 ignored issues
show
Bug introduced by
The property type 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...
129
        $this->type |= $modifier;
130
    }
131
}
132