Completed
Push — IncomprehensibleFinder/xavi ( fbab92...a3476a )
by
unknown
02:27
created

Finder::isFirstPersonMoreYoungerThanSecondPerson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kata\Algorithm;
6
7
final class Finder
8
{
9
    /** @var Person[] */
10
    private $people;
11
12 6
    public function __construct(array $a_people)
13
    {
14 6
        $this->people = $a_people;
15 6
    }
16
17 6
    public function find(int $birthday_sort): PeopleComparison
18
    {
19
        /** @var PeopleComparison[] $people_comparisons */
20 6
        $people_comparisons = [];
21
22 6
        for ($i = 0; $i < count($this->people); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
23 5
            for ($j = $i + 1; $j < count($this->people); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
24 4
                $current_people_comparison = new PeopleComparison();
25
26 4
                $first_person = $this->people[$i];
27 4
                $second_person = $this->people[$j];
28
29 4
                if ($this->isFirstPersonMoreYoungerThanSecondPerson($i, $j)) {
30 3
                    $current_people_comparison->setFirstPerson($first_person);
31 3
                    $current_people_comparison->setSecondPerson($second_person);
32
                } else {
33 3
                    $current_people_comparison->setFirstPerson($second_person);
34 3
                    $current_people_comparison->setSecondPerson($first_person);
35
                }
36
37 4
                $current_people_comparison->setBirthdayDifference($current_people_comparison->secondPerson()->birthDate()->getTimestamp()
38 4
                    - $current_people_comparison->firstPerson()->birthDate()->getTimestamp());
39
40 4
                $people_comparisons[] = $current_people_comparison;
41
            }
42
        }
43
44 6
        if (empty($people_comparisons)) {
45 2
            return new PeopleComparison();
46
        }
47
48 4
        return $this->getPeopleComparisonWith($birthday_sort, $people_comparisons);
49
    }
50
51 4
    private function isFirstPersonMoreYoungerThanSecondPerson($first_person_index, $second_person_index): bool
52
    {
53 4
        return $this->people[$first_person_index]->birthDate() < $this->people[$second_person_index]->birthDate();
54
    }
55
56 4
    private function getPeopleComparisonWith(int $birthday_sort, array $people_comparisons)
57
    {
58 4
        $answer = $people_comparisons[0];
59
60 4
        foreach ($people_comparisons as $result) {
61
            switch ($birthday_sort) {
62 4
                case BirthdaySort::CLOSEST:
63 2
                    if ($result->birthdayDifference() < $answer->birthdayDifference()) {
64 1
                        $answer = $result;
65
                    }
66 2
                    break;
67
68 2
                case BirthdaySort::FURTHEST:
69 2
                    if ($result->birthdayDifference() > $answer->birthdayDifference()) {
70
                        $answer = $result;
71
                    }
72 2
                    break;
73
            }
74
        }
75
76 4
        return $answer;
77
    }
78
}
79