Skip to content

Commit d8ede8c

Browse files
committed
Fix CS
1 parent 9c83f92 commit d8ede8c

9 files changed

+115
-80
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"files": ["src/iterable-functions.php"]
1111
},
1212
"autoload-dev": {
13+
"psr-4": {
14+
"BenTools\\IterableFunctions\\Tests\\": "tests"
15+
},
1316
"files": [
1417
"vendor/symfony/var-dumper/Resources/functions/dump.php"
1518
]

src/IterableObject.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BenTools\IterableFunctions;
46

57
use EmptyIterator;
@@ -11,21 +13,18 @@
1113
*/
1214
final class IterableObject implements IteratorAggregate
1315
{
14-
/**
15-
* @var iterable|array|Traversable
16-
*/
16+
/** @var iterable<mixed> */
1717
private $iterable;
1818

19-
/**
20-
* @var callable
21-
*/
19+
/** @var callable */
2220
private $filterFn;
2321

24-
/**
25-
* @var callable
26-
*/
22+
/** @var callable */
2723
private $mapFn;
2824

25+
/**
26+
* @param iterable<mixed>|null $iterable
27+
*/
2928
public function __construct(?iterable $iterable = null, ?callable $filter = null, ?callable $map = null)
3029
{
3130
$this->iterable = $iterable ?? new EmptyIterator();
@@ -43,27 +42,35 @@ public function map(callable $map): self
4342
return new self($this->iterable, $this->filterFn, $map);
4443
}
4544

45+
/**
46+
* @return Traversable<mixed>
47+
*/
4648
public function getIterator(): Traversable
4749
{
4850
$iterable = $this->iterable;
49-
if (null !== $this->filterFn) {
51+
if ($this->filterFn !== null) {
5052
$iterable = iterable_filter($iterable, $this->filterFn);
5153
}
52-
if (null !== $this->mapFn) {
54+
55+
if ($this->mapFn !== null) {
5356
$iterable = iterable_map($iterable, $this->mapFn);
5457
}
58+
5559
return iterable_to_traversable($iterable);
5660
}
5761

62+
/** @return array<mixed> */
5863
public function asArray(): array
5964
{
6065
$iterable = $this->iterable;
61-
if (null !== $this->filterFn) {
66+
if ($this->filterFn !== null) {
6267
$iterable = iterable_filter($iterable, $this->filterFn);
6368
}
64-
if (null !== $this->mapFn) {
69+
70+
if ($this->mapFn !== null) {
6571
$iterable = iterable_map($iterable, $this->mapFn);
6672
}
73+
6774
return iterable_to_array($iterable);
6875
}
6976
}

src/iterable-functions.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?php
22

3-
namespace BenTools\IterableFunctions;
3+
declare(strict_types=1);
44

5+
namespace BenTools\IterableFunctions;
56

67
use ArrayIterator;
78
use CallbackFilterIterator;
89
use IteratorIterator;
910
use Traversable;
1011

12+
use function array_filter;
13+
use function array_map;
14+
use function array_values;
15+
use function iterator_to_array;
16+
1117
/**
1218
* Maps a callable to an iterable.
1319
*
14-
* @param array|Traversable $iterable
15-
* @return array|ArrayIterator
20+
* @param iterable<mixed> $iterable
21+
*
22+
* @return iterable<mixed>
1623
*/
1724
function iterable_map(iterable $iterable, callable $map): iterable
1825
{
@@ -23,27 +30,29 @@ function iterable_map(iterable $iterable, callable $map): iterable
2330
return array_map($map, $iterable);
2431
}
2532

26-
2733
/**
2834
* Copy the iterable into an array. If the iterable is already an array, return it.
2935
*
30-
* @param array|Traversable $iterable
31-
* @param bool $use_keys [optional] Whether to use the iterator element keys as index.
32-
* @return array
36+
* @param iterable<mixed> $iterable
37+
* @param bool $preserveKeys [optional] Whether to use the iterator element keys as index.
38+
*
39+
* @return array<mixed>
3340
*/
34-
function iterable_to_array(iterable $iterable, bool $use_keys = true): array
41+
function iterable_to_array(iterable $iterable, bool $preserveKeys = true): array
3542
{
3643
if ($iterable instanceof Traversable) {
37-
return iterator_to_array($iterable, $use_keys);
44+
return iterator_to_array($iterable, $preserveKeys);
3845
}
3946

40-
return $use_keys ? $iterable : array_values($iterable);
47+
return $preserveKeys ? $iterable : array_values($iterable);
4148
}
4249

4350
/**
44-
* If the iterable is not intance of Traversable, it is an array => convert it to an ArrayIterator.
51+
* If the iterable is not instance of Traversable, it is an array => convert it to an ArrayIterator.
52+
*
53+
* @param iterable<mixed> $iterable
4554
*
46-
* @param array|Traversable $iterable
55+
* @return Traversable<mixed>
4756
*/
4857
function iterable_to_traversable(iterable $iterable): Traversable
4958
{
@@ -54,16 +63,16 @@ function iterable_to_traversable(iterable $iterable): Traversable
5463
return new ArrayIterator($iterable);
5564
}
5665

57-
5866
/**
5967
* Filters an iterable.
6068
*
61-
* @param array|Traversable $iterable
62-
* @return array|CallbackFilterIterator
69+
* @param iterable<mixed> $iterable
70+
*
71+
* @return array<mixed>|CallbackFilterIterator
6372
*/
6473
function iterable_filter(iterable $iterable, ?callable $filter = null)
6574
{
66-
if (null === $filter) {
75+
if ($filter === null) {
6776
$filter = static function ($value) {
6877
return (bool) $value;
6978
};
@@ -76,21 +85,19 @@ function iterable_filter(iterable $iterable, ?callable $filter = null)
7685
return array_filter($iterable, $filter);
7786
}
7887

79-
8088
/**
8189
* Reduces an iterable.
8290
*
8391
* @param iterable<mixed> $iterable
8492
* @param callable(mixed, mixed) $reduce
93+
*
8594
* @return mixed
8695
*
8796
* @psalm-template TValue
8897
* @psalm-template TResult
89-
*
9098
* @psalm-param iterable<TValue> $iterable
9199
* @psalm-param callable(TResult|null, TValue) $reduce
92100
* @psalm-param TResult|null $initial
93-
*
94101
* @psalm-return TResult|null
95102
*/
96103
function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
@@ -102,6 +109,9 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
102109
return $initial;
103110
}
104111

112+
/**
113+
* @param iterable<mixed> $iterable
114+
*/
105115
function iterable(iterable $iterable, ?callable $filter = null, ?callable $map = null): IterableObject
106116
{
107117
return new IterableObject($iterable, $filter, $map);

tests/IterableFilterTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
37
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
410
use function BenTools\IterableFunctions\iterable_filter;
511
use function BenTools\IterableFunctions\iterable_to_array;
612

@@ -10,7 +16,7 @@ public function testArrayFilter(): void
1016
{
1117
$iterable = ['foo', 'bar'];
1218
$filter = static function ($input) {
13-
return 'bar' === $input;
19+
return $input === 'bar';
1420
};
1521
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
1622
}
@@ -19,7 +25,7 @@ public function testTraversableFilter(): void
1925
{
2026
$iterable = SplFixedArray::fromArray(['foo', 'bar']);
2127
$filter = static function ($input) {
22-
return 'bar' === $input;
28+
return $input === 'bar';
2329
};
2430
$this->assertEquals([1 => 'bar'], iterable_to_array(iterable_filter($iterable, $filter)));
2531
}

tests/IterableMapTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
37
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
410
use function BenTools\IterableFunctions\iterable_map;
511
use function BenTools\IterableFunctions\iterable_to_array;
612

tests/IterableObjectTest.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,75 @@
11
<?php
22

3-
use BenTools\IterableFunctions\IterableObject;
3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
47
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
510
use function BenTools\IterableFunctions\iterable;
11+
use function iterator_to_array;
612

713
final class IterableObjectTest extends TestCase
814
{
915
/**
16+
* @param array<mixed> $data
17+
* @param array<mixed> $expectedResult
18+
*
1019
* @dataProvider dataProvider
1120
*/
12-
public function testFromArrayToIterator($data, $filter = null, $map = null, $expectedResult): void
21+
public function testFromArrayToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
1322
{
1423
$iterableObject = iterable($data, $filter, $map);
15-
$this->assertInstanceOf(IterableObject::class, $iterableObject);
1624
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
1725
}
1826

1927
/**
28+
* @param array<mixed> $data
29+
* @param array<mixed> $expectedResult
30+
*
2031
* @dataProvider dataProvider
2132
*/
22-
public function testFromArrayToArray($data, $filter = null, $map = null, $expectedResult): void
33+
public function testFromArrayToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
2334
{
2435
$iterableObject = iterable($data, $filter, $map);
25-
$this->assertInstanceOf(IterableObject::class, $iterableObject);
2636
$this->assertEquals($expectedResult, $iterableObject->asArray());
2737
}
2838

2939
/**
40+
* @param array<mixed> $data
41+
* @param array<mixed> $expectedResult
42+
*
3043
* @dataProvider dataProvider
3144
*/
32-
public function testFromTraversableToIterator($data, $filter = null, $map = null, $expectedResult): void
45+
public function testFromTraversableToIterator(array $data, ?callable $filter, ?string $map, array $expectedResult): void
3346
{
3447
$data = SplFixedArray::fromArray($data);
3548
$iterableObject = iterable($data, $filter, $map);
36-
$this->assertInstanceOf(IterableObject::class, $iterableObject);
3749
$this->assertEquals($expectedResult, iterator_to_array($iterableObject));
3850
}
3951

4052
/**
53+
* @param array<mixed> $data
54+
* @param array<mixed> $expectedResult
55+
*
4156
* @dataProvider dataProvider
4257
*/
43-
public function testFromTraversableToArray($data, $filter = null, $map = null, $expectedResult): void
58+
public function testFromTraversableToArray(array $data, ?callable $filter, ?string $map, array $expectedResult): void
4459
{
4560
$data = SplFixedArray::fromArray($data);
4661
$iterableObject = iterable($data, $filter, $map);
47-
$this->assertInstanceOf(IterableObject::class, $iterableObject);
4862
$this->assertEquals($expectedResult, $iterableObject->asArray());
4963
}
5064

51-
public function dataProvider()
65+
/**
66+
* @return list<array{array<mixed>, callable|null, string|null, array<mixed>}>>
67+
*/
68+
public function dataProvider(): array
5269
{
5370
$data = ['foo', 'bar'];
5471
$filter = static function ($value) {
55-
return 'bar' === $value;
72+
return $value === 'bar';
5673
};
5774
$map = 'strtoupper';
5875

@@ -83,5 +100,4 @@ public function dataProvider()
83100
],
84101
];
85102
}
86-
87103
}

tests/IterableReduceTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace BenTools\IterableFunctions\Tests;
6+
37
use PHPUnit\Framework\TestCase;
8+
use SplFixedArray;
9+
410
use function BenTools\IterableFunctions\iterable_reduce;
511

612
final class IterableReduceTest extends TestCase
713
{
814
public function testArrayReduce(): void
915
{
10-
$iterable = array(1, 2);
16+
$iterable = [1, 2];
1117
$reduce = static function ($carry, $item) {
1218
return $carry + $item;
1319
};
@@ -16,7 +22,7 @@ public function testArrayReduce(): void
1622

1723
public function testTraversableReduce(): void
1824
{
19-
$iterable = SplFixedArray::fromArray(array(1, 2));
25+
$iterable = SplFixedArray::fromArray([1, 2]);
2026
$reduce = static function ($carry, $item) {
2127
return $carry + $item;
2228
};

0 commit comments

Comments
 (0)