Skip to content

Commit 69978ed

Browse files
committed
Merge remote-tracking branch 'lstrojny/feature/placeholder'
* lstrojny/feature/placeholder: Adding tests where parameter position matters Using annotations in tests Removing Util API Introduce function API Adding Util::placeholder() as alias for Util:…() Making placeholder create method static Removing leftover Placeholder implementation to replace abitrary arguments
2 parents 79e9d35 + dc45874 commit 69978ed

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<directory>./src/</directory>
2323
</whitelist>
2424
</filter>
25+
<php>
26+
<ini name="error_reporting" value="-1"/>
27+
</php>
2528
</phpunit>

src/React/Curry/Placeholder.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace React\Curry;
4+
5+
final class Placeholder
6+
{
7+
private static $instance;
8+
9+
private function __construct()
10+
{
11+
}
12+
13+
public static function create()
14+
{
15+
if (self::$instance === null) {
16+
self::$instance = new self();
17+
}
18+
19+
return self::$instance;
20+
}
21+
22+
public function resolve(array &$args, $position)
23+
{
24+
if (count($args) === 0) {
25+
throw new \InvalidArgumentException(
26+
sprintf('Cannot resolve parameter placeholder at position %d. Parameter stack is empty', $position)
27+
);
28+
}
29+
30+
return array_shift($args);
31+
}
32+
}

src/React/Curry/Util.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ final class Util
66
{
77
public static function bind(/*$fn, $args...*/)
88
{
9+
$args = func_get_args();
10+
$fn = array_shift($args);
11+
912
return call_user_func_array('React\Curry\bind', func_get_args());
1013
}
14+
15+
public static function mergeParameters(array $left, array $right)
16+
{
17+
foreach ($left as $position => &$param) {
18+
if ($param instanceof Placeholder) {
19+
$param = $param->resolve($right, $position);
20+
}
21+
}
22+
23+
return array_merge($left, $right);
24+
}
1125
}

src/React/Curry/functions.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22

33
namespace React\Curry;
44

5+
use React\Curry\Placeholder;
6+
57
function bind(/*$fn, $args...*/)
68
{
79
$args = func_get_args();
810
$fn = array_shift($args);
911

1012
return function () use ($fn, $args) {
11-
return call_user_func_array($fn, array_merge($args, func_get_args()));
13+
return call_user_func_array($fn, Util::mergeParameters($args, func_get_args()));
1214
};
1315
}
16+
17+
function ()
18+
{
19+
return Placeholder::create();
20+
}
21+
22+
function placeholder()
23+
{
24+
return ();
25+
}

tests/React/Curry/BindTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,62 @@ public function testBindWithTwoArgs()
2525
$this->assertSame(6, $addOneAndFive());
2626
}
2727

28+
29+
public function testBindWithPlaceholder()
30+
{
31+
$add = $this->createAddFunction();
32+
$addFun = bind($add, (), 10);
33+
$this->assertSame(20, $addFun(10));
34+
$this->assertSame(30, $addFun(20));
35+
}
36+
37+
public function testBindWithMultiplePlaceholders()
38+
{
39+
$prod = $this->createProdFunction();
40+
$prodTwo = bind($prod, (), 2, ());
41+
$this->assertSame(4, $prodTwo(1, 2));
42+
$this->assertSame(6, $prodTwo(1, 3));
43+
$this->assertSame(8, $prodTwo(2, 2));
44+
$this->assertSame(24, $prodTwo(3, 4));
45+
$this->assertSame(48, $prodTwo(3, 8));
46+
}
47+
48+
public function testPlaceholderParameterPosition()
49+
{
50+
$substr = bind('substr', (), 0, ());
51+
$this->assertSame('foo', $substr('foo', 3));
52+
$this->assertSame('fo', $substr('foo', 2));
53+
$this->assertSame('f', $substr('foo', 1));
54+
}
55+
56+
/**
57+
* @expectedException InvalidArgumentException
58+
* @expectedExceptionMessage Cannot resolve parameter placeholder at position 0. Parameter stack is empty
59+
*/
60+
public function testStringConversion()
61+
{
62+
$add = $this->createAddFunction();
63+
$addTwo = bind($add, (), 2);
64+
65+
$addTwo();
66+
}
67+
68+
public function testAliasForUnicodePlaceholderFunction()
69+
{
70+
$this->assertSame((), placeholder());
71+
}
72+
2873
private function createAddFunction()
2974
{
3075
return function ($a, $b) {
3176
return $a + $b;
3277
};
3378
}
79+
80+
private function createProdFunction()
81+
{
82+
return function ($a, $b, $c) {
83+
return $a * $b * $c;
84+
};
85+
}
3486
}

0 commit comments

Comments
 (0)