Skip to content

Commit 67b814b

Browse files
browner12taylorotwell
authored andcommitted
remove factory "types" (#30867)
- simplify the global helper `factory()`. since the signature is no longer dynamic, we can be a little more explicit in our parameters - remove `defineAs`, `createAs`, `makeAs`, and `rawOf` which were methods specific to factory "types" - update the `FactoryBuilder` to remove references to the "name". we do keep one reference to the name "default" because that is how the callbacks are registered.
1 parent a4d6ec6 commit 67b814b

File tree

3 files changed

+18
-85
lines changed

3 files changed

+18
-85
lines changed

src/Illuminate/Database/Eloquent/Factory.php

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,16 @@ public static function construct(Faker $faker, $pathToFactories = null)
6868
return (new static($faker))->load($pathToFactories);
6969
}
7070

71-
/**
72-
* Define a class with a given short-name.
73-
*
74-
* @param string $class
75-
* @param string $name
76-
* @param callable $attributes
77-
* @return $this
78-
*/
79-
public function defineAs($class, $name, callable $attributes)
80-
{
81-
return $this->define($class, $attributes, $name);
82-
}
83-
8471
/**
8572
* Define a class with a given set of attributes.
8673
*
8774
* @param string $class
8875
* @param callable $attributes
89-
* @param string $name
9076
* @return $this
9177
*/
92-
public function define($class, callable $attributes, $name = 'default')
78+
public function define($class, callable $attributes)
9379
{
94-
$this->definitions[$class][$name] = $attributes;
80+
$this->definitions[$class] = $attributes;
9581

9682
return $this;
9783
}
@@ -179,19 +165,6 @@ public function create($class, array $attributes = [])
179165
return $this->of($class)->create($attributes);
180166
}
181167

182-
/**
183-
* Create an instance of the given model and type and persist it to the database.
184-
*
185-
* @param string $class
186-
* @param string $name
187-
* @param array $attributes
188-
* @return mixed
189-
*/
190-
public function createAs($class, $name, array $attributes = [])
191-
{
192-
return $this->of($class, $name)->create($attributes);
193-
}
194-
195168
/**
196169
* Create an instance of the given model.
197170
*
@@ -204,58 +177,30 @@ public function make($class, array $attributes = [])
204177
return $this->of($class)->make($attributes);
205178
}
206179

207-
/**
208-
* Create an instance of the given model and type.
209-
*
210-
* @param string $class
211-
* @param string $name
212-
* @param array $attributes
213-
* @return mixed
214-
*/
215-
public function makeAs($class, $name, array $attributes = [])
216-
{
217-
return $this->of($class, $name)->make($attributes);
218-
}
219-
220-
/**
221-
* Get the raw attribute array for a given named model.
222-
*
223-
* @param string $class
224-
* @param string $name
225-
* @param array $attributes
226-
* @return array
227-
*/
228-
public function rawOf($class, $name, array $attributes = [])
229-
{
230-
return $this->raw($class, $attributes, $name);
231-
}
232-
233180
/**
234181
* Get the raw attribute array for a given model.
235182
*
236183
* @param string $class
237184
* @param array $attributes
238-
* @param string $name
239185
* @return array
240186
*/
241-
public function raw($class, array $attributes = [], $name = 'default')
187+
public function raw($class, array $attributes = [])
242188
{
243189
return array_merge(
244-
call_user_func($this->definitions[$class][$name], $this->faker), $attributes
190+
call_user_func($this->definitions[$class], $this->faker), $attributes
245191
);
246192
}
247193

248194
/**
249195
* Create a builder for the given model.
250196
*
251197
* @param string $class
252-
* @param string $name
253198
* @return \Illuminate\Database\Eloquent\FactoryBuilder
254199
*/
255-
public function of($class, $name = 'default')
200+
public function of($class)
256201
{
257202
return new FactoryBuilder(
258-
$class, $name, $this->definitions, $this->states,
203+
$class, $this->definitions, $this->states,
259204
$this->afterMaking, $this->afterCreating, $this->faker
260205
);
261206
}

src/Illuminate/Database/Eloquent/FactoryBuilder.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ class FactoryBuilder
2424
*/
2525
protected $class;
2626

27-
/**
28-
* The name of the model being built.
29-
*
30-
* @var string
31-
*/
32-
protected $name = 'default';
33-
3427
/**
3528
* The database connection on which the model instance should be persisted.
3629
*
@@ -84,18 +77,16 @@ class FactoryBuilder
8477
* Create an new builder instance.
8578
*
8679
* @param string $class
87-
* @param string $name
8880
* @param array $definitions
8981
* @param array $states
9082
* @param array $afterMaking
9183
* @param array $afterCreating
9284
* @param \Faker\Generator $faker
9385
* @return void
9486
*/
95-
public function __construct($class, $name, array $definitions, array $states,
87+
public function __construct($class, array $definitions, array $states,
9688
array $afterMaking, array $afterCreating, Faker $faker)
9789
{
98-
$this->name = $name;
9990
$this->class = $class;
10091
$this->faker = $faker;
10192
$this->states = $states;
@@ -265,12 +256,12 @@ public function raw(array $attributes = [])
265256
*/
266257
protected function getRawAttributes(array $attributes = [])
267258
{
268-
if (! isset($this->definitions[$this->class][$this->name])) {
269-
throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}] [{$this->class}].");
259+
if (! isset($this->definitions[$this->class])) {
260+
throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
270261
}
271262

272263
$definition = call_user_func(
273-
$this->definitions[$this->class][$this->name],
264+
$this->definitions[$this->class],
274265
$this->faker, $attributes
275266
);
276267

@@ -403,7 +394,7 @@ public function callAfterCreating($models)
403394
*/
404395
protected function callAfter(array $afterCallbacks, $models)
405396
{
406-
$states = array_merge([$this->name], $this->activeStates);
397+
$states = array_merge(['default'], $this->activeStates);
407398

408399
$models->each(function ($model) use ($states, $afterCallbacks) {
409400
foreach ($states as $state) {

src/Illuminate/Foundation/helpers.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -484,24 +484,21 @@ function event(...$args)
484484

485485
if (! function_exists('factory')) {
486486
/**
487-
* Create a model factory builder for a given class, name, and amount.
487+
* Create a model factory builder for a given class and amount.
488488
*
489-
* @param dynamic class|class,name|class,amount|class,name,amount
489+
* @param string $class
490+
* @param int $amount
490491
* @return \Illuminate\Database\Eloquent\FactoryBuilder
491492
*/
492-
function factory()
493+
function factory($class, $amount = null)
493494
{
494495
$factory = app(EloquentFactory::class);
495496

496-
$arguments = func_get_args();
497-
498-
if (isset($arguments[1]) && is_string($arguments[1])) {
499-
return $factory->of($arguments[0], $arguments[1])->times($arguments[2] ?? null);
500-
} elseif (isset($arguments[1])) {
501-
return $factory->of($arguments[0])->times($arguments[1]);
497+
if (isset($amount) && is_int($amount)) {
498+
return $factory->of($class)->times($amount);
502499
}
503500

504-
return $factory->of($arguments[0]);
501+
return $factory->of($class);
505502
}
506503
}
507504

0 commit comments

Comments
 (0)