Skip to content

Commit 331ac34

Browse files
committed
#18: changed files for PHPStan analysis
1 parent 0b43679 commit 331ac34

File tree

8 files changed

+52
-21
lines changed

8 files changed

+52
-21
lines changed

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: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
/**
66
* This class checks how many times a specified method was called.
7-
* @template T
7+
* @template T of object
88
* @mixin T
9+
* @implements Indifferent<T>
910
*/
10-
class TimesCalled
11+
class TimesCalled implements Indifferent
1112
{
1213
/**
1314
* Original object.
@@ -33,9 +34,10 @@ class TimesCalled
3334
/**
3435
* Ctor.
3536
*
36-
* @param T $origin
37+
* @param T $origin
38+
* @param string $method
3739
*/
38-
public function __construct($origin, string $method)
40+
public function __construct(object $origin, string $method)
3941
{
4042
$this->origin = $origin;
4143
$this->method = $method;
@@ -53,10 +55,7 @@ public function value(): int
5355

5456
/**
5557
* Sends calls through itself and counts how many times a specific method was called.
56-
*
57-
* @param string $name
58-
* @param array $arguments
59-
* @return mixed
58+
* {@inheritDoc}
6059
*/
6160
public function __call(string $name, array $arguments): mixed
6261
{

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" />

tests/fakes/TimesCalledTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function testCountsHowManyTimesTheMethodWasCalled(): void
4949
public function testCountsHowManyTimesTheMethodWasCalledAlongWIthOtherMethods(): void
5050
{
5151
$method = "key";
52-
$other = "current";
5352
$called = new TimesCalled(
5453
new ArrayIterator([16, 14, 13, 15, 12, 18]),
5554
$method
@@ -60,7 +59,7 @@ public function testCountsHowManyTimesTheMethodWasCalledAlongWIthOtherMethods():
6059
$called->$method();
6160

6261
if (($i % 2 === 0) or ($i % 3 === 0)) {
63-
$called->$other();
62+
$called->current();
6463
}
6564
}
6665

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)