Skip to content

Commit bf8ccf1

Browse files
committed
Translate Incrompehensible Finder Refactoring kata base code to PHP
1 parent 0d29f9b commit bf8ccf1

File tree

5 files changed

+238
-3
lines changed

5 files changed

+238
-3
lines changed

src/Algorithm/F.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CodelyTV\FinderKata\Algorithm;
6+
7+
final class F
8+
{
9+
/** @var Thing */
10+
public $p1;
11+
12+
/** @var Thing */
13+
public $p2;
14+
15+
/** @var int */
16+
public $d;
17+
}

src/Algorithm/FT.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CodelyTV\FinderKata\Algorithm;
6+
7+
interface FT
8+
{
9+
const ONE = 1;
10+
const TWO = 2;
11+
}

src/Algorithm/Finder.php

+57
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,65 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
35
namespace CodelyTV\FinderKata\Algorithm;
46

57
final class Finder
68
{
9+
/** @var Thing[] */
10+
private $_p;
11+
12+
public function __construct(array $p)
13+
{
14+
$this->_p = $p;
15+
}
16+
17+
public function find(int $ft): F
18+
{
19+
/** @var F[] $tr */
20+
$tr = [];
21+
22+
for ($i = 0; $i < count($this->_p); $i++) {
23+
for ($j = $i + 1; $j < count($this->_p); $j++) {
24+
$r = new F();
25+
26+
if ($this->_p[$i]->birthDate < $this->_p[$j]->birthDate) {
27+
$r->p1 = $this->_p[$i];
28+
$r->p2 = $this->_p[$j];
29+
} else {
30+
$r->p1 = $this->_p[$j];
31+
$r->p2 = $this->_p[$i];
32+
}
33+
34+
$r->d = $r->p2->birthDate->getTimestamp()
35+
- $r->p1->birthDate->getTimestamp();
36+
37+
$tr[] = $r;
38+
}
39+
}
40+
41+
if (count($tr) < 1) {
42+
return new F();
43+
}
44+
45+
$answer = $tr[0];
46+
47+
foreach ($tr as $result) {
48+
switch ($ft) {
49+
case FT::ONE:
50+
if ($result->d < $answer->d) {
51+
$answer = $result;
52+
}
53+
break;
54+
55+
case FT::TWO:
56+
if ($result->d > $answer->d) {
57+
$answer = $result;
58+
}
59+
break;
60+
}
61+
}
762

63+
return $answer;
64+
}
865
}

src/Algorithm/Thing.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CodelyTV\FinderKata\Algorithm;
6+
7+
use DateTime;
8+
9+
final class Thing
10+
{
11+
/** @var string */
12+
public $name;
13+
14+
/** @var DateTime */
15+
public $birthDate;
16+
17+
public function getName(): string
18+
{
19+
return $this->name;
20+
}
21+
22+
public function setName(string $name)
23+
{
24+
$this->name = $name;
25+
}
26+
27+
public function getBirthDate(): DateTime
28+
{
29+
return $this->birthDate;
30+
}
31+
32+
public function setBirthDate(DateTime $birthDate)
33+
{
34+
$this->birthDate = $birthDate;
35+
}
36+
}

tests/Algorithm/FinderTest.php

+117-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,131 @@
11
<?php
22

3+
declare(strict_types = 1);
4+
35
namespace CodelyTV\FinderKataTest\Algorithm;
46

57
use CodelyTV\FinderKata\Algorithm\Finder;
8+
use CodelyTV\FinderKata\Algorithm\FT;
9+
use CodelyTV\FinderKata\Algorithm\Thing;
610
use PHPUnit\Framework\TestCase;
711

812
final class FinderTest extends TestCase
913
{
14+
/** @var Thing */
15+
private $sue;
16+
17+
/** @var Thing */
18+
private $greg;
19+
20+
/** @var Thing */
21+
private $sarah;
22+
23+
/** @var Thing */
24+
private $mike;
25+
26+
protected function setUp()
27+
{
28+
$this->sue = new Thing();
29+
$this->sue->name = "Sue";
30+
$this->sue->birthDate = new \DateTime("1950-01-01");
31+
32+
$this->greg = new Thing();
33+
$this->greg->name = "Greg";
34+
$this->greg->birthDate = new \DateTime("1952-05-01");
35+
36+
$this->sarah = new Thing();
37+
$this->sarah->name = "Sarah";
38+
$this->sarah->birthDate = new \DateTime("1982-01-01");
39+
40+
$this->mike = new Thing();
41+
$this->mike->name = "Mike";
42+
$this->mike->birthDate = new \DateTime("1979-01-01");
43+
}
44+
1045
/** @test */
11-
public function should_be_able_to_instantiate()
46+
public function should_return_empty_when_given_empty_list()
1247
{
13-
$finder = new Finder();
48+
$list = [];
49+
$finder = new Finder($list);
50+
51+
$result = $finder->find(FT::ONE);
52+
53+
$this->assertEquals(null, $result->p1);
54+
$this->assertEquals(null, $result->p2);
55+
}
56+
57+
/** @test */
58+
public function should_return_empty_when_given_one_person()
59+
{
60+
$list = [];
61+
$list[] = $this->sue;
62+
$finder = new Finder($list);
63+
64+
$result = $finder->find(FT::ONE);
65+
66+
$this->assertEquals(null, $result->p1);
67+
$this->assertEquals(null, $result->p2);
68+
}
69+
70+
/** @test */
71+
public function should_return_closest_two_for_two_people()
72+
{
73+
$list = [];
74+
$list[] = $this->sue;
75+
$list[] = $this->greg;
76+
$finder = new Finder($list);
77+
78+
$result = $finder->find(FT::ONE);
79+
80+
$this->assertEquals($this->sue, $result->p1);
81+
$this->assertEquals($this->greg, $result->p2);
82+
}
83+
84+
/** @test */
85+
public function should_return_furthest_two_for_two_people()
86+
{
87+
$list = [];
88+
$list[] = $this->mike;
89+
$list[] = $this->greg;
90+
$finder = new Finder($list);
91+
92+
$result = $finder->find(FT::TWO);
93+
94+
$this->assertEquals($this->greg, $result->p1);
95+
$this->assertEquals($this->mike, $result->p2);
96+
}
97+
98+
/** @test */
99+
public function should_return_furthest_two_for_four_people()
100+
{
101+
$list = [];
102+
$list[] = $this->sue;
103+
$list[] = $this->sarah;
104+
$list[] = $this->mike;
105+
$list[] = $this->greg;
106+
$finder = new Finder($list);
107+
108+
$result = $finder->find(FT::TWO);
109+
110+
$this->assertEquals($this->sue, $result->p1);
111+
$this->assertEquals($this->sarah, $result->p2);
112+
}
113+
114+
/**
115+
* @test
116+
*/
117+
public function should_return_closest_two_for_four_people()
118+
{
119+
$list = [];
120+
$list[] = $this->sue;
121+
$list[] = $this->sarah;
122+
$list[] = $this->mike;
123+
$list[] = $this->greg;
124+
$finder = new Finder($list);
125+
126+
$result = $finder->find(FT::ONE);
14127

15-
$this->assertInstanceOf(Finder::class, $finder);
128+
$this->assertEquals($this->sue, $result->p1);
129+
$this->assertEquals($this->greg, $result->p2);
16130
}
17131
}

0 commit comments

Comments
 (0)