Completed
Push — master ( e86a88...9f06ff )
by Boudry
04:59 queued 02:04
created

Linkable::destroyLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
namespace Condorcet;
12
13
use Condorcet\CondorcetException;
14
use Condorcet\Election;
15
16
trait Linkable
17
{
18
    private $_link = [];
19
20 1
    public function __clone ()
21
    {
22 1
        $this->destroyAllLink();
23 1
    }
24
25 88
    public function haveLink (Election $election) : bool
26
    {
27 88
        return in_array($election, $this->_link, true);
28
    }
29
30 102
    public function countLinks () : int
31
    {
32 102
        return count($this->_link);
33
    }
34
35 102
    public function getLinks () : ?array
36
    {
37 102
        return (!empty($this->_link)) ? $this->_link : null;
38
    }
39
40
    // Internal
41
        # Dot not Overloading ! Do not Use !
42
43 116
    public function registerLink (Election $election) : void
44
    {
45 116
        if (array_search($election, $this->_link, true) === false)
46 116
            { $this->_link[] = $election; }
47
        else
48
            { throw new CondorcetException; }
49 116
    }
50
51 12
    public function destroyLink (Election $election) : bool
52
    {
53 12
        $destroyKey = array_search($election, $this->_link, true);
54
55 12
        if ($destroyKey !== false) :
56 11
            unset($this->_link[$destroyKey]);
57 11
            return true;
58
        else :
59 7
            return false;
60
        endif;
61
    }
62
63 3
    protected function destroyAllLink () : void
64
    {
65 3
        $this->_link = [];
66 3
    }
67
}
68