Skip to content

Commit 72358f0

Browse files
authored
Merge pull request #20 from MaxGoryunov/feature/18
Feature/18
2 parents ed2b24a + 331ac34 commit 72358f0

File tree

9 files changed

+72
-29
lines changed

9 files changed

+72
-29
lines changed

.github/workflows/php.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ jobs:
2323
with:
2424
bootstrap: vendor/autoload.php
2525
configuration: phpunit.xml
26-
args: --coverage-text
26+
args: --coverage-text
27+
28+
- name: PHPStan analysis
29+
run: ./vendor/bin/phpstan analyse

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "maxgoryunov/saving-iterator",
33
"description": "True Caching Iterator for PHP",
44
"require": {
5-
"php": ">=7.4"
5+
"php": ">=8.0"
66
},
77
"require-dev": {
88
"phpstan/phpstan": "^0.12.91",

fakes/Indifferent.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace MaxGoryunov\SavingIterator\Fakes;
4+
5+
/**
6+
* Interface for objects on which any methods can be called.
7+
* @template T of object
8+
* @mixin T
9+
*/
10+
interface Indifferent
11+
{
12+
13+
/**
14+
* Returns result of any called method.
15+
*
16+
* @param string $name
17+
* @param array<mixed> $arguments
18+
* @return mixed
19+
*/
20+
public function __call(string $name, array $arguments): mixed;
21+
}

fakes/TimesCalled.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace MaxGoryunov\SavingIterator\Fakes;
44

5-
class TimesCalled
5+
/**
6+
* This class checks how many times a specified method was called.
7+
* @template T of object
8+
* @mixin T
9+
* @implements Indifferent<T>
10+
*/
11+
class TimesCalled implements Indifferent
612
{
713
/**
814
* Original object.
915
*
10-
* @var object
16+
* @var T
1117
*/
1218
private object $origin;
1319

@@ -28,7 +34,8 @@ class TimesCalled
2834
/**
2935
* Ctor.
3036
*
31-
* @param object $origin
37+
* @param T $origin
38+
* @param string $method
3239
*/
3340
public function __construct(object $origin, string $method)
3441
{
@@ -48,22 +55,13 @@ public function value(): int
4855

4956
/**
5057
* Sends calls through itself and counts how many times a specific method was called.
51-
*
52-
* @param string $name
53-
* @param array $arguments
54-
* @return mixed
58+
* {@inheritDoc}
5559
*/
5660
public function __call(string $name, array $arguments): mixed
5761
{
5862
if ($name === $this->method) {
5963
$this->times++;
6064
}
61-
62-
if ($arguments === []) {
63-
$result = $this->origin->$name();
64-
} else {
65-
$result = $this->origin->$name($arguments);
66-
}
67-
return $result;
65+
return $this->origin->$name(...$arguments);
6866
}
6967
}

fakes/TransparentIterator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
use Iterator;
66

7+
/**
8+
* Wraps objects which are not interators.
9+
*
10+
* @template TKey
11+
* @template TValue
12+
* @implements Iterator<TKey, TValue>
13+
*/
714
class TransparentIterator implements Iterator
815
{
916

@@ -12,16 +19,16 @@ class TransparentIterator implements Iterator
1219
*
1320
* This is NOT necessarily an iterator
1421
*
15-
* @var object
22+
* @var Iterator<TKey, TValue>|Indifferent<Iterator<TKey, TValue>>
1623
*/
17-
private object $origin;
24+
private Iterator|Indifferent $origin;
1825

1926
/**
2027
* Ctor.
2128
*
22-
* @param object $iterable
29+
* @param Iterator<TKey, TValue>|Indifferent<Iterator<TKey, TValue>> $iterable
2330
*/
24-
public function __construct(object $iterable)
31+
public function __construct(Iterator|Indifferent $iterable)
2532
{
2633
$this->origin = $iterable;
2734
}

phpstan.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ parameters:
22
level: 8
33
paths:
44
- src
5-
- tests
5+
- fakes
6+
- tests
7+
includes:
8+
- vendor/phpstan/phpstan/conf/bleedingEdge.neon

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
88
>
99
<projectFiles>
10+
<directory name="src" />
11+
<directory name="fakes" />
12+
<directory name="tests" />
1013
<file name="vendor/autoload.php" />
1114
<file name="vendor/composer/autoload_classmap.php" />
1215
<file name="vendor/composer/autoload_files.php" />

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/src/SavingIteratorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MaxGoryunov\SavingIterator\Tests\Src;
44

5+
use Iterator;
56
use ArrayIterator;
67
use Generator;
78
use MaxGoryunov\SavingIterator\Fakes\TimesCalled;
@@ -60,10 +61,8 @@ public function testDoesNotCallOriginIfValuesAreInCache(): void
6061
new ArrayIterator($input),
6162
"next"
6263
);
63-
$iterator = new SavingIterator(
64-
new TransparentIterator(
65-
$called
66-
)
64+
$iterator = new SavingIterator(/* @phpstan-ignore-next-line */
65+
new TransparentIterator($called)
6766
);
6867
for ($i = 0; $i < rand(0, 10); $i++) {
6968
foreach ($iterator as $key => $value) {
@@ -179,7 +178,7 @@ public function testContinuesSuccessfullyAfterBeingInterrupted(): void
179178
new ArrayIterator($input)
180179
);
181180
foreach ($iterator as $value) {
182-
if ($value === 90) {
181+
if ($value === $input[3]) {
183182
break;
184183
}
185184
}

0 commit comments

Comments
 (0)