Skip to content

Commit 0b43679

Browse files
committed
#18: added PHPStan hints
1 parent ee64bad commit 0b43679

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

fakes/TimesCalled.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace MaxGoryunov\SavingIterator\Fakes;
44

5+
/**
6+
* This class checks how many times a specified method was called.
7+
* @template T
8+
* @mixin T
9+
*/
510
class TimesCalled
611
{
712
/**
813
* Original object.
914
*
10-
* @var object
15+
* @var T
1116
*/
1217
private object $origin;
1318

@@ -28,9 +33,9 @@ class TimesCalled
2833
/**
2934
* Ctor.
3035
*
31-
* @param object $origin
36+
* @param T $origin
3237
*/
33-
public function __construct(object $origin, string $method)
38+
public function __construct($origin, string $method)
3439
{
3540
$this->origin = $origin;
3641
$this->method = $method;
@@ -58,12 +63,6 @@ public function __call(string $name, array $arguments): mixed
5863
if ($name === $this->method) {
5964
$this->times++;
6065
}
61-
62-
if ($arguments === []) {
63-
$result = $this->origin->$name();
64-
} else {
65-
$result = $this->origin->$name($arguments);
66-
}
67-
return $result;
66+
return $this->origin->$name(...$arguments);
6867
}
6968
}

src/SavingIterator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@
44

55
use Iterator;
66

7+
/**
8+
* Iterator which stores iterated values.
9+
*
10+
* @template TKey
11+
* @template TValue
12+
* @implements Iterator<TKey, TValue>
13+
*/
714
class SavingIterator implements Iterator
815
{
916

1017
/**
1118
* Original iterator
1219
*
13-
* @var Iterator
20+
* @var Iterator<TKey, TValue>
1421
*/
1522
private Iterator $origin;
1623

1724
/**
1825
* Cached values from the inner iterator
1926
*
20-
* @var array
27+
* @var array<TKey, TValue>
2128
*/
2229
private array $saved = [];
2330
/**
2431
* Ctor.
2532
*
26-
* @param Iterator $iterator
33+
* @param Iterator<TKey, TValue> $iterator
2734
*/
2835
public function __construct(Iterator $iterator)
2936
{
@@ -32,6 +39,7 @@ public function __construct(Iterator $iterator)
3239

3340
/**
3441
* {@inheritDoc}
42+
* @return TValue|false
3543
*/
3644
public function current(): mixed
3745
{
@@ -43,6 +51,7 @@ public function current(): mixed
4351

4452
/**
4553
* {@inheritDoc}
54+
* @return TKey|null
4655
*/
4756
public function key(): mixed
4857
{

tests/fakes/TimesCalledTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function testCountsHowManyTimesTheMethodWasCalled(): void
4949
public function testCountsHowManyTimesTheMethodWasCalledAlongWIthOtherMethods(): void
5050
{
5151
$method = "key";
52+
$other = "current";
5253
$called = new TimesCalled(
5354
new ArrayIterator([16, 14, 13, 15, 12, 18]),
5455
$method
@@ -59,7 +60,7 @@ public function testCountsHowManyTimesTheMethodWasCalledAlongWIthOtherMethods():
5960
$called->$method();
6061

6162
if (($i % 2 === 0) or ($i % 3 === 0)) {
62-
$called->current();
63+
$called->$other();
6364
}
6465
}
6566

0 commit comments

Comments
 (0)