Skip to content

Commit

Permalink
Rename findOne to findFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 15, 2021
1 parent 893f828 commit 7896f29
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ Tests for the existence of an element that satisfies the given predicate.
return $value === 'first';
}); // true
findOne
-------
findFirst
---------

Returns the first element of this collection that satisfies the given predicate.

.. code-block:: php
$collection = new Collection([1, 2, 3, 2, 1]);
$one = $collection->findOne(function(int $key, int $value): bool {
$one = $collection->findFirst(function(int $key, int $value): bool {
return $value > 2 && $key > 1;
}); // 3
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/Common/Collections/AbstractLazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ public function exists(Closure $p): bool
return $this->collection->exists($p);
}

public function findOne(Closure $func)
/**
* {@inheritDoc}
*/
public function findFirst(Closure $func)
{
$this->initialize();

return $this->collection->findOne($func);
return $this->collection->findFirst($func);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/Collections/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@ public function filter(Closure $p): Collection
return $this->createFrom(array_filter($this->elements, $p, ARRAY_FILTER_USE_BOTH));
}

public function findOne(Closure $p)
/**
* {@inheritDoc}
*/
public function findFirst(Closure $p)
{
foreach ($this->elements as $key => $element) {
if ($p($key, $element)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/Common/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function exists(Closure $p): bool;
* @psalm-param Closure(TKey=, T=):bool $p
* @psalm-return T|null
*/
public function findOne(Closure $p);
public function findFirst(Closure $p);

/**
* Returns all the elements of this collection that satisfy the predicate p.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,22 @@ public function testExists(): void
}), 'Element not exists');
}

public function testFindOne(): void
public function testFindFirst(): void
{
$elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0];
$collection = $this->buildCollection($elements);

self::assertSame('a', $collection->findOne(static function ($key, $element) {
self::assertSame('a', $collection->findFirst(static function ($key, $element) {
return $key === 'A' && $element === 'a';
}), 'Element exists');
}

public function testFindOneNotFound(): void
public function testFindFirstNotFound(): void
{
$elements = [1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0];
$collection = $this->buildCollection($elements);

self::assertNull($collection->findOne(static function ($key, $element) {
self::assertNull($collection->findFirst(static function ($key, $element) {
return $key === 'non-existent' && $element === 'non-existent';
}), 'Element does not exists');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public function testExists(): void
self::assertFalse($exists);
}

public function testFindOne(): void
public function testFindFirst(): void
{
$this->collection->add('one');
$this->collection->add('two');
$one = $this->collection->findOne(static function ($k, $e) {
$one = $this->collection->findFirst(static function ($k, $e) {
return $e === 'one';
});
self::assertSame('one', $one);
}

public function testFindOneNotFound(): void
public function testFindFirstNotFound(): void
{
$this->collection->add('one');
$this->collection->add('two');
$other = $this->collection->findOne(static function ($k, $e) {
$other = $this->collection->findFirst(static function ($k, $e) {
return $e === 'other';
});
self::assertNull($other);
Expand Down

0 comments on commit 7896f29

Please sign in to comment.