Passed
Branch TEST/ScrutinizerPHPAnalysisEng... (bc3f13)
by Boudry
07:08
created

Linkable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 50
ccs 15
cts 22
cp 0.6818
rs 10
c 0
b 0
f 0

7 Methods

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