Passed
Branch dev-1.5.x (fb93e4)
by Boudry
04:14
created

Linkable::destroyAllLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
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
// Generic for Candidate & Vote Class
16
trait Linkable
17
{
18
    private $_link = [];
19
20
    public function __clone ()
21
    {
22
        $this->destroyAllLink();
23
    }
24
25
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
    public function __debugInfo () : array
27
    {
28
        $var = get_object_vars($this);
29
30
        foreach ($var['_link'] as $key => $oneLink)
31
        {
32
            $var['_link'][$key] = 'Object '.get_class($oneLink).' => '.sha1(spl_object_hash($oneLink));
33
        }
34
35
        return $var;
36
    }
37
    */
38
39 4
    public function haveLink (Election $election) : bool
40
    {
41 4
        return in_array($election, $this->_link, true);
42
    }
43
44 4
    public function countLinks () : int
45
    {
46 4
        return count($this->_link);
47
    }
48
49 4
    public function getLinks () : ?array
50
    {
51 4
        return (!empty($this->_link)) ? $this->_link : null;
52
    }
53
54
    // Internal
55
        # Dot not Overloading ! Do not Use !
56
57 4
    public function registerLink (Election $election) : void
58
    {
59 4
        if (array_search($election, $this->_link, true) === false)
60 4
            { $this->_link[] = $election; }
61
        else
62
            { throw new CondorcetException; }
63 4
    }
64
65 3
    public function destroyLink (Election $election) : bool
66
    {
67 3
        $destroyKey = array_search($election, $this->_link, true);
68
69 3
        if ($destroyKey !== false) :
70 3
            unset($this->_link[$destroyKey]);
71 3
            return true;
72
        else :
73
            throw new CondorcetException;
74
        endif;
75
    }
76
77
    protected function destroyAllLink () : void
78
    {
79
        $this->_link = [];
80
    }
81
}
82