Completed
Push — dev-1.6.x ( e86a88...4ab371 )
by Boudry
03:36
created

Linkable::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 88
    public function haveLink (Election $election) : bool
25
    {
26 88
        return in_array($election, $this->_link, true);
27
    }
28
29 99
    public function countLinks () : int
30
    {
31 99
        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 111
    public function registerLink (Election $election) : void
43
    {
44 111
        if (array_search($election, $this->_link, true) === false)
45 111
            { $this->_link[] = $election; }
46
        else
47
            { throw new CondorcetException; }
48 111
    }
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 2
    protected function destroyAllLink () : void
63
    {
64 2
        $this->_link = [];
65 2
    }
66
}
67