Skip to content

Commit a4d6358

Browse files
committed
Add callable typehints
Also removes trigger_error's for non-function arguments and related tests
1 parent 95fb578 commit a4d6358

13 files changed

+18
-173
lines changed

src/React/Promise/Deferred.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Deferred implements PromiseInterface, ResolverInterface, PromisorInterface
1010
private $handlers = array();
1111
private $progressHandlers = array();
1212

13-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null)
13+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
1414
{
1515
if (null !== $this->completed) {
1616
return $this->completed->then($onFulfilled, $onRejected, $onProgress);
@@ -27,10 +27,6 @@ public function then($onFulfilled = null, $onRejected = null, $onProgress = null
2727
}
2828
};
2929
} else {
30-
if (null !== $onProgress) {
31-
trigger_error('Invalid $onProgress argument passed to then(), must be null or callable.', E_USER_NOTICE);
32-
}
33-
3430
$progHandler = array($deferred, 'progress');
3531
}
3632

src/React/Promise/DeferredPromise.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct(Deferred $deferred)
1111
$this->deferred = $deferred;
1212
}
1313

14-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null)
14+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
1515
{
1616
return $this->deferred->then($onFulfilled, $onRejected, $onProgress);
1717
}

src/React/Promise/FulfilledPromise.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ public function __construct($value = null)
1111
$this->value = $value;
1212
}
1313

