Completed
Push — IncomprehensibleFinder/xavi ( 4f7b40...c40efa )
by
unknown
02:14
created

Finder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 64
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C find() 0 48 10
A isFirstPersonMoreYoungerThanSecondPerson() 0 4 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 $ft): F
18
    {
19
        /** @var F[] $tr */
20 6
        $tr = [];
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
                $r = new F();
25
26 4
                if ($this->isFirstPersonMoreYoungerThanSecondPerson($i, $j)) {
27 3
                    $r->first_person = $this->people[$i];
28 3
                    $r->second_person = $this->people[$j];
29
                } else {
30 3
                    $r->first_person = $this->people[$j];
31 3
                    $r->second_person = $this->people[$i];
32
                }
33
34 4
                $r->birthday_difference = $r->second_person->birthDate->getTimestamp()
35 4
                    - $r->first_person->birthDate->getTimestamp();
36
37 4
                $tr[] = $r;
38
            }
39
        }
40
41 6
        if (count($tr) < 1) {
42 2
            return new F();
43
        }
44
45 4
        $answer = $tr[0];
46
47 4
        foreach ($tr as $result) {
48
            switch ($ft) {
49 4
                case FT::ONE:
50 2
                    if ($result->birthday_difference < $answer->birthday_difference) {
51 1
                        $answer = $result;
52
                    }
53 2
                    break;
54
55 2
                case FT::TWO:
56 2
                    if ($result->birthday_difference > $answer->birthday_difference) {
57
                        $answer = $result;
58
                    }
59 2
                    break;
60
            }
61
        }
62
63 4
        return $answer;
64
    }
65
66 4
    private function isFirstPersonMoreYoungerThanSecondPerson($first_person, $second_person): bool
67
    {
68 4
        return $this->people[$first_person]->birthDate < $this->people[$second_person]->birthDate;
69
    }
70
}
71