NodeAbstract::setLine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PhpParser;
4
5
abstract class NodeAbstract implements Node
6
{
7
    protected $attributes;
8
9
    /**
10
     * Creates a Node.
11
     *
12
     * @param array $attributes Array of attributes
13
     */
14
    public function __construct(array $attributes = array()) {
15
        $this->attributes = $attributes;
16
    }
17
18
    /**
19
     * Gets the type of the node.
20
     *
21
     * @return string Type of the node
22
     */
23
    public function getType() {
24
        return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
25
    }
26
27
    /**
28
     * Gets line the node started in.
29
     *
30
     * @return int Line
31
     */
32
    public function getLine() {
33
        return $this->getAttribute('startLine', -1);
34
    }
35
36
    /**
37
     * Sets line the node started in.
38
     *
39
     * @param int $line Line
40
     */
41
    public function setLine($line) {
42
        $this->setAttribute('startLine', (int) $line);
43
    }
44
45
    /**
46
     * Gets the doc comment of the node.
47
     *
48
     * The doc comment has to be the last comment associated with the node.
49
     *
50
     * @return null|Comment\Doc Doc comment object or null
51
     */
52
    public function getDocComment() {
53
        $comments = $this->getAttribute('comments');
54
        if (!$comments) {
55
            return null;
56
        }
57
58
        $lastComment = $comments[count($comments) - 1];
59
        if (!$lastComment instanceof Comment\Doc) {
60
            return null;
61
        }
62
63
        return $lastComment;
64
    }
65
66
    public function setAttribute($key, $value) {
67
        $this->attributes[$key] = $value;
68
    }
69
70
    public function hasAttribute($key) {
71
        return array_key_exists($key, $this->attributes);
72
    }
73
74
    public function &getAttribute($key, $default = null) {
75
        if (!array_key_exists($key, $this->attributes)) {
76
            return $default;
77
        } else {
78
            return $this->attributes[$key];
79
        }
80
    }
81
82
    public function getAttributes() {
83
        return $this->attributes;
84
    }
85
}
86