Conditions | 9 |
Paths | 21 |
Total Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 9.0117 |
Changes | 0 |
1 | <?php |
||
19 | 6 | public function find(int $criteria): AgeComparatorResult |
|
20 | { |
||
21 | /** @var AgeComparatorResult[] $results */ |
||
22 | 6 | $results = []; |
|
23 | 6 | $number_of_people = count($this->people); |
|
24 | |||
25 | 6 | foreach ($this->people as $index => $person) { |
|
26 | 5 | for ($j = $index + 1; $j < $number_of_people; $j++) { |
|
27 | 4 | $results[] = $this->compareTwoBirthDates($person, $j); |
|
28 | } |
||
29 | } |
||
30 | |||
31 | 6 | if (count($results) < 1) { |
|
32 | 2 | throw new \InvalidArgumentException('Add at least two people'); |
|
33 | } |
||
34 | |||
35 | 4 | $answer = $results[0]; |
|
36 | |||
37 | 4 | foreach ($results as $result) { |
|
38 | switch ($criteria) { |
||
39 | 4 | case Criteria::CLOSEST: |
|
40 | 2 | if ($result->age_difference < $answer->age_difference) { |
|
41 | 1 | $answer = $result; |
|
42 | } |
||
43 | 2 | break; |
|
44 | |||
45 | 2 | case Criteria::FURTHEST: |
|
46 | 2 | if ($result->age_difference > $answer->age_difference) { |
|
47 | $answer = $result; |
||
48 | } |
||
49 | 2 | break; |
|
50 | } |
||
51 | } |
||
52 | |||
53 | 4 | return $answer; |
|
54 | } |
||
55 | |||
67 |