Skip to content

Commit 677f541

Browse files
committed
Execute sort less times
By sorting every call, we are just pumping up the system. There is no need to do this, only when we need the files.
1 parent 8648d0f commit 677f541

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

packages/guides/src/Files.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ final class Files implements IteratorAggregate, Countable
3333

3434
public function add(string $filename): void
3535
{
36-
if (in_array($filename, $this->files, true)) {
36+
if (array_key_exists($filename, $this->files)) {
3737
return;
3838
}
3939

40-
$this->files[] = $filename;
41-
sort($this->files, SORT_NATURAL | SORT_FLAG_CASE);
40+
$this->files[$filename] = $filename;
4241
}
4342

4443
/** @return Iterator<string> */
4544
public function getIterator(): Iterator
4645
{
46+
sort($this->files, SORT_NATURAL | SORT_FLAG_CASE);
47+
4748
return new ArrayIterator($this->files);
4849
}
4950

0 commit comments

Comments
 (0)