Completed
Push — dev-1.6.x ( f43552...3412fc )
by Boudry
16:50
created

Linkable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95.65%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A haveLink() 0 4 1
A countLinks() 0 4 1
A getLinks() 0 4 2
A registerLink() 0 7 2
A destroyAllLink() 0 4 1
A destroyLink() 0 11 2
A __clone() 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 1
    public function __clone ()
20
    {
21 1
        $this->destroyAllLink();
22 1
    }
23
24 88
    public function haveLink (Election $election) : bool
25
    {
26 88
        return in_array($election, $this->_link, true);
27
    }
28
29 100
    public function countLinks () : int
30
    {
31 100
        return count($this->_link);
32
    }
33
34 100
    public function getLinks () : ?array
35
    {
36 100
        return (!empty($this->_link)) ? $this->_link : null;
37
    }
38
39
    // Internal
40
        # Dot not Overloading ! Do not Use !
41
42 114
    public function registerLink (Election $election) : void
43
    {
44 114
        if (array_search($election, $this->_link, true) === false)
45 114
            { $this->_link[] = $election; }
46
        else
47
            { throw new CondorcetException; }
48 114
    }
49
50 11
    public function destroyLink (Election $election) : bool
51
    {
52 11
        $destroyKey = array_search($election, $this->_link, true);
53
54 11
        if ($destroyKey !== false) :
55 10
            unset($this->_link[$destroyKey]);
56 10
            return true;
57
        else :
58 7
            return false;
59
        endif;
60
    }
61
62 3
    protected function destroyAllLink () : void
63
    {
64 3
        $this->_link = [];
65 3
    }
66
}
67