LoopTrait::hasLoop()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the bisarca/graph package.
5
 *
6
 * (c) Emanuele Minotto <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bisarca\Graph\Graph\Descriptor;
13
14
use Bisarca\Graph\Edge\Set;
15
use Bisarca\Graph\Vertex\VertexInterface;
16
17
trait LoopTrait
18
{
19
    /**
20
     * Gets the edges set.
21
     *
22
     * @return Set
23
     */
24
    abstract public function getEdgeSet(): Set;
25
26
    /**
27
     * Checks if the graph has an edge that is a loop.
28
     *
29
     * @return bool
30
     */
31
    public function hasLoop(): bool
32
    {
33
        $loop = false;
34
35
        foreach ($this->getEdgeSet() as $edge) {
36
            $loop = $loop || $edge->isLoop();
37
        }
38
39
        return $loop;
40
    }
41
42
    /**
43
     * Checks if the graph has an edge that is a loop and with a vertex $vertex.
44
     *
45
     * @param VertexInterface $vertex
46
     *
47
     * @return bool
48
     */
49
    public function hasLoopOn(VertexInterface $vertex): bool
50
    {
51
        $loop = false;
52
53
        foreach ($this->getEdgeSet() as $edge) {
54
            $loop = $loop || (
55
                $edge->isLoop() &&
56
                $vertex === $edge->getVertexStart()
57
            );
58
        }
59
60
        return $loop;
61
    }
62
}
63