forked from larastan/larastan
/
CorrectCollectionCalls.php
153 lines (133 loc) · 3.53 KB
/
CorrectCollectionCalls.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Tests\Rules\Data;
use App\Account;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class CorrectCollectionCalls
{
public function staticCount(): int
{
return User::count();
}
public function hydrate(): ?User
{
$users = [['name' => 'Daan', 'email' => 'test@test.dev']];
return User::hydrate($users)->first();
}
/** @return Collection<int, mixed> */
public function pluckQuery(): Collection
{
return User::query()->pluck('id');
}
/** @return Collection<int, mixed> */
public function pluckComputed(): Collection
{
return User::all()->pluck('allCapsName');
}
/** @return Collection<int, mixed> */
public function pluckRelation(): Collection
{
return User::with(['accounts'])->get()->pluck('accounts');
}
public function firstRelation(): ?Account
{
return User::firstOrFail()->accounts()->first();
}
/** @phpstan-return mixed */
public function maxQuery()
{
return DB::table('users')->max('id');
}
/** @phpstan-return mixed */
public function collectionCalls()
{
return collect([1, 2, 3])->flip()->reverse()->sum();
}
/**
* Can't analyze the closure as a parameter to contains, so should not throw any error.
*
* @return bool
*/
public function testContainsClosure(): bool
{
return User::where('id', '>', 1)->get()->contains(function (User $user): bool {
return $user->id === 2;
});
}
/**
* Can't analyze the arrow function as a parameter to contains, so should not throw any error.
*
* @return bool
*/
public function testContainsArrowFunction(): bool
{
return User::where('id', '>', 1)->get()->contains(fn (User $user): bool => $user->id === 2);
}
/**
* Can't analyze the closure as a parameter to first, so should not throw any error.
*
* @return User|null
*/
public function testFirstClosure(): ?User
{
return User::where('id', '>', 1)->get()->first(function (User $user): bool {
return $user->id === 2;
});
}
/**
* Can't analyze the arrow function as a parameter to first, so should not throw any error.
*
* @return User|null
*/
public function testFirstArrowFunction(): ?User
{
return User::where('id', '>', 1)->get()->first(fn (User $user): bool => $user->id === 2);
}
/** @phpstan-return mixed */
public function testAggregateNoArgs()
{
return User::query()
->select([DB::raw('COUNT(*) as temp')])
->pluck('temp')
->sum();
}
/** @phpstan-return mixed */
public function testRelationAggregate(User $user)
{
return $user->group()
->withCount(['accounts' => function ($query) {
$query->where('id', '<>', 1);
}])
->pluck('id')
->avg();
}
}
class Foo extends Model
{
/**
* @param \Illuminate\Database\Query\Builder $query
* @return FooBuilder
*/
public function newEloquentBuilder($query): FooBuilder
{
return new FooBuilder($query);
}
}
/**
* @extends Builder<Foo>
*/
class FooBuilder extends Builder
{
/**
* @return mixed
*/
public function returnMixed()
{
/** @var mixed */
return $this;
}
}