Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce return type hints on all functions and require PHP 7.1+ as a consequence #149

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{"name": "Jan Sorgalla", "email": "[email protected]"}
],
"require": {
"php": ">=7.0.0"
"php": ">=7.1.0"
},
"require-dev": {
"phpunit/phpunit": "~6.4"
Expand Down
8 changes: 4 additions & 4 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function __construct(callable $canceller = null)
$this->canceller = $canceller;
}

public function promise()
public function promise(): PromiseInterface
{
if (null === $this->promise) {
$canceller = $this->canceller;
$this->canceller = null;

$this->promise = new Promise(function ($resolve, $reject) {
$this->promise = new Promise(function ($resolve, $reject): void {
$this->resolveCallback = $resolve;
$this->rejectCallback = $reject;
}, $canceller);
Expand All @@ -29,14 +29,14 @@ public function promise()
return $this->promise;
}

public function resolve($value = null)
public function resolve($value = null): void
{
$this->promise();

\call_user_func($this->resolveCallback, $value);
}

public function reject(\Throwable $reason)
public function reject(\Throwable $reason): void
{
$this->promise();

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CompositeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(array $throwables, $message = '', $code = 0, $previo
/**
* @return \Throwable[]
*/
public function getThrowables()
public function getThrowables(): array
{
return $this->throwables;
}
Expand Down
16 changes: 8 additions & 8 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function __construct($value = null)
$this->value = $value;
}

public function then(callable $onFulfilled = null, callable $onRejected = null)
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null === $onFulfilled) {
return $this;
}

return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled) {
enqueue(function () use ($resolve, $reject, $onFulfilled) {
return new Promise(function (callable $resolve, callable $reject) use ($onFulfilled): void {
enqueue(function () use ($resolve, $reject, $onFulfilled): void {
try {
$resolve($onFulfilled($this->value));
} catch (\Throwable $exception) {
Expand All @@ -32,7 +32,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
});
}

public function done(callable $onFulfilled = null, callable $onRejected = null)
public function done(callable $onFulfilled = null, callable $onRejected = null): void
{
if (null === $onFulfilled) {
return;
Expand All @@ -51,21 +51,21 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
});
}

public function otherwise(callable $onRejected)
public function otherwise(callable $onRejected): PromiseInterface
{
return $this;
}

public function always(callable $onFulfilledOrRejected)
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(function ($value) use ($onFulfilledOrRejected) {
return $this->then(function ($value) use ($onFulfilledOrRejected): PromiseInterface {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
return $value;
});
});
}

public function cancel()
public function cancel(): void
{
}
}
6 changes: 3 additions & 3 deletions src/Internal/CancellationQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class CancellationQueue
private $started = false;
private $queue = [];

public function __invoke()
public function __invoke(): void
{
if ($this->started) {
return;
Expand All @@ -20,7 +20,7 @@ public function __invoke()
$this->drain();
}

public function enqueue($cancellable)
public function enqueue($cancellable): void
{
if (!\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
return;
Expand All @@ -33,7 +33,7 @@ public function enqueue($cancellable)
}
}

private function drain()
private function drain(): void
{
for ($i = \key($this->queue); isset($this->queue[$i]); $i++) {
$cancellable = $this->queue[$i];
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ final class Queue
{
private $queue = [];

public function enqueue(callable $task)
public function enqueue(callable $task): void
{
if (1 === \array_push($this->queue, $task)) {
$this->drain();
}
}

private function drain()
private function drain(): void
{
for ($i = \key($this->queue); isset($this->queue[$i]); $i++) {
$task = $this->queue[$i];
Expand Down
25 changes: 13 additions & 12 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(callable $resolver, callable $canceller = null)
$this->call($resolver);
}

public function then(callable $onFulfilled = null, callable $onRejected = null)
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null !== $this->result) {
return $this->result->then($onFulfilled, $onRejected);
Expand All @@ -38,10 +38,11 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
});
}

public function done(callable $onFulfilled = null, callable $onRejected = null)
public function done(callable $onFulfilled = null, callable $onRejected = null): void
{
if (null !== $this->result) {
return $this->result->done($onFulfilled, $onRejected);
$this->result->done($onFulfilled, $onRejected);
return;
}

$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected) {
Expand All @@ -50,7 +51,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
};
}

public function otherwise(callable $onRejected)
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->then(null, function ($reason) use ($onRejected) {
if (!_checkTypehint($onRejected, $reason)) {
Expand All @@ -61,7 +62,7 @@ public function otherwise(callable $onRejected)
});
}

public function always(callable $onFulfilledOrRejected)
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(function ($value) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($value) {
Expand All @@ -74,7 +75,7 @@ public function always(callable $onFulfilledOrRejected)
});
}

public function cancel()
public function cancel(): void
{
$canceller = $this->canceller;
$this->canceller = null;
Expand Down Expand Up @@ -109,7 +110,7 @@ public function cancel()
}
}

private function resolver(callable $onFulfilled = null, callable $onRejected = null)
private function resolver(callable $onFulfilled = null, callable $onRejected = null): callable
{
return function ($resolve, $reject) use ($onFulfilled, $onRejected) {
$this->handlers[] = function (PromiseInterface $promise) use ($onFulfilled, $onRejected, $resolve, $reject) {
Expand All @@ -120,7 +121,7 @@ private function resolver(callable $onFulfilled = null, callable $onRejected = n
};
}

private function resolve($value = null)
private function resolve($value = null): void
{
if (null !== $this->result) {
return;
Expand All @@ -129,7 +130,7 @@ private function resolve($value = null)
$this->settle(resolve($value));
}

private function reject(\Throwable $reason)
private function reject(\Throwable $reason): void
{
if (null !== $this->result) {
return;
Expand All @@ -138,7 +139,7 @@ private function reject(\Throwable $reason)
$this->settle(reject($reason));
}

private function settle(PromiseInterface $result)
private function settle(PromiseInterface $result): void
{
$result = $this->unwrap($result);

Expand All @@ -165,7 +166,7 @@ private function settle(PromiseInterface $result)
}
}

private function unwrap($promise)
private function unwrap($promise): PromiseInterface
{
while ($promise instanceof self && null !== $promise->result) {
$promise = $promise->result;
Expand All @@ -174,7 +175,7 @@ private function unwrap($promise)
return $promise;
}

private function call(callable $callback)
private function call(callable $callback): void
{
// Use reflection to inspect number of arguments expected by this callback.
// We did some careful benchmarking here: Using reflection to avoid unneeded
Expand Down
10 changes: 5 additions & 5 deletions src/PromiseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface PromiseInterface
* @param callable|null $onRejected
* @return PromiseInterface
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;

/**
* Consumes the promise's ultimate value if the promise fulfills, or handles the
Expand All @@ -48,7 +48,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null);
* @param callable|null $onRejected
* @return void
*/
public function done(callable $onFulfilled = null, callable $onRejected = null);
public function done(callable $onFulfilled = null, callable $onRejected = null): void;

/**
* Registers a rejection handler for promise. It is a shortcut for:
Expand All @@ -63,7 +63,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null);
* @param callable $onRejected
* @return PromiseInterface
*/
public function otherwise(callable $onRejected);
public function otherwise(callable $onRejected): PromiseInterface;

/**
* Allows you to execute "cleanup" type tasks in a promise chain.
Expand Down Expand Up @@ -110,7 +110,7 @@ public function otherwise(callable $onRejected);
* @param callable $onFulfilledOrRejected
* @return PromiseInterface
*/
public function always(callable $onFulfilledOrRejected);
public function always(callable $onFulfilledOrRejected): PromiseInterface;

/**
* The `cancel()` method notifies the creator of the promise that there is no
Expand All @@ -121,5 +121,5 @@ public function always(callable $onFulfilledOrRejected);
*
* @return void
*/
public function cancel();
public function cancel(): void;
}
2 changes: 1 addition & 1 deletion src/PromisorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface PromisorInterface
*
* @return PromiseInterface
*/
public function promise();
public function promise(): PromiseInterface;
}
18 changes: 9 additions & 9 deletions src/RejectedPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public function __construct(\Throwable $reason)
$this->reason = $reason;
}

public function then(callable $onFulfilled = null, callable $onRejected = null)
public function then(callable $onFulfilled = null, callable $onRejected = null): PromiseInterface
{
if (null === $onRejected) {
return $this;
}

return new Promise(function (callable $resolve, callable $reject) use ($onRejected) {
enqueue(function () use ($resolve, $reject, $onRejected) {
return new Promise(function (callable $resolve, callable $reject) use ($onRejected): void {
enqueue(function () use ($resolve, $reject, $onRejected): void {
try {
$resolve($onRejected($this->reason));
} catch (\Throwable $exception) {
Expand All @@ -28,7 +28,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
});
}

public function done(callable $onFulfilled = null, callable $onRejected = null)
public function done(callable $onFulfilled = null, callable $onRejected = null): void
{
enqueue(function () use ($onRejected) {
if (null === $onRejected) {
Expand All @@ -51,7 +51,7 @@ public function done(callable $onFulfilled = null, callable $onRejected = null)
});
}

public function otherwise(callable $onRejected)
public function otherwise(callable $onRejected): PromiseInterface
{
if (!_checkTypehint($onRejected, $this->reason)) {
return $this;
Expand All @@ -60,16 +60,16 @@ public function otherwise(callable $onRejected)
return $this->then(null, $onRejected);
}

public function always(callable $onFulfilledOrRejected)
public function always(callable $onFulfilledOrRejected): PromiseInterface
{
return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected) {
return resolve($onFulfilledOrRejected())->then(function () use ($reason) {
return $this->then(null, function (\Throwable $reason) use ($onFulfilledOrRejected): PromiseInterface {
return resolve($onFulfilledOrRejected())->then(function () use ($reason): PromiseInterface {
return new RejectedPromise($reason);
});
});
}

public function cancel()
public function cancel(): void
{
}
}
Loading