Passed
Push — master ( 2a8e0f...9ee175 )
by Boudry
03:21
created

Linkable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 52
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
A haveLink() 0 4 1
A countLinks() 0 4 1
A getLinks() 0 4 2
A registerLink() 0 7 2
A destroyLink() 0 11 2
A destroyAllLink() 0 4 1
1
<?php
2
/*
3
    Condorcet PHP Class, with Schulze Methods and others !
4
5
    By Julien Boudry - MIT LICENSE (Please read LICENSE.txt)
6
    https://github.com/julien-boudry/Condorcet
7
*/
8
declare(strict_types=1);
9
10
namespace Condorcet;
11
12
use Condorcet\CondorcetException;
13
use Condorcet\Election;
14
15
trait Linkable
16
{
17
    private $_link = [];
18
19
    public function __clone ()
20
    {
21
        $this->destroyAllLink();
22
    }
23
24 82
    public function haveLink (Election $election) : bool
25
    {
26 82
        return in_array($election, $this->_link, true);
27
    }
28
29 90
    public function countLinks () : int
30
    {
31 90
        return count($this->_link);
32
    }
33
34 91
    public function getLinks () : ?array
35
    {
36 91
        return (!empty($this->_link)) ? $this->_link : null;
37
    }
38
39
    // Internal
40
        # Dot not Overloading ! Do not Use !
41
42 95
    public function registerLink (Election $election) : void
43
    {
44 95
        if (array_search($election, $this->_link, true) === false)
45 95
            { $this->_link[] = $election; }
46
        else
47
            { throw new CondorcetException; }
48 95
    }
49
50 9
    public function destroyLink (Election $election) : bool
51
    {
52 9
        $destroyKey = array_search($election, $this->_link, true);
53
54 9
        if ($destroyKey !== false) :
55 9
            unset($this->_link[$destroyKey]);
56 9
            return true;
57
        else :
58
            throw new CondorcetException;
59
        endif;
60
    }
61
62 1
    protected function destroyAllLink () : void
63
    {
64 1
        $this->_link = [];
65 1
    }
66
}
67