Skip to content

Commit 0416dc4

Browse files
authored
[minor] Be able to remove most of method annotations on user factories (#185)
PHPStorm 2021.2+ has rudimentary generic annotation support.
1 parent 468e80b commit 0416dc4

File tree

8 files changed

+48
-28
lines changed

8 files changed

+48
-28
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ use Zenstruck\Foundry\ModelFactory;
196196
use Zenstruck\Foundry\Proxy;
197197

198198
/**
199+
* @extends ModelFactory<Post>
200+
*
199201
* @method static Post|Proxy createOne(array $attributes = [])
200202
* @method static Post[]|Proxy[] createMany(int $number, $attributes = [])
201203
* @method static Post|Proxy find($criteria)
@@ -209,7 +211,7 @@ use Zenstruck\Foundry\Proxy;
209211
* @method static Post[]|Proxy[] randomSet(int $number, array $attributes = []))
210212
* @method static Post[]|Proxy[] randomRange(int $min, int $max, array $attributes = []))
211213
* @method static PostRepository|RepositoryProxy repository()
212-
* @method Post|Proxy create($attributes = [])
214+
* @method Post|Proxy create(array|callable $attributes = [])
213215
*/
214216
final class PostFactory extends ModelFactory
215217
{

src/AnonymousFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public static function new(string $class, $defaultAttributes = []): self
2222
* Try and find existing object for the given $attributes. If not found,
2323
* instantiate and persist.
2424
*
25-
* @return Proxy|object
26-
*
25+
* @return Proxy&TModel
2726
* @psalm-return Proxy<TModel>
2827
*/
2928
public function findOrCreate(array $attributes): Proxy
@@ -74,8 +73,7 @@ public function random(array $attributes = []): Proxy
7473
/**
7574
* Fetch one random object and create a new object if none exists.
7675
*
77-
* @return Proxy|object
78-
*
76+
* @return Proxy&TModel
7977
* @psalm-return Proxy<TModel>
8078
*/
8179
public function randomOrCreate(array $attributes = []): Proxy

src/Factory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Faker;
66

77
/**
8-
* @template TObject as object
8+
* @template TObject of object
99
* @abstract
1010
*
1111
* @author Kevin Bond <kevinbond@gmail.com>
@@ -68,8 +68,7 @@ public function __call(string $name, array $arguments)
6868
/**
6969
* @param array|callable $attributes
7070
*
71-
* @return Proxy|object
72-
*
71+
* @return Proxy<TObject>&TObject
7372
* @psalm-return Proxy<TObject>
7473
*/
7574
final public function create($attributes = []): Proxy
@@ -119,7 +118,7 @@ function($value) {
119118
/**
120119
* @see FactoryCollection::__construct()
121120
*
122-
* @psalm-return FactoryCollection<TObject>
121+
* @return FactoryCollection<TObject>
123122
*/
124123
final public function many(int $min, ?int $max = null): FactoryCollection
125124
{

src/FactoryCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Zenstruck\Foundry;
44

55
/**
6-
* @template TObject as object
6+
* @template TObject of object
77
*
88
* @author Kevin Bond <kevinbond@gmail.com>
99
*/
@@ -37,7 +37,7 @@ public function __construct(Factory $factory, int $min, ?int $max = null)
3737
/**
3838
* @param array|callable $attributes
3939
*
40-
* @return Proxy[]|object[]
40+
* @return list<TObject&Proxy<TObject>>
4141
*
4242
* @psalm-suppress InvalidReturnType
4343
* @psalm-return list<Proxy<TObject>>

src/ModelFactory.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @template TModel of object
77
* @template-extends Factory<TModel>
88
*
9-
* @method static Proxy[]|object[] createMany(int $number, array|callable $attributes = [])
9+
* @method static Proxy[]|TModel[] createMany(int $number, array|callable $attributes = [])
1010
* @psalm-method static list<Proxy<TModel>> createMany(int $number, array|callable $attributes = [])
1111
*
1212
* @author Kevin Bond <kevinbond@gmail.com>
@@ -66,8 +66,7 @@ final public static function new($defaultAttributes = [], string ...$states): se
6666
/**
6767
* A shortcut to create a single model without states.
6868
*
69-
* @return Proxy|object
70-
*
69+
* @return Proxy<TModel>&TModel
7170
* @psalm-return Proxy<TModel>
7271
*/
7372
final public static function createOne(array $attributes = []): Proxy
@@ -79,8 +78,7 @@ final public static function createOne(array $attributes = []): Proxy
7978
* Try and find existing object for the given $attributes. If not found,
8079
* instantiate and persist.
8180
*
82-
* @return Proxy|object
83-
*
81+
* @return Proxy<TModel>&TModel
8482
* @psalm-return Proxy<TModel>
8583
*/
8684
final public static function findOrCreate(array $attributes): Proxy
@@ -95,6 +93,9 @@ final public static function findOrCreate(array $attributes): Proxy
9593
/**
9694
* @see RepositoryProxy::first()
9795
*
96+
* @return Proxy<TModel>&TModel
97+
* @psalm-return Proxy<TModel>
98+
*
9899
* @throws \RuntimeException If no entities exist
99100
*/
100101
final public static function first(string $sortedField = 'id'): Proxy
@@ -109,6 +110,9 @@ final public static function first(string $sortedField = 'id'): Proxy
109110
/**
110111
* @see RepositoryProxy::last()
111112
*
113+
* @return Proxy<TModel>&TModel
114+
* @psalm-return Proxy<TModel>
115+
*
112116
* @throws \RuntimeException If no entities exist
113117
*/
114118
final public static function last(string $sortedField = 'id'): Proxy
@@ -122,6 +126,9 @@ final public static function last(string $sortedField = 'id'): Proxy
122126

123127
/**
124128
* @see RepositoryProxy::random()
129+
*
130+
* @return Proxy<TModel>&TModel
131+
* @psalm-return Proxy<TModel>
125132
*/
126133
final public static function random(array $attributes = []): Proxy
127134
{
@@ -131,8 +138,7 @@ final public static function random(array $attributes = []): Proxy
131138
/**
132139
* Fetch one random object and create a new object if none exists.
133140
*
134-
* @return Proxy|object
135-
*
141+
* @return Proxy<TModel>&TModel
136142
* @psalm-return Proxy<TModel>
137143
*/
138144
final public static function randomOrCreate(array $attributes = []): Proxy
@@ -146,6 +152,9 @@ final public static function randomOrCreate(array $attributes = []): Proxy
146152

147153
/**
148154
* @see RepositoryProxy::randomSet()
155+
*
156+
* @return list<TModel&Proxy<TModel>>
157+
* @psalm-return list<Proxy<TModel>>
149158
*/
150159
final public static function randomSet(int $number, array $attributes = []): array
151160
{
@@ -154,6 +163,9 @@ final public static function randomSet(int $number, array $attributes = []): arr
154163

155164
/**
156165
* @see RepositoryProxy::randomRange()
166+
*
167+
* @return list<TModel&Proxy<TModel>>
168+
* @psalm-return list<Proxy<TModel>>
157169
*/
158170
final public static function randomRange(int $min, int $max, array $attributes = []): array
159171
{
@@ -178,6 +190,9 @@ final public static function truncate(): void
178190

179191
/**
180192
* @see RepositoryProxy::findAll()
193+
*
194+
* @return list<TModel&Proxy<TModel>>
195+
* @psalm-return list<Proxy<TModel>>
181196
*/
182197
final public static function all(): array
183198
{
@@ -187,6 +202,9 @@ final public static function all(): array
187202
/**
188203
* @see RepositoryProxy::find()
189204
*
205+
* @return Proxy<TModel>&TModel
206+
* @psalm-return Proxy<TModel>
207+
*
190208
* @throws \RuntimeException If no entity found
191209
*/
192210
final public static function find($criteria): Proxy
@@ -200,6 +218,9 @@ final public static function find($criteria): Proxy
200218

201219
/**
202220
* @see RepositoryProxy::findBy()
221+
*
222+
* @return list<TModel&Proxy<TModel>>
223+
* @psalm-return list<Proxy<TModel>>
203224
*/
204225
final public static function findBy(array $attributes): array
205226
{

src/Proxy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function __toString(): string
8787
/**
8888
* @internal
8989
*
90-
* @template TObject as object
90+
* @template TObject of object
9191
* @psalm-param TObject $object
9292
* @psalm-return Proxy<TObject>
9393
*/
@@ -105,7 +105,7 @@ public function isPersisted(): bool
105105
}
106106

107107
/**
108-
* @psalm-return TProxiedObject
108+
* @return TProxiedObject
109109
*/
110110
public function object(): object
111111
{

src/RepositoryProxy.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function assertNotExists($criteria, string $message = ''): self
164164
}
165165

166166
/**
167-
* @return Proxy|object|null
167+
* @return Proxy&TProxiedObject|null
168168
*
169169
* @psalm-return Proxy<TProxiedObject>|null
170170
*/
@@ -174,7 +174,7 @@ public function first(string $sortedField = 'id'): ?Proxy
174174
}
175175

176176
/**
177-
* @return Proxy|object|null
177+
* @return Proxy&TProxiedObject|null
178178
*
179179
* @psalm-return Proxy<TProxiedObject>|null
180180
*/
@@ -208,7 +208,7 @@ public function truncate(): void
208208
*
209209
* @param array $attributes The findBy criteria
210210
*
211-
* @return Proxy|object
211+
* @return Proxy&TProxiedObject
212212
*
213213
* @throws \RuntimeException if no objects are persisted
214214
*
@@ -276,7 +276,7 @@ public function randomRange(int $min, int $max, array $attributes = []): array
276276
/**
277277
* @param object|array|mixed $criteria
278278
*
279-
* @return Proxy|object|null
279+
* @return Proxy&TProxiedObject|null
280280
*
281281
* @psalm-param Proxy<TProxiedObject>|array|mixed $criteria
282282
* @psalm-return Proxy<TProxiedObject>|null
@@ -314,7 +314,7 @@ public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $
314314
/**
315315
* @param array|null $orderBy Some ObjectRepository's (ie Doctrine\ORM\EntityRepository) add this optional parameter
316316
*
317-
* @return Proxy|object|null
317+
* @return Proxy&TProxiedObject|null
318318
*
319319
* @throws \RuntimeException if the wrapped ObjectRepository does not have the $orderBy parameter
320320
*

src/functions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @see Factory::__construct()
99
*
10-
* @template TObject as object
10+
* @template TObject of object
1111
* @psalm-param class-string<TObject> $class
1212
* @psalm-return AnonymousFactory<TObject>
1313
*/
@@ -19,7 +19,7 @@ function factory(string $class, $defaultAttributes = []): AnonymousFactory
1919
/**
2020
* @see Factory::create()
2121
*
22-
* @return Proxy|object
22+
* @return Proxy&TObject
2323
*
2424
* @template TObject of object
2525
* @psalm-param class-string<TObject> $class
@@ -47,7 +47,7 @@ function create_many(int $number, string $class, $attributes = []): array
4747
/**
4848
* Instantiate object without persisting.
4949
*
50-
* @return Proxy|object "unpersisted" Proxy wrapping the instantiated object
50+
* @return Proxy&TObject "unpersisted" Proxy wrapping the instantiated object
5151
*
5252
* @template TObject of object
5353
* @psalm-param class-string<TObject> $class

0 commit comments

Comments
 (0)