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

Linkable::countLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 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