1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Kata\Algorithm; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use function dump; |
9
|
|
|
|
10
|
|
|
final class Finder |
11
|
|
|
{ |
12
|
|
|
/** @var Person[] */ |
13
|
|
|
private $peopleByAgeDifferenceCollection; |
14
|
|
|
|
15
|
6 |
|
public function __construct(PeopleByAgeDifferenceCollection $peopleByAgeDifferenceCollection) |
16
|
|
|
{ |
17
|
6 |
|
$this->peopleByAgeDifferenceCollection = $peopleByAgeDifferenceCollection; |
|
|
|
|
18
|
6 |
|
} |
19
|
|
|
|
20
|
6 |
|
public function find(int $finderCriteria): PeopleByAgeDifference |
21
|
|
|
{ |
22
|
|
|
/** @var PeopleByAgeDifference[] $tr */ |
23
|
6 |
|
$tr = []; |
24
|
|
|
|
25
|
|
|
/** @var Person $firstPerson */ |
26
|
6 |
|
foreach ($this->peopleByAgeDifferenceCollection as $firstPerson) { |
27
|
|
|
/** @var Person $secondPerson */ |
28
|
5 |
|
foreach ($this->peopleByAgeDifferenceCollection as $secondPerson) { |
29
|
5 |
|
if ($firstPerson->equals($secondPerson)){ |
|
|
|
|
30
|
5 |
|
continue; |
31
|
|
|
} |
32
|
4 |
|
$peopleByAgeDifference = new PeopleByAgeDifference(); |
33
|
|
|
|
34
|
4 |
|
if ($this->isFirstBirthdaySmallerThanSecondOne( |
35
|
4 |
|
$firstPerson->birthDate, |
36
|
4 |
|
$secondPerson->birthDate |
37
|
|
|
) |
38
|
|
|
) { |
39
|
4 |
|
$peopleByAgeDifference->firstPerson = $firstPerson; |
40
|
4 |
|
$peopleByAgeDifference->secondPerson = $secondPerson; |
41
|
|
|
} else { |
42
|
4 |
|
$peopleByAgeDifference->firstPerson = $secondPerson; |
43
|
4 |
|
$peopleByAgeDifference->secondPerson = $firstPerson; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$peopleByAgeDifference->timeDifference = $peopleByAgeDifference->secondPerson->birthDate->getTimestamp() |
47
|
4 |
|
- $peopleByAgeDifference->firstPerson->birthDate->getTimestamp(); |
48
|
|
|
|
49
|
4 |
|
$tr[] = $peopleByAgeDifference; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
if (count($tr) < 1) { |
55
|
2 |
|
return new PeopleByAgeDifference(); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
$answer = $tr[0]; |
59
|
|
|
|
60
|
4 |
|
foreach ($tr as $result) { |
61
|
|
|
switch ($finderCriteria) { |
62
|
4 |
|
case FinderCriteria::CLOSEST: |
63
|
2 |
|
if ($result->timeDifference < $answer->timeDifference) { |
64
|
1 |
|
$answer = $result; |
65
|
|
|
} |
66
|
2 |
|
break; |
67
|
|
|
|
68
|
2 |
|
case FinderCriteria::FURTHEST: |
69
|
2 |
|
if ($result->timeDifference > $answer->timeDifference) { |
70
|
|
|
$answer = $result; |
71
|
|
|
} |
72
|
2 |
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
return $answer; |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
private function isFirstBirthdaySmallerThanSecondOne( |
80
|
|
|
DateTime $firstBirthday, |
81
|
|
|
DateTime $secondBirthday |
82
|
|
|
): bool { |
83
|
4 |
|
return $firstBirthday < $secondBirthday; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..