Skip to content

Commit 4fdb0e6

Browse files
committed
Streamline creating related model factories
1 parent 843eceb commit 4fdb0e6

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
3636
'username' => $faker->userName,
3737
'email' => $faker->safeEmail,
3838
'password' => bcrypt($faker->password),
39-
'company_id' => function () {
40-
return factory(App\Company::class)->create()->id;
41-
},
39+
'company_id' => factory(App\Company::class),
4240
'remember_token' => Str::random(10),
4341
];
4442
});

src/Console/GenerateCommand.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ protected function getPropertiesFromMethods($model)
259259
$methods = get_class_methods($model);
260260

261261
foreach ($methods as $method) {
262-
if (!method_exists('Illuminate\Database\Eloquent\Model', $method) && !Str::startsWith($method, 'get')) {
263-
//Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
262+
if (!Str::startsWith($method, 'get') && !method_exists('Illuminate\Database\Eloquent\Model', $method)) {
263+
// Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php
264264
$reflection = new \ReflectionMethod($model, $method);
265265
$file = new \SplFileObject($reflection->getFileName());
266266
$file->seek($reflection->getStartLine() - 1);
@@ -272,22 +272,13 @@ protected function getPropertiesFromMethods($model)
272272
$code = trim(preg_replace('/\s\s+/', '', $code));
273273
$begin = strpos($code, 'function(');
274274
$code = substr($code, $begin, strrpos($code, '}') - $begin + 1);
275-
foreach (array(
276-
'belongsTo',
277-
) as $relation) {
275+
foreach (['belongsTo'] as $relation) {
278276
$search = '$this->' . $relation . '(';
279277
if ($pos = stripos($code, $search)) {
280-
//Resolve the relation's model to a Relation object.
281278
$relationObj = $model->$method();
282279
if ($relationObj instanceof Relation) {
283-
$relatedModel = '\\' . get_class($relationObj->getRelated());
284-
$relatedObj = new $relatedModel;
285-
$property = method_exists($relationObj, 'getForeignKeyName')
286-
? $relationObj->getForeignKeyName()
287-
: $relationObj->getForeignKey();
288-
$this->setProperty($property, 'function () {
289-
return factory(' . get_class($relationObj->getRelated()) . '::class)->create()->' . $relatedObj->getKeyName() . ';
290-
}');
280+
echo 'found method: ', $method, PHP_EOL;
281+
$this->setProperty($relationObj->getForeignKeyName(), 'factory(' . get_class($relationObj->getRelated()) . '::class)');
291282
}
292283
}
293284
}
@@ -301,7 +292,7 @@ protected function getPropertiesFromMethods($model)
301292
*/
302293
protected function setProperty($name, $type = null, $table = null)
303294
{
304-
if ($type !== null && Str::startsWith($type, 'function (')) {
295+
if ($type !== null && Str::startsWith($type, 'factory(')) {
305296
$this->properties[$name] = $type;
306297

307298
return;

0 commit comments

Comments
 (0)