Skip to content

Commit 505b0a9

Browse files
authored
Remove ArrayIterator::uasort and SplFileObject::READ_AHEAD usage (#590)
1 parent 2ba4c96 commit 505b0a9

8 files changed

Lines changed: 24 additions & 50 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All Notable changes to `Csv` will be documented in this file
2020
- `EscapeFormula` round-trip now preserves fields starting with the escape character
2121
- BOM handling is re-written to fix [#586]([https://github](https://github.com/thephpleague/csv/issues/586))
2222
- CSV Header during CSV download.
23+
- Remove `ArrayIterator::uasort` usage because the method is deprecated in PHP8.6.
2324

2425
### Remove
2526

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Please find below the PHP support for `Csv` version 9.
4242
| 9.8.0 | PHP 7.4.0 | PHP 8.1.x |
4343
| 9.9.0 | PHP 8.1.2 | PHP 8.3.x |
4444
| 9.16.0 | PHP 8.1.2 | PHP 8.x |
45+
| 9.29.0 | PHP 8.2.0 | PHP 8.x |
4546

4647
## Install
4748

src/Query/Ordering/Column.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use League\Csv\Query\QueryException;
2020
use League\Csv\Query\Row;
2121
use League\Csv\Query\Sort;
22-
use OutOfBoundsException;
2322
use ReflectionException;
2423
use SortDirection;
2524

@@ -28,6 +27,7 @@
2827
use function iterator_to_array;
2928
use function strtoupper;
3029
use function trim;
30+
use function uasort;
3131

3232
/**
3333
* Enable sorting a record based on the value of a one of its cell.
@@ -111,20 +111,9 @@ public function __invoke(mixed $valueA, mixed $valueB): int
111111

112112
public function sort(iterable $value): Iterator
113113
{
114-
$class = new class () extends ArrayIterator {
115-
public function seek(int $offset): void
116-
{
117-
try {
118-
parent::seek($offset);
119-
} catch (OutOfBoundsException) {
120-
return;
121-
}
122-
}
123-
};
124-
125-
$it = new $class(!is_array($value) ? iterator_to_array($value) : $value);
126-
$it->uasort($this);
114+
$arr = !is_array($value) ? iterator_to_array($value) : $value;
115+
uasort($arr, $this);
127116

128-
return $it;
117+
return new ArrayIterator($arr);
129118
}
130119
}

src/Query/Ordering/MultiSort.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
use League\Csv\MapIterator;
2020
use League\Csv\Query\Sort;
2121
use League\Csv\Query\SortCombinator;
22-
use OutOfBoundsException;
2322

2423
use function array_map;
24+
use function is_array;
25+
use function iterator_to_array;
26+
use function uasort;
2527

2628
/**
2729
* Enable sorting a record based on multiple column.
@@ -96,20 +98,9 @@ public function sort(iterable $value): Iterator
9698
return MapIterator::toIterator($value);
9799
}
98100

99-
$class = new class () extends ArrayIterator {
100-
public function seek(int $offset): void
101-
{
102-
try {
103-
parent::seek($offset);
104-
} catch (OutOfBoundsException) {
105-
return;
106-
}
107-
}
108-
};
109-
110-
$it = new $class(!is_array($value) ? iterator_to_array($value) : $value);
111-
$it->uasort($this);
101+
$arr = !is_array($value) ? iterator_to_array($value) : $value;
102+
uasort($arr, $this);
112103

113-
return $it;
104+
return new ArrayIterator($arr);
114105
}
115106
}

src/Query/Sort.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* The class can be used directly with PHP's
2222
* <ol>
2323
* <li>usort and uasort.</li>
24-
* <li>ArrayIterator::uasort.</li>
2524
* <li>ArrayObject::uasort.</li>
2625
* </ol>
2726
*/

src/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ protected function getSeekableIterator(): SeekableIterator
205205
*/
206206
protected function getDocument(): SplFileObject|Stream
207207
{
208-
$this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD);
208+
$this->document->setFlags(SplFileObject::READ_CSV);
209209
$this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
210210
$this->document->rewind();
211211

src/ResultSet.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use League\Csv\Serializer\TypeCastingFailed;
2828
use LimitIterator;
2929
use mysqli_result;
30+
use OutOfBoundsException;
3031
use PDOStatement;
3132
use PgSql\Result;
3233
use ReflectionException;
@@ -534,8 +535,12 @@ public function nth(int $nth): array
534535
{
535536
0 <= $nth || throw InvalidArgument::dueToInvalidRecordOffset($nth, __METHOD__);
536537

537-
$iterator = new LimitIterator($this->getIterator(), $nth, 1);
538-
$iterator->rewind();
538+
try {
539+
$iterator = new LimitIterator($this->getIterator(), $nth, 1);
540+
$iterator->rewind();
541+
} catch (OutOfBoundsException) {
542+
return [];
543+
}
539544

540545
/** @var array|null $result */
541546
$result = $iterator->current(); /* @phpstan-ignore-line */

src/Statement.php

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Closure;
1919
use Deprecated;
2020
use Iterator;
21-
use OutOfBoundsException;
2221
use ReflectionException;
2322
use ReflectionFunction;
2423
use SortDirection;
@@ -464,22 +463,11 @@ protected function buildOrderBy(Iterator $iterator): Iterator
464463
return $cmp ?? 0;
465464
};
466465

467-
$class = new class () extends ArrayIterator {
468-
public function seek(int $offset): void
469-
{
470-
try {
471-
parent::seek($offset);
472-
} catch (OutOfBoundsException) {
473-
return;
474-
}
475-
}
476-
};
477-
478-
/** @var ArrayIterator<array-key, array<string|null>> $it */
479-
$it = new $class([...$iterator]);
480-
$it->uasort($compare);
466+
/** @var array<array-key, array<string|null>> $arr */
467+
$arr = [...$iterator];
468+
uasort($arr, $compare);
481469

482-
return $it;
470+
return new ArrayIterator($arr);
483471
}
484472

485473

0 commit comments

Comments
 (0)