Completed
Push — master ( dc2d5c...df2031 )
by Jaap
17:57 queued 16:30
created

Node   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 109
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A create() 0 4 1
A setName() 0 5 1
A getName() 0 4 1
A __call() 0 13 3
A __toString() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * phpDocumentor
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\GraphViz;
15
16
use function addslashes;
17
use function implode;
18
use function strtolower;
19
use function substr;
20
21
/**
22
 * Class representing a node / element in a graph.
23
 *
24
 * @link      http://phpdoc.org
25
 *
26
 * @method void setLabel(string $name) Sets the label for this node.
27
 */
28
class Node
29
{
30
    use Attributes;
31
32
    /** @var string Name for this node */
33
    protected $name = '';
34
35
    /**
36
     * Creates a new node with name and optional label.
37
     *
38
     * @param string      $name  Name of the new node.
39
     * @param string|null $label Optional label text.
40
     */
41 1
    public function __construct(string $name, ?string $label = null)
42
    {
43 1
        $this->setName($name);
44 1
        if ($label === null) {
45
            return;
46
        }
47
48 1
        $this->setLabel($label);
49 1
    }
50
51
    /**
52
     * Factory method used to assist with fluent interface handling.
53
     *
54
     * See the examples for more details.
55
     *
56
     * @param string      $name  Name of the new node.
57
     * @param string|null $label Optional label text.
58
     */
59 1
    public static function create(string $name, ?string $label = null) : self
60
    {
61 1
        return new self($name, $label);
62
    }
63
64
    /**
65
     * Sets the name for this node.
66
     *
67
     * Not to confuse with the label.
68
     *
69
     * @param string $name Name for this node.
70
     */
71 1
    public function setName(string $name) : self
72
    {
73 1
        $this->name = $name;
74 1
        return $this;
75
    }
76
77
    /**
78
     * Returns the name for this node.
79
     */
80 1
    public function getName() : string
81
    {
82 1
        return $this->name;
83
    }
84
85
    /**
86
     * Magic method to provide a getter/setter to add attributes on the Node.
87
     *
88
     * Using this method we make sure that we support any attribute without
89
     * too much hassle. If the name for this method does not start with get or
90
     * set we return null.
91
     *
92
     * Set methods return this graph (fluent interface) whilst get methods
93
     * return the attribute value.
94
     *
95
     * @param string  $name      Method name; either getX or setX is expected.
96
     * @param mixed[] $arguments List of arguments; only 1 is expected for setX.
97
     *
98
     * @return Attribute|Node|null
99
     *
100
     * @throws AttributeNotFound
101
     */
102 1
    public function __call(string $name, array $arguments)
103
    {
104 1
        $key = strtolower(substr($name, 3));
105 1
        if (strtolower(substr($name, 0, 3)) === 'set') {
106 1
            return $this->setAttribute($key, (string) $arguments[0]);
107
        }
108
109 1
        if (strtolower(substr($name, 0, 3)) === 'get') {
110 1
            return $this->getAttribute($key);
111
        }
112
113 1
        return null;
114
    }
115
116
    /**
117
     * Returns the node definition as is requested by GraphViz.
118
     */
119 2
    public function __toString() : string
120
    {
121 2
        $attributes = [];
122 2
        foreach ($this->attributes as $value) {
123 2
            $attributes[] = (string) $value;
124
        }
125
126 2
        $attributes = implode("\n", $attributes);
127
128 2
        $name = addslashes($this->getName());
129
130
        return <<<DOT
131 2
"{$name}" [
132 2
${attributes}
133
]
134
DOT;
135
    }
136
}
137