Skip to content

Commit b0b24df

Browse files
committed
update
1 parent 5664f98 commit b0b24df

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/Pairs.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace Php\Pairs\Pairs;
44

5+
use Closure;
6+
use function is_callable;
7+
58
/**
69
* Creates a pair with two values
710
* @param mixed $a first value
811
* @param mixed $b second value
9-
* @return callable pair
12+
* @return Closure pair
1013
* @example
1114
* $pair = cons(5, 'hello');
1215
* @example
1316
* $pair = cons(cons(1, null), 'world');
1417
*/
15-
function cons($a, $b)
18+
function cons($a, $b): Closure
1619
{
1720
return function ($method) use ($a, $b) {
1821
switch ($method) {
@@ -28,59 +31,64 @@ function cons($a, $b)
2831

2932
/**
3033
* Check if something is pair
31-
* @param callable $pair
34+
* @param Closure $pair
3235
* @return boolean
3336
* @example
3437
* $pair = cons(5, 'hello');
3538
* isPair($pair); // true
3639
* isPair(5); // false
3740
*/
38-
function isPair($pair)
41+
function isPair(mixed $pair): bool
3942
{
40-
return is_callable($pair);
43+
return $pair instanceof Closure;
4144
}
4245

43-
function checkPair($pair)
46+
/**
47+
* @param mixed $pair
48+
* @return void
49+
* @throws \Exception
50+
*/
51+
function checkPair(mixed $pair): void
4452
{
4553
if (!isPair($pair)) {
46-
$value = is_array($pair) ? 'array' : (string) $pair;
54+
$value = gettype($pair);
4755
throw new \Exception("Argument must be pair, but it was '{$value}'");
4856
}
4957
}
5058

5159
/**
5260
* Get car (first element) from pair
53-
* @param callable $pair
61+
* @param Closure $pair
5462
* @return mixed
5563
*/
56-
function car(callable $pair)
64+
function car(Closure $pair): mixed
5765
{
5866
checkPair($pair);
5967
return $pair("car");
6068
}
6169

6270
/**
6371
* Get cdr (second element) from pair
64-
* @param callable $pair
72+
* @param Closure $pair
6573
* @return mixed
6674
*/
67-
function cdr(callable $pair)
75+
function cdr(Closure $pair): mixed
6876
{
6977
checkPair($pair);
7078
return $pair("cdr");
7179
}
7280

7381
/**
7482
* Convert pair to string (recursively)
75-
* @param callable $pair
83+
* @param Closure $pair
7684
* @return string
7785
* @example
7886
* toString(cons('', 10)); // ('', 10)
7987
*/
80-
function toString($pair)
88+
function toString(mixed $pair): string
8189
{
8290
checkPair($pair);
83-
$rec = function ($p) use (&$rec) {
91+
$iter = function ($p) use (&$iter) {
8492
if (!isPair($p)) {
8593
if ($p === null) {
8694
return 'null';
@@ -92,11 +100,11 @@ function toString($pair)
92100
return (string) $p;
93101
}
94102

95-
$recLeft = $rec(car($p));
96-
$recRight = $rec(cdr($p));
103+
$recLeft = $iter(car($p));
104+
$recRight = $iter(cdr($p));
97105

98106
return "({$recLeft}, {$recRight})";
99107
};
100108

101-
return $rec($pair);
109+
return $iter($pair);
102110
}

tests/PairsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
class PairsTest extends TestCase
1414
{
15-
public function testPairs()
15+
public function testPairs(): void
1616
{
1717
$pair = cons(3, 4);
1818

1919
$this->assertEquals(3, car($pair));
2020
$this->assertEquals(4, cdr($pair));
2121
}
2222

23-
public function testToString()
23+
public function testToString(): void
2424
{
2525
$pair1 = cons(3, cons(3, 2));
2626
$pair2 = cons(cons(3, 5), cons(1, null));
@@ -33,11 +33,11 @@ public function testToString()
3333
$this->assertEquals("('', -10)", toString($pair4));
3434
}
3535

36-
public function testCheckPair()
36+
public function testCheckPair(): void
3737
{
38-
$this->expectExceptionMessage("Argument must be pair, but it was '1'");
38+
$this->expectExceptionMessage("Argument must be pair, but it was 'integer'");
3939
checkPair(1);
40-
$this->expectExceptionMessage("Argument must be pair, but it was 'some string'");
40+
$this->expectExceptionMessage("Argument must be pair, but it was 'string'");
4141
checkPair('some string');
4242
$this->expectExceptionMessage("Argument must be pair, but it was 'array'");
4343
checkPair([1, 2]);

0 commit comments

Comments
 (0)