14-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null)
14+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
1515
{
1616
try {
1717
$value = $this->value;
1818

19-
if (is_callable($onFulfilled)) {
19+
if (null !== $onFulfilled) {
2020
$value = call_user_func($onFulfilled, $value);
21-
} elseif (null !== $onFulfilled) {
22-
trigger_error('Invalid $onFulfilled argument passed to then(), must be null or callable.', E_USER_NOTICE);
2321
}
2422

2523
return Util::promiseFor($value);

src/React/Promise/LazyPromise.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class LazyPromise implements PromiseInterface
77
private $factory;
88
private $promise;
99

10-
public function __construct($factory)
10+
public function __construct(callable $factory)
1111
{
1212
$this->factory = $factory;
1313
}
1414

15-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null)
15+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
1616
{
1717
if (null === $this->promise) {
1818
try {

src/React/Promise/PromiseInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface PromiseInterface
66
{
7-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null);
7+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null);
88
}

src/React/Promise/RejectedPromise.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ public function __construct($reason = null)
1111
$this->reason = $reason;
1212
}
1313

14-
public function then($onFulfilled = null, $onRejected = null, $onProgress = null)
14+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
1515
{
1616
try {
17-
if (!is_callable($onRejected)) {
18-
if (null !== $onRejected) {
19-
trigger_error('Invalid $onRejected argument passed to then(), must be null or callable.', E_USER_NOTICE);
20-
}
21-
17+
if (null === $onRejected) {
2218
return new RejectedPromise($this->reason);
2319
}
2420

src/React/Promise/When.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function lazy($factory)
1919
return new LazyPromise($factory);
2020
}
2121

22-
public static function all($promisesOrValues, $onFulfilled = null, $onRejected = null, $onProgress = null)
22+
public static function all($promisesOrValues, callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
2323
{
2424
$promise = static::map($promisesOrValues, function ($val) {
2525
return $val;
@@ -28,7 +28,7 @@ public static function all($promisesOrValues, $onFulfilled = null, $onRejected =
2828
return $promise->then($onFulfilled, $onRejected, $onProgress);
2929
}
3030

31-
public static function any($promisesOrValues, $onFulfilled = null, $onRejected = null, $onProgress = null)
31+
public static function any($promisesOrValues, callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
3232
{
3333
$unwrapSingleResult = function ($val) use ($onFulfilled) {
3434
$val = array_shift($val);
@@ -39,7 +39,7 @@ public static function any($promisesOrValues, $onFulfilled = null, $onRejected =
3939
return static::some($promisesOrValues, 1, $unwrapSingleResult, $onRejected, $onProgress);
4040
}
4141

42-
public static function some($promisesOrValues, $howMany, $onFulfilled = null, $onRejected = null, $onProgress = null)
42+
public static function some($promisesOrValues, $howMany, callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
4343
{
4444
return When::resolve($promisesOrValues)->then(function ($array) use ($howMany, $onFulfilled, $onRejected, $onProgress) {
4545
if (!is_array($array)) {
@@ -104,7 +104,7 @@ public static function some($promisesOrValues, $howMany, $onFulfilled = null, $o
104104
});
105105
}
106106

107-
public static function map($promisesOrValues, $mapFunc)
107+
public static function map($promisesOrValues, callable $mapFunc)
108108
{
109109
return When::resolve($promisesOrValues)->then(function ($array) use ($mapFunc) {
110110
if (!is_array($array)) {
@@ -142,7 +142,7 @@ function ($mapped) use (&$values, $i, &$toResolve, $deferred) {
142142
});
143143
}
144144

145-
public static function reduce($promisesOrValues, $reduceFunc , $initialValue = null)
145+
public static function reduce($promisesOrValues, callable $reduceFunc , $initialValue = null)
146146
{
147147
return When::resolve($promisesOrValues)->then(function ($array) use ($reduceFunc, $initialValue) {
148148
if (!is_array($array)) {

tests/React/Promise/DeferredProgressTest.php

-34
Original file line numberDiff line numberDiff line change
@@ -312,38 +312,4 @@ public function shouldAllowRejectAfterProgress()
312312
->resolver()
313313
->reject(2);
314314
}
315-
316-
/**
317-
* @test
318-
* @dataProvider invalidCallbackDataProvider
319-
**/
320-
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
321-
{
322-
$errorCollector = new ErrorCollector();
323-
$errorCollector->register();
324-
325-
$mock = $this->createCallableMock();
326-
$mock
327-
->expects($this->once())
328-
->method('__invoke')
329-
->with($this->identicalTo(1));
330-
331-
$d = new Deferred();
332-
$d
333-
->then(
334-
null,
335-
null,
336-
$var
337-
)
338-
->then(
339-
$this->expectCallableNever(),
340-
$this->expectCallableNever(),
341-
$mock
342-
);
343-
344-
$d->progress(1);
345-
346-
$errorCollector->assertCollectedError('Invalid $onProgress argument passed to then(), must be null or callable.', E_USER_NOTICE);
347-
$errorCollector->unregister();
348-
}
349315
}

tests/React/Promise/DeferredPromiseTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ class DeferredPromiseTest extends TestCase
1111
/** @test */
1212
public function shouldForwardToDeferred()
1313
{
14+
$callable = $this->createCallableMock();
15+
1416
$mock = $this->getMock('React\\Promise\\Deferred');
1517
$mock
1618
->expects($this->once())
1719
->method('then')
18-
->with(1, 2, 3);
20+
->with($callable, $callable, $callable);
1921

2022
$p = new DeferredPromise($mock);
21-
$p->then(1, 2, 3);
23+
$p->then($callable, $callable, $callable);
2224
}
2325
}

tests/React/Promise/DeferredRejectTest.php

-32
Original file line numberDiff line numberDiff line change
@@ -125,36 +125,4 @@ public function shouldForwardReasonWhenCallbackIsNull()
125125

126126
$d->reject(1);
127127
}
128-
129-
/**
130-
* @test
131-
* @dataProvider invalidCallbackDataProvider
132-
**/
133-
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
134-
{
135-
$errorCollector = new ErrorCollector();
136-
$errorCollector->register();
137-
138-
$mock = $this->createCallableMock();
139-
$mock
140-
->expects($this->once())
141-
->method('__invoke')
142-
->with($this->identicalTo(1));
143-
144-
$d = new Deferred();
145-
$d
146-
->then(
147-
null,
148-
$var
149-
)
150-
->then(
151-
$this->expectCallableNever(),
152-
$mock
153-
);
154-
155-
$d->reject(1);
156-
157-
$errorCollector->assertCollectedError('Invalid $onRejected argument passed to then(), must be null or callable.', E_USER_NOTICE);
158-
$errorCollector->unregister();
159-
}
160128
}

tests/React/Promise/DeferredResolveTest.php

-31
Original file line numberDiff line numberDiff line change
@@ -162,35 +162,4 @@ public function shouldForwardValueWhenCallbackIsNull()
162162

163163
$d->resolve(1);
164164
}
165-
166-
/**
167-
* @test
168-
* @dataProvider invalidCallbackDataProvider
169-
**/
170-
public function shouldIgnoreNonFunctionsAndTriggerPhpNotice($var)
171-
{
172-
$errorCollector = new ErrorCollector();
173-
$errorCollector->register();
174-
175-
$mock = $this->createCallableMock();
176-
$mock
177-
->expects($this->once())
178-
->method('__invoke')
179-
->with($this->identicalTo(1));
180-
181-
$d = new Deferred();
182-
$d
183-
->then(
184-
$var
185-
)
186-
->then(
187-
$mock,
188-
$this->expectCallableNever()
189-
);
190-
191-
$d->resolve(1);
192-
193-
$errorCollector->assertCollectedError('Invalid $onFulfilled argument passed to then(), must be null or callable.', E_USER_NOTICE);
194-
$errorCollector->unregister();
195-
}
196165
}

tests/React/Promise/ErrorCollector.php

-38
This file was deleted.

tests/React/Promise/TestCase.php

-12
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,4 @@ public function createCallableMock()
3838
{
3939
return $this->getMock('React\\Promise\Stub\CallableStub');
4040
}
41-
42-
public function invalidCallbackDataProvider()
43-
{
44-
return array(
45-
'empty string' => array(''),
46-
'true' => array(true),
47-
'false' => array(false),
48-
'object' => array(new \stdClass),
49-
'truthy' => array(1),
50-
'falsey' => array(0)
51-
);
52-
}
5341
}

0 commit comments

Comments
 (0)