1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PierceMcGeough\phpquartiles; |
4
|
|
|
|
5
|
|
|
class Quartile |
6
|
|
|
{ |
7
|
|
|
public $scores; |
8
|
|
|
public $quartiles; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Create a new Quartiles Instance |
12
|
|
|
*/ |
13
|
|
|
public function __construct($scores = null) |
14
|
|
|
{ |
15
|
|
|
if ($this->arrayOnlyContainsNumbers($scores) === true) { |
16
|
|
|
$this->scores = $scores; |
17
|
|
|
$this->quartiles = $this->getQuartiles($scores); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Calculate the quartiles |
23
|
|
|
* |
24
|
|
|
* @param array $scores |
|
|
|
|
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function getQuartiles() |
29
|
|
|
{ |
30
|
|
|
if (count($this->scores)+1 <= 3) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
sort($this->scores, SORT_NUMERIC); |
35
|
|
|
|
36
|
|
|
return [ |
37
|
|
|
'lowest' => $this->getQuartile($this->scores, 0.25), |
38
|
|
|
'third' => $this->getQuartile($this->scores, 0.50), |
39
|
|
|
'second' => $this->getQuartile($this->scores, 0.75) |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Use the params to work out the quartile specific to this $array |
46
|
|
|
* |
47
|
|
|
* @param $array |
48
|
|
|
* @param $quartilePlace |
49
|
|
|
* |
50
|
|
|
* @return float |
51
|
|
|
*/ |
52
|
|
|
public function getQuartile($array, $quartilePlace) |
53
|
|
|
{ |
54
|
|
|
$pos = (count($array) + 1) * $quartilePlace; |
55
|
|
|
|
56
|
|
|
if (fmod($pos, 1) == 0) { |
57
|
|
|
return $array[$pos-1]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$fraction = $pos - floor($pos); |
61
|
|
|
|
62
|
|
|
$lower_num = $array[floor($pos) - 1]; |
63
|
|
|
$upper_num = $array[ceil($pos) - 1]; |
64
|
|
|
|
65
|
|
|
$difference = $upper_num - $lower_num; |
66
|
|
|
|
67
|
|
|
return round($lower_num + ($difference * $fraction), 2); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function arrayOnlyContainsNumbers($scores) |
71
|
|
|
{ |
72
|
|
|
foreach ($scores as $score) { |
73
|
|
|
if (!is_numeric($score)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.