Skip to content

Commit

Permalink
Remove iterator_to_array usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 21, 2023
1 parent db12286 commit 0503d87
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 59 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
"doctrine/collections": "^2.1.2",
"friendsofphp/php-cs-fixer": "^v3.14.3",
"phpbench/phpbench": "^1.2.8",
"phpstan/phpstan": "^1.9.17",
"phpstan/phpstan-deprecation-rules": "^1.1.1",
"phpstan/phpstan-phpunit": "^1.3.4",
"phpstan/phpstan-strict-rules": "^1.4.5",
"phpunit/phpunit": "^10.0.7",
"phpstan/phpstan": "^1.10.0",
"phpstan/phpstan-deprecation-rules": "^1.1.2",
"phpstan/phpstan-phpunit": "^1.3.7",
"phpstan/phpstan-strict-rules": "^1.5.0",
"phpunit/phpunit": "^10.0.11",
"ext-xdebug": "*"
},
"autoload": {
Expand Down
5 changes: 1 addition & 4 deletions src/AbstractCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use SplTempFileObject;
use function chr;
use function function_exists;
use function iterator_to_array;
use function ob_get_clean;
use function ob_start;
use function strtolower;
Expand Down Expand Up @@ -149,7 +148,6 @@ public function testChunkDoesNotTimeoutAfterReading(): void
{
$raw_csv = "john,doe,[email protected]\njane,doe,[email protected]\n";
$csv = Reader::createFromString($raw_csv);
iterator_to_array($csv->getRecords());

self::assertSame($raw_csv, $csv->toString());
}
Expand All @@ -173,8 +171,7 @@ public function testChunkTriggersException(): void
{
$this->expectException(InvalidArgument::class);

$chunk = $this->csv->chunk(0);
iterator_to_array($chunk);
[...$this->csv->chunk(0)];
}

public function testChunk(): void
Expand Down
11 changes: 4 additions & 7 deletions src/CharsetConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function testItDoesNotChangeTheCSVContentIfNoBOMSequenceIsFound(): void
self::assertSame(
[['start
end']],
iterator_to_array($reader)
[...$reader]
);
}

Expand All @@ -184,7 +184,7 @@ public static function testItSkipBOMSequenceBeforeConsumingTheCSVStream(string $
self::assertSame(
[['start
end']],
iterator_to_array($reader)
[...$reader]
);
}

Expand All @@ -202,7 +202,7 @@ public function testItOnlySkipOnceTheBOMSequenceBeforeConsumingTheCSVStreamOnMul
self::assertSame(
[[$sequence.'start
end']],
iterator_to_array($reader)
[...$reader]
);
}

Expand All @@ -213,10 +213,7 @@ public function testItOnlySkipOnceTheBOMSequenceBeforeConsumingTheCSVStreamOnSin
CharsetConverter::addBOMSkippingTo($reader);
$reader->includeInputBOM();

self::assertSame(
[[$sequence.'start', $sequence.'end']],
iterator_to_array($reader)
);
self::assertSame([[$sequence.'start', $sequence.'end']], [...$reader]);
}

public static function providesBOMSequences(): iterable
Expand Down
2 changes: 1 addition & 1 deletion src/MapIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testMapIteratorCanActLikeArrayMapWithOneArray(): void

self::assertSame(
array_map($mapper, $array),
iterator_to_array(new MapIterator($iterator, $mapper), true)
[...new MapIterator($iterator, $mapper)]
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use function count;
use function is_array;
use function iterator_count;
use function iterator_to_array;
use function mb_strlen;
use function mb_substr;
use function strlen;
Expand Down Expand Up @@ -235,7 +234,7 @@ public function getIterator(): Iterator
*/
public function jsonSerialize(): array
{
return iterator_to_array($this->getRecords(), false);
return array_values([...$this->getRecords()]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function testHeaderThrowsIfItContainsNonStringNames(): void
{
$this->expectException(SyntaxError::class);

iterator_to_array($this->csv->getRecords(['field1', 2, 'field3']));
[...$this->csv->getRecords(['field1', 2, 'field3'])];
}

#[DataProvider('validBOMSequences')]
Expand Down
3 changes: 1 addition & 2 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use function array_search;
use function is_string;
use function iterator_count;
use function iterator_to_array;

/**
* Represents the result set of a {@link Reader} processed by a {@link Statement}.
Expand Down Expand Up @@ -138,7 +137,7 @@ public function count(): int

public function jsonSerialize(): array
{
return iterator_to_array($this->records, false);
return array_values([...$this->records]);
}

/**
Expand Down
37 changes: 12 additions & 25 deletions src/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use SplTempFileObject;
use function current;
use function in_array;
use function iterator_to_array;
use function json_encode;
use function next;

Expand Down Expand Up @@ -67,10 +66,7 @@ public function testFilter(): void
$result2 = $stmt->where($func2)->process($result1, ['foo', 'bar']);
$result3 = $stmt->where($func2)->process($result2, ['foo', 'bar']);

self::assertNotContains(
['jane', 'doe', '[email protected]'],
iterator_to_array($result1, false)
);
self::assertNotContains(['jane', 'doe', '[email protected]'], [...$result1]);

self::assertCount(0, $result2);
self::assertEquals($result3, $result2);
Expand All @@ -82,13 +78,14 @@ public function testFetchColumnTriggersException(int|string $field): void
$this->expectException(InvalidArgument::class);

$this->csv->setHeaderOffset(0);
$resultSet = $this->stmt->process($this->csv);
if (is_int($field)) {
iterator_to_array($this->stmt->process($this->csv)->fetchColumnByOffset($field), false);
[...$resultSet->fetchColumnByOffset($field)];

return;
}

iterator_to_array($this->stmt->process($this->csv)->fetchColumnByName($field), false);
[...$resultSet->fetchColumnByName($field)];
}

public static function invalidFieldNameProvider(): array
Expand All @@ -104,8 +101,7 @@ public function testFetchColumnTriggersOutOfRangeException(): void
$this->expectException(InvalidArgument::class);

$this->csv->setHeaderOffset(0);
$res = $this->stmt->process($this->csv)->fetchColumnByOffset(-1);
iterator_to_array($res, false);
[...$this->stmt->process($this->csv)->fetchColumnByOffset(-1)];
}

public function testFetchColumn(): void
Expand All @@ -119,21 +115,15 @@ public function testFetchColumnByNameTriggersException(): void
$this->expectException(InvalidArgument::class);
$this->csv->setHeaderOffset(0);

iterator_to_array(
$this->stmt->process($this->csv)->fetchColumnByName('foobar'),
false
);
[...$this->stmt->process($this->csv)->fetchColumnByName('foobar')];
}

public function testFetchColumnByOffsetTriggersException(): void
{
$this->expectException(InvalidArgument::class);
$this->csv->setHeaderOffset(0);

iterator_to_array(
$this->stmt->process($this->csv)->fetchColumnByOffset(24),
false
);
[...$this->stmt->process($this->csv)->fetchColumnByOffset(24)];
}

public function testFetchColumnByOffsetTriggersOutOfRangeException(): void
Expand All @@ -142,10 +132,7 @@ public function testFetchColumnByOffsetTriggersOutOfRangeException(): void

$this->csv->setHeaderOffset(0);

iterator_to_array(
$this->stmt->process($this->csv)->fetchColumnByOffset(-1),
false
);
[...$this->stmt->process($this->csv)->fetchColumnByOffset(-1)];
}

public function testFetchAssocWithRowIndex(): void
Expand All @@ -166,7 +153,7 @@ public function testFetchAssocWithRowIndex(): void
$csv->setHeaderOffset(2);
self::assertContains(
['D' => '6', 'E' => '7', 'F' => '8'],
iterator_to_array($this->stmt->process($csv), false)
[...$this->stmt->process($csv)]
);
}

Expand Down Expand Up @@ -194,7 +181,7 @@ public function testFetchColumnInconsistentColumnCSV(): void
$csv = Reader::createFromFileObject($file);
$res = $this->stmt->process($csv)->fetchColumnByOffset(2);

self::assertCount(1, iterator_to_array($res));
self::assertCount(1, [...$res]);
}

public function testFetchColumnEmptyCol(): void
Expand All @@ -210,7 +197,7 @@ public function testFetchColumnEmptyCol(): void
}
$csv = Reader::createFromFileObject($file);
$res = $this->stmt->process($csv)->fetchColumnByOffset(2);
self::assertCount(0, iterator_to_array($res));
self::assertCount(0, [...$res]);
}

public function testfetchOne(): void
Expand Down Expand Up @@ -261,7 +248,7 @@ public static function fetchPairsDataProvider(): array

public function testFetchPairsWithInvalidOffset(): void
{
self::assertCount(0, iterator_to_array($this->stmt->process($this->csv)->fetchPairs(10, 1), true));
self::assertCount(0, [...$this->stmt->process($this->csv)->fetchPairs(10, 1)]);
}

public function testFetchPairsWithInvalidValue(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function buildOrderBy(Iterator $iterator): Iterator
};

/** @var ArrayIterator<array-key, array<string|null>> $it */
$it = new ArrayIterator(iterator_to_array($iterator));
$it = new ArrayIterator([...$iterator]);
$it->uasort($compare);

return $it;
Expand Down
15 changes: 4 additions & 11 deletions src/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use SplTempFileObject;
use function array_reverse;
use function in_array;
use function iterator_to_array;
use function strcmp;
use function strlen;

Expand Down Expand Up @@ -108,10 +107,7 @@ public function testIntervalThrowException(): void
{
$this->expectException(OutOfBoundsException::class);

iterator_to_array($this->stmt
->offset(1)
->limit(0)
->process($this->csv));
[...$this->stmt->offset(1)->limit(0)->process($this->csv)];
}

public function testFilter(): void
Expand All @@ -124,10 +120,7 @@ public function testFilter(): void
$result2 = $stmt->where($func2)->process($result1, ['foo', 'bar']);
$result3 = $stmt->where($func2)->process($result2, ['foo', 'bar']);

self::assertNotContains(
['jane', 'doe', '[email protected]'],
iterator_to_array($result1, false)
);
self::assertNotContains(['jane', 'doe', '[email protected]'], [...$result1]);

self::assertCount(0, $result2);
self::assertEquals($result3, $result2);
Expand All @@ -139,7 +132,7 @@ public function testOrderBy(): void
->orderBy(fn (array $rowA, array $rowB): int => strcmp($rowA[0], $rowB[0]))
->process($this->csv);

self::assertSame(array_reverse($this->expected), iterator_to_array($calculated, false));
self::assertSame(array_reverse($this->expected), array_values([...$calculated]));
}

public function testOrderByWithEquity(): void
Expand All @@ -148,6 +141,6 @@ public function testOrderByWithEquity(): void
->orderBy(fn (array $rowA, array $rowB): int => strlen($rowA[0]) <=> strlen($rowB[0]))
->process($this->csv);

self::assertSame($this->expected, iterator_to_array($calculated, false));
self::assertSame($this->expected, array_values([...$calculated]));
}
}

0 comments on commit 0503d87

Please sign in to comment.