|
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
|
|
|
/* |
|
|
|
|
|
|
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
|
|
|
|
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.