Completed
Push — master ( f5cf3a...270d64 )
by Pierce
12s
created

Quartile::arrayOnlyContainsNumbers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
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);
0 ignored issues
show
Unused Code introduced by
The call to Quartile::getQuartiles() has too many arguments starting with $scores.

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.

Loading history...
18
        }
19
    }
20
21
    /**
22
     * Calculate the quartiles
23
     *
24
     * @param array $scores
0 ignored issues
show
Bug introduced by
There is no parameter named $scores. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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