Skip to content

Commit

Permalink
Use full function namespaces to avoid clashes with serialization
Browse files Browse the repository at this point in the history
`Promise.php` contains function calls that expect the current namespace. However if you serialize & then later unserialize a Promise, this namespace is lost, and the function calls clash with other defined global functions.

E.g. if using Laravel, you serialize a `\React\Promise\Promise` instance, and then later unserialize & attempt to resolve it - the call to `resolve` on line 232 clashes with Laravel's global `resolve` function - causing it to throw an exception.

By using the full namespace on the function call this eliminates this possible problem. Also makes the code an easier read IMO making it obvious that these are namespaced functions.
  • Loading branch information
brentkelly committed Aug 29, 2020
1 parent 0845d29 commit 604023b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Promise.php
Expand Up @@ -84,11 +84,11 @@ public function otherwise(callable $onRejected): PromiseInterface
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(static function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return \React\Promise\resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
}, static function ($reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return \React\Promise\resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return new RejectedPromise($reason);
});
});
Expand Down Expand Up @@ -146,7 +146,7 @@ private function reject(\Throwable $reason): void
return;
}

$this->settle(reject($reason));
$this->settle(\React\Promise\reject($reason));
}

private function settle(PromiseInterface $result): void
Expand Down Expand Up @@ -223,7 +223,7 @@ private function call(callable $cb): void
$callback(
static function ($value = null) use (&$target) {
if ($target !== null) {
$target->settle(resolve($value));
$target->settle(\React\Promise\resolve($value));
$target = null;
}
},
Expand Down

0 comments on commit 604023b

Please sign in to comment.