Skip to content

Commit f9d98cc

Browse files
committed
refactor: remove length parameter
BREAKING CHANGE: yes
1 parent 7142179 commit f9d98cc

File tree

1 file changed

+3
-24
lines changed

1 file changed

+3
-24
lines changed

src/ResourceIteratorAggregate.php

+3-24
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,23 @@
1818
*/
1919
final class ResourceIteratorAggregate implements IteratorAggregate
2020
{
21-
/**
22-
* @var null|non-negative-int
23-
*/
24-
private ?int $length = null;
25-
2621
/**
2722
* @var resource
2823
*/
2924
private $resource;
3025

3126
/**
3227
* @param false|resource $resource
33-
* @param null|non-negative-int $length
3428
* @param Closure(resource): T $consumer
3529
*/
36-
public function __construct($resource, private bool $closeResource = false, ?int $length = null, private ?Closure $consumer = null)
30+
public function __construct($resource, private bool $closeResource = false, private ?Closure $consumer = null)
3731
{
3832
if (!is_resource($resource) || 'stream' !== get_resource_type($resource)) {
3933
throw new InvalidArgumentException('Invalid resource type.');
4034
}
4135

4236
$this->resource = $resource;
43-
$this->length = $length;
37+
$this->consumer ??= static fn ($resource): bool|string => fgetc($resource);
4438
}
4539

4640
/**
@@ -49,25 +43,10 @@ public function __construct($resource, private bool $closeResource = false, ?int
4943
public function getIterator(): Generator
5044
{
5145
$closeResource = $this->closeResource;
52-
$length = $this->length;
5346
$resource = $this->resource;
5447

55-
$fgetc =
56-
/**
57-
* @param resource $resource
58-
*/
59-
static fn ($resource): false|string => fgetc($resource);
60-
61-
$fgets =
62-
/**
63-
* @param resource $resource
64-
*/
65-
static fn ($resource): false|string => fgets($resource, $length);
66-
67-
$function = $this->consumer ?? ((null === $length) ? $fgetc : $fgets);
68-
6948
try {
70-
while (false !== $chunk = $function($resource)) {
49+
while ($chunk = ($this->consumer)($resource)) {
7150
yield $chunk;
7251
}
7352
} finally {

0 commit comments

Comments
 (0)