Skip to content

Commit 0a96a93

Browse files
Merge pull request #31 from quant-php/dev
refactor: findBy() of AbstractList returns null or AbstractList
2 parents f9e3e4a + e9172a7 commit 0a96a93

File tree

4 files changed

+113
-8
lines changed

4 files changed

+113
-8
lines changed

src/Quant/Core/AbstractList.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,24 @@ public function map(callable $mapFn): static
103103

104104

105105
/**
106-
* Returns the entry in this list given the callback function.
106+
* Returns a new AbstractList containing all the entries for which the callable returned `true`.
107+
* Returns null if no matches were found.
107108
*
108109
* @param callable $findFn A callback. Return true in the function to indicate a match. First match will
109110
* be returned. The callback is passed the current entry.
110111
*
111-
* @return ?TValue
112+
* @return null|static
112113
*/
113-
public function findBy(callable $findFn): mixed
114+
public function findBy(callable $findFn): null|static
114115
{
116+
$matches = [];
115117
foreach ($this->data as $resource) {
116118
if ($findFn($resource) === true) {
117-
return $resource;
119+
$matches[] = $resource;
118120
}
119121
}
120122

121-
return null;
123+
return count($matches) === 0 ? null : static::make(...$matches);
122124
}
123125

124126

@@ -194,7 +196,7 @@ protected function compareItems(mixed $a, mixed $b): bool
194196
*
195197
* @throws OutOfBoundsException
196198
*/
197-
protected function doInsert(mixed $offset, mixed $value)
199+
private function doInsert(mixed $offset, mixed $value)
198200
{
199201
if (!is_null($offset) && !is_int($offset)) {
200202
throw new OutOfBoundsException(
@@ -216,7 +218,7 @@ protected function doInsert(mixed $offset, mixed $value)
216218
*
217219
* @throws TypeError
218220
*/
219-
protected function assertTypeFor(mixed $value): bool
221+
private function assertTypeFor(mixed $value): bool
220222
{
221223
$entityType = $this->getType();
222224

src/Quant/Core/Tests/AbstractListTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use OutOfBoundsException;
2121
use Quant\Core\Contract\Comparable;
2222
use Quant\Core\Contract\Equatable;
23+
use Quant\Core\Tests\Resources\Entity;
24+
use Quant\Core\Tests\Resources\EntityList;
2325
use stdClass;
2426
use PHPUnit\Framework\TestCase;
2527
use TypeError;
@@ -211,9 +213,11 @@ public function testFindBy(): void
211213

212214
/** @phpstan-ignore-next-line */
213215
$cb = $mock->findCallback(...);
216+
214217
$this->assertSame(
215218
$cmpList[1],
216-
$abstractList->findBy($cb)
219+
/** @phpstan-ignore-next-line */
220+
$abstractList->findBy($cb)[0]
217221
);
218222
}
219223

@@ -361,6 +365,36 @@ public function getType(): string
361365
}
362366

363367

368+
public function testDocs(): void
369+
{
370+
371+
$listA = new EntityList();
372+
$listA[] = new Entity("1");
373+
$listA[] = new Entity("1");
374+
375+
$listB = new EntityList();
376+
$listB[] = new Entity("1");
377+
$listB[] = new Entity("2");
378+
379+
$this->assertFalse($listA->equals($listB));
380+
381+
$listC = EntityList::make(new Entity("3"), new Entity("4"));
382+
383+
$this->assertTrue($listC->map(fn(Entity $item): Entity => $item->setValue("1"))->equals($listA));
384+
385+
/**
386+
* @var Entity $head
387+
*/
388+
$head = $listB->peek();
389+
$this->assertSame("2", $head->getValue());
390+
391+
$this->assertNull($listC->findBy(fn (Entity $item): bool => $item->getValue() === "a"));
392+
$this->assertEquals(
393+
$listC->findBy(fn (Entity $item): bool => $item->getValue() !== "a")?->toArray(),
394+
[$listC[0], $listC[1]]
395+
);
396+
}
397+
364398

365399
// ---------------------
366400
// Helper Functions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the quant project.
5+
*
6+
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
7+
*
8+
* For full copyright and license information, please consult the LICENSE-file distributed
9+
* with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Quant\Core\Tests\Resources;
15+
16+
use Quant\Core\Attribute\Getter;
17+
use Quant\Core\Attribute\Setter;
18+
use Quant\Core\Trait\AccessorTrait;
19+
20+
#[Getter] #[Setter]
21+
class Entity
22+
{
23+
use AccessorTrait;
24+
25+
public function __construct(private string $value)
26+
{
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the quant project.
5+
*
6+
* (c) 2023 Thorsten Suckow-Homberg <[email protected]>
7+
*
8+
* For full copyright and license information, please consult the LICENSE-file distributed
9+
* with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Quant\Core\Tests\Resources;
15+
16+
use Quant\Core\AbstractList;
17+
use stdClass;
18+
19+
/**
20+
* @extends AbstractList<Entity>
21+
*/
22+
class EntityList extends AbstractList
23+
{
24+
public function getType(): string
25+
{
26+
return Entity::class;
27+
}
28+
29+
protected function compareItems(mixed $a, mixed $b): bool
30+
{
31+
if (!($a instanceof Entity) && !($b instanceof Entity)) {
32+
return false;
33+
}
34+
35+
/**
36+
* @var Entity $a
37+
* @var Entity $b
38+
*/
39+
return $a->getValue() === $b->getValue();
40+
}
41+
}

0 commit comments

Comments
 (0)