Skip to content

Commit

Permalink
Added PHP 7 type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed May 21, 2023
1 parent 2deefbe commit 369bc29
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 126 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@

## 2.0.0 - TBC

### Added

- Added PHP 7 type hints

### Changed

- All previously non-final non-exception classes have been marked as soft-final

### Removed

- Removed deprecated global functions
- Removed support for PHP <7.2.5
- Dropped PHP < 7.2 support
- All functions in the `GuzzleHttp\Promise` namespace


## 1.5.3 - 2023-05-21
Expand Down
2 changes: 1 addition & 1 deletion src/AggregateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class AggregateException extends RejectionException
{
public function __construct($msg, array $reasons)
public function __construct(string $msg, array $reasons)
{
parent::__construct(
$reasons,
Expand Down
12 changes: 5 additions & 7 deletions src/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,30 @@ public function __construct(callable $generatorFn)

/**
* Create a new coroutine.
*
* @return self
*/
public static function of(callable $generatorFn)
public static function of(callable $generatorFn): self
{
return new self($generatorFn);
}

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

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

public function wait($unwrap = true)
public function wait(bool $unwrap = true)
{
return $this->result->wait($unwrap);
}

public function getState()
public function getState(): string
{
return $this->result->getState();
}
Expand Down
18 changes: 5 additions & 13 deletions src/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ final class Create
* Creates a promise for a value if the value is not a promise.
*
* @param mixed $value Promise or value.
*
* @return PromiseInterface
*/
public static function promiseFor($value)
public static function promiseFor($value): PromiseInterface
{
if ($value instanceof PromiseInterface) {
return $value;
Expand All @@ -37,10 +35,8 @@ public static function promiseFor($value)
* If the provided reason is a promise, then it is returned as-is.
*
* @param mixed $reason Promise or reason.
*
* @return PromiseInterface
*/
public static function rejectionFor($reason)
public static function rejectionFor($reason): PromiseInterface
{
if ($reason instanceof PromiseInterface) {
return $reason;
Expand All @@ -53,12 +49,10 @@ public static function rejectionFor($reason)
* Create an exception for a rejected promise value.
*
* @param mixed $reason
*
* @return \Exception|\Throwable
*/
public static function exceptionFor($reason)
public static function exceptionFor($reason): \Throwable
{
if ($reason instanceof \Exception || $reason instanceof \Throwable) {
if ($reason instanceof \Throwable) {
return $reason;
}

Expand All @@ -69,10 +63,8 @@ public static function exceptionFor($reason)
* Returns an iterator for the given value.
*
* @param mixed $value
*
* @return \Iterator
*/
public static function iterFor($value)
public static function iterFor($value): \Iterator
{
if ($value instanceof \Iterator) {
return $value;
Expand Down
12 changes: 3 additions & 9 deletions src/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ final class Each
* @param mixed $iterable Iterator or array to iterate over.
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*/
public static function of(
$iterable,
callable $onFulfilled = null,
callable $onRejected = null
) {
): PromiseInterface {
return (new EachPromise($iterable, [
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
Expand All @@ -48,15 +46,13 @@ public static function of(
* @param int|callable $concurrency
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*/
public static function ofLimit(
$iterable,
$concurrency,
callable $onFulfilled = null,
callable $onRejected = null
) {
): PromiseInterface {
return (new EachPromise($iterable, [
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
Expand All @@ -72,14 +68,12 @@ public static function ofLimit(
* @param mixed $iterable
* @param int|callable $concurrency
* @param callable $onFulfilled
*
* @return PromiseInterface
*/
public static function ofLimitAll(
$iterable,
$concurrency,
callable $onFulfilled = null
) {
): PromiseInterface {
return self::ofLimit(
$iterable,
$concurrency,
Expand Down
10 changes: 5 additions & 5 deletions src/EachPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct($iterable, array $config = [])
}

/** @psalm-suppress InvalidNullableReturnType */
public function promise()
public function promise(): PromiseInterface
{
if ($this->aggregate) {
return $this->aggregate;
Expand Down Expand Up @@ -154,7 +154,7 @@ private function refillPending(): void
}
}

private function addPending()
private function addPending(): bool
{
if (!$this->iterable || !$this->iterable->valid()) {
return false;
Expand Down Expand Up @@ -195,7 +195,7 @@ function ($reason) use ($idx, $key): void {
return true;
}

private function advanceIterator()
private function advanceIterator(): bool
{
// Place a lock on the iterator so that we ensure to not recurse,
// preventing fatal generator errors.
Expand All @@ -218,7 +218,7 @@ private function advanceIterator()
}
}

private function step($idx): void
private function step(int $idx): void
{
// If the promise was already resolved, then ignore this step.
if (Is::settled($this->aggregate)) {
Expand All @@ -236,7 +236,7 @@ private function step($idx): void
}
}

private function checkIfFinished()
private function checkIfFinished(): bool
{
if (!$this->pending && !$this->iterable->valid()) {
// Resolve the promise if there's nothing left to do.
Expand Down
11 changes: 7 additions & 4 deletions src/FulfilledPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class FulfilledPromise implements PromiseInterface
{
private $value;

/**
* @param mixed $value
*/
public function __construct($value)
{
if (is_object($value) && method_exists($value, 'then')) {
Expand All @@ -30,7 +33,7 @@ public function __construct($value)
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
): PromiseInterface {
// Return itself if there is no onFulfilled function.
if (!$onFulfilled) {
return $this;
Expand All @@ -52,17 +55,17 @@ public function then(
return $p;
}

public function otherwise(callable $onRejected)
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->then(null, $onRejected);
}

public function wait($unwrap = true, $defaultDelivery = null)
public function wait(bool $unwrap = true)
{
return $unwrap ? $this->value : null;
}

public function getState()
public function getState(): string
{
return self::FULFILLED;
}
Expand Down
16 changes: 4 additions & 12 deletions src/Is.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,32 @@ final class Is
{
/**
* Returns true if a promise is pending.
*
* @return bool
*/
public static function pending(PromiseInterface $promise)
public static function pending(PromiseInterface $promise): bool
{
return $promise->getState() === PromiseInterface::PENDING;
}

/**
* Returns true if a promise is fulfilled or rejected.
*
* @return bool
*/
public static function settled(PromiseInterface $promise)
public static function settled(PromiseInterface $promise): bool
{
return $promise->getState() !== PromiseInterface::PENDING;
}

/**
* Returns true if a promise is fulfilled.
*
* @return bool
*/
public static function fulfilled(PromiseInterface $promise)
public static function fulfilled(PromiseInterface $promise): bool
{
return $promise->getState() === PromiseInterface::FULFILLED;
}

/**
* Returns true if a promise is rejected.
*
* @return bool
*/
public static function rejected(PromiseInterface $promise)
public static function rejected(PromiseInterface $promise): bool
{
return $promise->getState() === PromiseInterface::REJECTED;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
): PromiseInterface {
if ($this->state === self::PENDING) {
$p = new Promise(null, [$this, 'cancel']);
$this->handlers[] = [$p, $onFulfilled, $onRejected];
Expand All @@ -59,12 +59,12 @@ public function then(
return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
}

public function otherwise(callable $onRejected)
public function otherwise(callable $onRejected): PromiseInterface
{
return $this->then(null, $onRejected);
}

public function wait($unwrap = true)
public function wait(bool $unwrap = true)
{
$this->waitIfPending();

Expand All @@ -80,7 +80,7 @@ public function wait($unwrap = true)
}
}

public function getState()
public function getState(): string
{
return $this->state;
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public function reject($reason): void
$this->settle(self::REJECTED, $reason);
}

private function settle($state, $value): void
private function settle(string $state, $value): void
{
if ($this->state !== self::PENDING) {
// Ignore calls with the same resolution.
Expand Down Expand Up @@ -185,7 +185,7 @@ static function ($reason) use ($handlers): void {
* @param mixed $value Value to pass to the callback.
* @param array $handler Array of handler data (promise and callbacks).
*/
private static function callHandler($index, $value, array $handler): void
private static function callHandler(int $index, $value, array $handler): void
{
/** @var PromiseInterface $promise */
$promise = $handler[0];
Expand Down
Loading

0 comments on commit 369bc29

Please sign in to comment.