Skip to content

Commit 369bc29

Browse files
Added PHP 7 type hints
1 parent 2deefbe commit 369bc29

17 files changed

+80
-126
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33

44
## 2.0.0 - TBC
55

6+
### Added
7+
8+
- Added PHP 7 type hints
9+
610
### Changed
711

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

1014
### Removed
1115

12-
- Removed deprecated global functions
13-
- Removed support for PHP <7.2.5
16+
- Dropped PHP < 7.2 support
17+
- All functions in the `GuzzleHttp\Promise` namespace
1418

1519

1620
## 1.5.3 - 2023-05-21

src/AggregateException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class AggregateException extends RejectionException
1111
{
12-
public function __construct($msg, array $reasons)
12+
public function __construct(string $msg, array $reasons)
1313
{
1414
parent::__construct(
1515
$reasons,

src/Coroutine.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,30 @@ public function __construct(callable $generatorFn)
7777

7878
/**
7979
* Create a new coroutine.
80-
*
81-
* @return self
8280
*/
83-
public static function of(callable $generatorFn)
81+
public static function of(callable $generatorFn): self
8482
{
8583
return new self($generatorFn);
8684
}
8785

8886
public function then(
8987
callable $onFulfilled = null,
9088
callable $onRejected = null
91-
) {
89+
): PromiseInterface {
9290
return $this->result->then($onFulfilled, $onRejected);
9391
}
9492

95-
public function otherwise(callable $onRejected)
93+
public function otherwise(callable $onRejected): PromiseInterface
9694
{
9795
return $this->result->otherwise($onRejected);
9896
}
9997

100-
public function wait($unwrap = true)
98+
public function wait(bool $unwrap = true)
10199
{
102100
return $this->result->wait($unwrap);
103101
}
104102

105-
public function getState()
103+
public function getState(): string
106104
{
107105
return $this->result->getState();
108106
}

src/Create.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ final class Create
1010
* Creates a promise for a value if the value is not a promise.
1111
*
1212
* @param mixed $value Promise or value.
13-
*
14-
* @return PromiseInterface
1513
*/
16-
public static function promiseFor($value)
14+
public static function promiseFor($value): PromiseInterface
1715
{
1816
if ($value instanceof PromiseInterface) {
1917
return $value;
@@ -37,10 +35,8 @@ public static function promiseFor($value)
3735
* If the provided reason is a promise, then it is returned as-is.
3836
*
3937
* @param mixed $reason Promise or reason.
40-
*
41-
* @return PromiseInterface
4238
*/
43-
public static function rejectionFor($reason)
39+
public static function rejectionFor($reason): PromiseInterface
4440
{
4541
if ($reason instanceof PromiseInterface) {
4642
return $reason;
@@ -53,12 +49,10 @@ public static function rejectionFor($reason)
5349
* Create an exception for a rejected promise value.
5450
*
5551
* @param mixed $reason
56-
*
57-
* @return \Exception|\Throwable
5852
*/
59-
public static function exceptionFor($reason)
53+
public static function exceptionFor($reason): \Throwable
6054
{
61-
if ($reason instanceof \Exception || $reason instanceof \Throwable) {
55+
if ($reason instanceof \Throwable) {
6256
return $reason;
6357
}
6458

@@ -69,10 +63,8 @@ public static function exceptionFor($reason)
6963
* Returns an iterator for the given value.
7064
*
7165
* @param mixed $value
72-
*
73-
* @return \Iterator
7466
*/
75-
public static function iterFor($value)
67+
public static function iterFor($value): \Iterator
7668
{
7769
if ($value instanceof \Iterator) {
7870
return $value;

src/Each.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ final class Each
2222
* @param mixed $iterable Iterator or array to iterate over.
2323
* @param callable $onFulfilled
2424
* @param callable $onRejected
25-
*
26-
* @return PromiseInterface
2725
*/
2826
public static function of(
2927
$iterable,
3028
callable $onFulfilled = null,
3129
callable $onRejected = null
32-
) {
30+
): PromiseInterface {
3331
return (new EachPromise($iterable, [
3432
'fulfilled' => $onFulfilled,
3533
'rejected' => $onRejected,
@@ -48,15 +46,13 @@ public static function of(
4846
* @param int|callable $concurrency
4947
* @param callable $onFulfilled
5048
* @param callable $onRejected
51-
*
52-
* @return PromiseInterface
5349
*/
5450
public static function ofLimit(
5551
$iterable,
5652
$concurrency,
5753
callable $onFulfilled = null,
5854
callable $onRejected = null
59-
) {
55+
): PromiseInterface {
6056
return (new EachPromise($iterable, [
6157
'fulfilled' => $onFulfilled,
6258
'rejected' => $onRejected,
@@ -72,14 +68,12 @@ public static function ofLimit(
7268
* @param mixed $iterable
7369
* @param int|callable $concurrency
7470
* @param callable $onFulfilled
75-
*
76-
* @return PromiseInterface
7771
*/
7872
public static function ofLimitAll(
7973
$iterable,
8074
$concurrency,
8175
callable $onFulfilled = null
82-
) {
76+
): PromiseInterface {
8377
return self::ofLimit(
8478
$iterable,
8579
$concurrency,

src/EachPromise.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function __construct($iterable, array $config = [])
7373
}
7474

7575
/** @psalm-suppress InvalidNullableReturnType */
76-
public function promise()
76+
public function promise(): PromiseInterface
7777
{
7878
if ($this->aggregate) {
7979
return $this->aggregate;
@@ -154,7 +154,7 @@ private function refillPending(): void
154154
}
155155
}
156156

157-
private function addPending()
157+
private function addPending(): bool
158158
{
159159
if (!$this->iterable || !$this->iterable->valid()) {
160160
return false;
@@ -195,7 +195,7 @@ function ($reason) use ($idx, $key): void {
195195
return true;
196196
}
197197

198-
private function advanceIterator()
198+
private function advanceIterator(): bool
199199
{
200200
// Place a lock on the iterator so that we ensure to not recurse,
201201
// preventing fatal generator errors.
@@ -218,7 +218,7 @@ private function advanceIterator()
218218
}
219219
}
220220

221-
private function step($idx): void
221+
private function step(int $idx): void
222222
{
223223
// If the promise was already resolved, then ignore this step.
224224
if (Is::settled($this->aggregate)) {
@@ -236,7 +236,7 @@ private function step($idx): void
236236
}
237237
}
238238

239-
private function checkIfFinished()
239+
private function checkIfFinished(): bool
240240
{
241241
if (!$this->pending && !$this->iterable->valid()) {
242242
// Resolve the promise if there's nothing left to do.

src/FulfilledPromise.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class FulfilledPromise implements PromiseInterface
1616
{
1717
private $value;
1818

19+
/**
20+
* @param mixed $value
21+
*/
1922
public function __construct($value)
2023
{
2124
if (is_object($value) && method_exists($value, 'then')) {
@@ -30,7 +33,7 @@ public function __construct($value)
3033
public function then(
3134
callable $onFulfilled = null,
3235
callable $onRejected = null
33-
) {
36+
): PromiseInterface {
3437
// Return itself if there is no onFulfilled function.
3538
if (!$onFulfilled) {
3639
return $this;
@@ -52,17 +55,17 @@ public function then(
5255
return $p;
5356
}
5457

55-
public function otherwise(callable $onRejected)
58+
public function otherwise(callable $onRejected): PromiseInterface
5659
{
5760
return $this->then(null, $onRejected);
5861
}
5962

60-
public function wait($unwrap = true, $defaultDelivery = null)
63+
public function wait(bool $unwrap = true)
6164
{
6265
return $unwrap ? $this->value : null;
6366
}
6467

65-
public function getState()
68+
public function getState(): string
6669
{
6770
return self::FULFILLED;
6871
}

src/Is.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,32 @@ final class Is
88
{
99
/**
1010
* Returns true if a promise is pending.
11-
*
12-
* @return bool
1311
*/
14-
public static function pending(PromiseInterface $promise)
12+
public static function pending(PromiseInterface $promise): bool
1513
{
1614
return $promise->getState() === PromiseInterface::PENDING;
1715
}
1816

1917
/**
2018
* Returns true if a promise is fulfilled or rejected.
21-
*
22-
* @return bool
2319
*/
24-
public static function settled(PromiseInterface $promise)
20+
public static function settled(PromiseInterface $promise): bool
2521
{
2622
return $promise->getState() !== PromiseInterface::PENDING;
2723
}
2824

2925
/**
3026
* Returns true if a promise is fulfilled.
31-
*
32-
* @return bool
3327
*/
34-
public static function fulfilled(PromiseInterface $promise)
28+
public static function fulfilled(PromiseInterface $promise): bool
3529
{
3630
return $promise->getState() === PromiseInterface::FULFILLED;
3731
}
3832

3933
/**
4034
* Returns true if a promise is rejected.
41-
*
42-
* @return bool
4335
*/
44-
public static function rejected(PromiseInterface $promise)
36+
public static function rejected(PromiseInterface $promise): bool
4537
{
4638
return $promise->getState() === PromiseInterface::REJECTED;
4739
}

src/Promise.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
public function then(
3636
callable $onFulfilled = null,
3737
callable $onRejected = null
38-
) {
38+
): PromiseInterface {
3939
if ($this->state === self::PENDING) {
4040
$p = new Promise(null, [$this, 'cancel']);
4141
$this->handlers[] = [$p, $onFulfilled, $onRejected];
@@ -59,12 +59,12 @@ public function then(
5959
return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
6060
}
6161

62-
public function otherwise(callable $onRejected)
62+
public function otherwise(callable $onRejected): PromiseInterface
6363
{
6464
return $this->then(null, $onRejected);
6565
}
6666

67-
public function wait($unwrap = true)
67+
public function wait(bool $unwrap = true)
6868
{
6969
$this->waitIfPending();
7070

@@ -80,7 +80,7 @@ public function wait($unwrap = true)
8080
}
8181
}
8282

83-
public function getState()
83+
public function getState(): string
8484
{
8585
return $this->state;
8686
}
@@ -120,7 +120,7 @@ public function reject($reason): void
120120
$this->settle(self::REJECTED, $reason);
121121
}
122122

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

0 commit comments

Comments
 (0)