Skip to content

Commit

Permalink
feat: convert array-key to int|string for collection keys (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed May 16, 2024
1 parent fdc7d29 commit 32493f4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
6 changes: 3 additions & 3 deletions stubs/common/Collection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use Illuminate\Contracts\Support\CanBeEscapedWhenCastToString;
use Illuminate\Support\Traits\EnumeratesValues;

/**
* @template TKey of array-key
* @template TKey of int|string
* @template TValue
*
* @implements \ArrayAccess<TKey, TValue>
Expand Down Expand Up @@ -59,7 +59,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
/**
* Push all of the given items onto the collection.
*
* @template TConcatKey of array-key
* @template TConcatKey of int|string
* @template TConcatValue
*
* @param iterable<TConcatKey, TConcatValue> $source
Expand All @@ -70,7 +70,7 @@ class Collection implements ArrayAccess, CanBeEscapedWhenCastToString, Enumerabl
}

/**
* @template TKey of array-key
* @template TKey of int|string
* @template TValue
*
* @implements \Illuminate\Support\Enumerable<TKey, TValue>
Expand Down
2 changes: 1 addition & 1 deletion stubs/common/Contracts/Support.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Htmlable
{}

/**
* @template TKey of array-key
* @template TKey of int|string
* @template TValue
*/
interface Arrayable
Expand Down
2 changes: 1 addition & 1 deletion stubs/common/EloquentCollection.stub
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Illuminate\Contracts\Queue\QueueableCollection;
use Illuminate\Support\Collection as BaseCollection;

/**
* @template TKey of array-key
* @template TKey of int|string
* @template TModel of \Illuminate\Database\Eloquent\Model
*
* @extends \Illuminate\Support\Collection<TKey, TModel>
Expand Down
22 changes: 21 additions & 1 deletion stubs/common/Enumerable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
namespace Illuminate\Support;

/**
* @template TKey of array-key
* @template TKey of int|string
* @template TValue
*
* @extends \IteratorAggregate<TKey, TValue>
*/
interface Enumerable extends \Countable, \IteratorAggregate, \JsonSerializable
{
/**
* Search the collection for a given value and return the corresponding key if successful.
*
* @param TValue|callable(TValue,TKey): bool $value
* @param bool $strict
* @return TKey|bool
*/
public function search($value, $strict = false);

/**
* Get one or a specified number of items randomly from the collection.
*
Expand All @@ -29,4 +38,15 @@ interface Enumerable extends \Countable, \IteratorAggregate, \JsonSerializable
* @return static<array-key, TCombineValue>
*/
public function combine($values);

/**
* Push all of the given items onto the collection.
*
* @template TConcatKey of int|string
* @template TConcatValue
*
* @param iterable<TConcatKey, TConcatValue> $source
* @return static<TKey|TConcatKey, TValue|TConcatValue>
*/
public function concat($source);
}
13 changes: 10 additions & 3 deletions tests/Type/data/collection-generic-static-methods.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

namespace CollectionGenericStaticMethods;

use App\Transaction;
use App\TransactionCollection;
use App\User;
use App\UserCollection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\LazyCollection;
Expand All @@ -11,8 +14,8 @@

/** @var EloquentCollection<int, User> $collection */
/** @var SupportCollection<string, int> $items */
/** @var App\TransactionCollection<int, Transaction> $customEloquentCollection */
/** @var App\UserCollection $secondCustomEloquentCollection */
/** @var TransactionCollection<int, Transaction> $customEloquentCollection */
/** @var UserCollection $secondCustomEloquentCollection */
/** @var LazyCollection<int, User> $lazyCollection */
/** @var User $user */
assertType('Illuminate\Database\Eloquent\Collection<int, int>', EloquentCollection::range(1, 10));
Expand Down Expand Up @@ -161,7 +164,7 @@
assertType('Illuminate\Database\Eloquent\Collection<int, App\User>', $collection->concat([new User()]));
assertType('App\TransactionCollection<int, App\Transaction|App\User>', $customEloquentCollection->concat([new User()]));
assertType('App\UserCollection', $secondCustomEloquentCollection->concat([new User()]));
assertType('Illuminate\Support\Collection<string, App\User|int>', $items->concat([new User()]));
assertType('Illuminate\Support\Collection<int|string, App\User|int>', $items->concat([new User()]));

////////////////////////////
// EnumeratesValues Trait //
Expand Down Expand Up @@ -223,3 +226,7 @@
'type' => 'B',
],
])->groupBy('type'));

/** @var \Illuminate\Support\Enumerable<int, \App\User> $enumerableUsers */

assertType('bool|int', $enumerableUsers->search(fn(User $user) => $user->id === 1));
4 changes: 2 additions & 2 deletions tests/Type/data/collection-make-static.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
/** @phpstan-param EloquentCollection<int, \App\User> $eloquentCollection */
function eloquentCollectionInteger(EloquentCollection $eloquentCollection): void
{
assertType('Illuminate\Support\Collection<(int|string), mixed>', SupportCollection::make($eloquentCollection));
assertType('Illuminate\Support\Collection<int|string, mixed>', SupportCollection::make($eloquentCollection));
}

/** @phpstan-param EloquentCollection<int, User> $eloquentCollection */
function eloquentCollectionUser(EloquentCollection $eloquentCollection): void
{
assertType('Illuminate\Support\Collection<(int|string), mixed>', SupportCollection::make($eloquentCollection));
assertType('Illuminate\Support\Collection<int|string, mixed>', SupportCollection::make($eloquentCollection));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/data/eloquent-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function testOldestToBaseWithQueryExpression()

function testPluckToBaseWithQueryExpression()
{
assertType('Illuminate\Support\Collection<(int|string), mixed>', User::query()
assertType('Illuminate\Support\Collection<int|string, mixed>', User::query()
->whereNull('name')
->pluck(\Illuminate\Support\Facades\DB::raw('created_at'))
->toBase()
Expand Down

0 comments on commit 32493f4

Please sign in to comment.