Edge::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Edge: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph;
25
26
use Pimple\Container;
27
28
class Edge extends AbstractElement {
29
30
  /**
31
   * @var Node
32
   */
33
  public $sourceNode;
34
35
  /**
36
   * @var Node
37
   */
38
  public $destinationNode;
39
40
  /**
41
   * Optional port name on source Node.
42
   *
43
   * @var string
44
   */
45
  public $sourcePort = null;
46
47
  /**
48
   * Optional port name on destination Node.
49
   *
50
   * @var string
51
   */
52
  public $destinationPort = null;
53
54
  /**
55
   * Edge need a unique id.
56
   *
57
   * This is because, multiple edges may exist between the same vertices,
58
   * port included.
59
   *
60
   * @var int
61
   */
62
  public static $sequence = 0;
63
64
  /**
65
   * @var boolean
66
   */
67
  public $fDirected = true;
68
69
  /**
70
   * @param \Pimple\Container $dic
71
   * @param \Grafizzi\Graph\Node $source
72
   * @param \Grafizzi\Graph\Node $destination
73
   * @param array $attributes
74
   * @param string $sourcePort
75
   * @param string $destinationPort
76
   *
77
   * @throws \InvalidArgumentException
78
   */
79 26
  public function __construct(Container $dic, Node $source, Node $destination,
80
    array $attributes = array(), $sourcePort = null, $destinationPort = null) {
81 26
    parent::__construct($dic);
82 26
    $this->sourceNode = $source;
83 26
    $this->destinationNode = $destination;
84 26
    $name = self::$sequence++ . '--' . $source->getName() . '--' . $destination->getName();
85 26
    if ($sourcePort && $destinationPort) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sourcePort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $destinationPort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86 5
      $this->sourcePort = $sourcePort;
87 5
      $this->destinationPort = $destinationPort;
88 5
      $name .= "--$sourcePort--$destinationPort";
89 22
    } elseif ($sourcePort || $destinationPort) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sourcePort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $destinationPort of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
      throw new \InvalidArgumentException('Both ports must be set if one is set, but you only set one.');
91
    }
92 26
    $this->setName($name);
93 26
    $this->setAttributes($attributes);
94 26
  }
95
96
  /**
97
   * @param boolean $directed
98
   *
99
   * @return string
100
   */
101 23
  public function build($directed = null) {
102 23
    $type = $this->getType();
103 23
    $name = $this->getName();
104 23
    if (!isset($directed)) {
105 4
      $directed = true;
106
    }
107 23
    $this->logger->debug("Building $type $name, depth {$this->fDepth}.");
108 23
    $ret = str_repeat(' ', $this->fDepth * self::DEPTH_INDENT)
109 23
      . $this->escape($this->sourceNode->getName())
110 23
      . (isset($this->sourcePort) ? ":$this->sourcePort" : null)
111 23
      . ($directed ? ' -> ' : ' -- ')
112 23
      . $this->escape($this->destinationNode->getName())
113 23
      . (isset($this->destinationPort) ? ":$this->destinationPort" : null);
114
115
    $attributes = array_map(function (AttributeInterface $attribute) use ($directed) {
116 12
      return $attribute->build($directed);
117 23
    }, $this->fAttributes);
118
119 23
    $ret .= $this->buildAttributes($attributes, NULL, NULL);
120 23
    return $ret;
121
  }
122
123 1
  public static function getAllowedChildTypes() {
124 1
    return array();
125
  }
126
127 26
  public function getType() {
128 26
    $ret = 'edge';
129 26
    return $ret;
130
  }
131
}
132