Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,35 +292,39 @@ public function maxOrNull(?Closure $selector = null): mixed;
* Returns the minimum value produced by the selector.
* Throws if the collection is empty.
*
* @param Closure(E, int):mixed $selector
* @return mixed
* @template R of mixed
* @param Closure(E, int):R $selector
* @return R
* @throws NoSuchElementException
*/
public function minOf(Closure $selector): mixed;

/**
* Returns the minimum value produced by the selector, or null if empty.
*
* @param Closure(E, int):mixed $selector
* @return mixed
* @template R of mixed
* @param Closure(E, int):R $selector
* @return R|null
*/
public function minOfOrNull(Closure $selector): mixed;

/**
* Returns the maximum value produced by the selector.
* Throws if the collection is empty.
*
* @param Closure(E, int):mixed $selector
* @return mixed
* @template R of mixed
* @param Closure(E, int):R $selector
* @return R
* @throws NoSuchElementException
*/
public function maxOf(Closure $selector): mixed;

/**
* Returns the maximum value produced by the selector, or null if empty.
*
* @param Closure(E, int):mixed $selector
* @return mixed
* @template R of mixed
* @param Closure(E, int):R $selector
* @return R|null
*/
public function maxOfOrNull(Closure $selector): mixed;

Expand Down
27 changes: 27 additions & 0 deletions tests/Type/MinMaxOfTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of the Noctud Collection.
* Copyright (c) Noctud.dev
*/

declare(strict_types=1);

namespace Noctud\Collection\Tests\Type;

use function Noctud\Collection\listOf;
use function PHPStan\Testing\assertType;

$strings = listOf(['a', 'bb']);

// minOf/maxOf return the selector's type R instead of mixed.
assertType('int', $strings->minOf(fn (string $s): int => (int) $s));
assertType('int', $strings->maxOf(fn (string $s): int => (int) $s));
assertType('float', $strings->maxOf(fn (string $s): float => (float) $s));

// The OrNull variants add null for the empty-collection case.
assertType('int|null', $strings->minOfOrNull(fn (string $s): int => (int) $s));
assertType('int|null', $strings->maxOfOrNull(fn (string $s): int => (int) $s));

// R is inferred even from an untyped selector parameter.
assertType('int', listOf([1, 2, 3])->minOf(fn ($x) => (int) $x));
Loading