Completed
Push — IncomprehensibleFinder/marcos ( 3df5a7 )
by Marcos
05:21 queued 03:01
created

Finder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 84
c 0
b 0
f 0
ccs 31
cts 31
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B find() 0 31 6
A sortFromOlderToYounger() 0 17 2
A firstIsOlder() 0 4 1
A addAgeDifference() 0 6 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 $all_people)
13
    {
14 6
        $this->people = $all_people;
15 6
    }
16
17 6
    public function find(int $sorting_type): Comparator
18
    {
19
        /** @var Comparator[] $sorted_people_from_older_to_younger */
20 6
        $sorted_people_from_older_to_younger = [];
21
22 6
        foreach ($this->people as $index => $person)
23
        {
24 5
            for ($j = $index + 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...
25
            {
26 4
                $person_to_compare_with = $this->people[$j];
27 4
                $current_couple         = $this->sortFromOlderToYounger($person, $person_to_compare_with);
28
29 4
                $sorted_people_from_older_to_younger[] = $current_couple;
30
            }
31
        }
32
33 6
        if (empty($sorted_people_from_older_to_younger))
34
        {
35 2
            return new Comparator();
36
        }
37
38 4
        $first_couple = $sorted_people_from_older_to_younger[0];
39
40 4
        $sorting_algorithm = $sorting_type == 1 ? SortClosest::class : SortFurthest::class;
41 4
        foreach ($sorted_people_from_older_to_younger as $current_couple)
42
        {
43 4
            $first_couple = (new $sorting_algorithm())->__invoke($current_couple, $first_couple);
44
        }
45
46 4
        return $first_couple;
47
    }
48
49
    /**
50
     * @param $person
51
     * @param $person_to_compare_with
52
     *
53
     * @return Comparator
54
     */
55 4
    private function sortFromOlderToYounger($person, $person_to_compare_with): Comparator
56
    {
57 4
        $result = new Comparator();
58 4
        if ($this->firstIsOlder($person, $person_to_compare_with))
59
        {
60 3
            $result->first_person  = $person;
61 3
            $result->second_person = $person_to_compare_with;
62
        }
63
        else
64
        {
65 3
            $result->first_person  = $person_to_compare_with;
66 3
            $result->second_person = $person;
67
        }
68 4
        $this->addAgeDifference($result);
69
70 4
        return $result;
71
    }
72
73
    /**
74
     * @param $person
75
     * @param $person_to_compare_with
76
     *
77
     * @return bool
78
     */
79 4
    private function firstIsOlder($person, $person_to_compare_with): bool
80
    {
81 4
        return $person->birthDate < $person_to_compare_with->birthDate;
82
    }
83
84 4
    private function addAgeDifference($result)
85
    {
86 4
        $result->age_difference = $result->second_person->birthDate->getTimestamp() - $result->first_person->birthDate->getTimestamp();
87
88 4
        return $result;
89
    }
90
}
91