Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FactoryGenerator: No need to import fqcn foreign keys. #642

Closed
wants to merge 1 commit into from
Closed

FactoryGenerator: No need to import fqcn foreign keys. #642

wants to merge 1 commit into from

Conversation

Jehong-Ahn
Copy link

No need use App\Models\\Foo\Bar; for foreign:\Foo\Bar.

@jasonmccreary
Copy link
Collaborator

I wonder what affect this has on other areas of the code: migrations, controllers, etc.

Copy link
Contributor

@ghostwriter ghostwriter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Jehong-Ahn ,

We appreciate the time you invested in preparing this pull request.

I will be reviewing this patch now.

Thank you for your contribution.

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use App\Models\Post;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import use My\User; fully-qualified class name.

{
return [
'title' => $this->faker->sentence(4),
'author_id' => \My\User::factory(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify \My\User::factory() fully-qualified class name to User::factory().

if (!Str::startsWith($class, '\\')) {
$reference = $this->fullyQualifyModelReference($class) ?? $model;
$this->addImport($model, $reference->fullyQualifiedNamespace() . '\\' . $class);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.


I believe you want to remove the application's namespace prefix for fully-qualified class names that start with '\'.

eg. for foreign:\Foo\Bar

- use App\Models\\Foo\Bar;
+ use Foo\Bar;

To achieve this we need to change:

$class = Str::studly(Str::singular($table));
$reference = $this->fullyQualifyModelReference($class) ?? $model;

- $this->addImport($model, $reference->fullyQualifiedNamespace() . '\\' . $class);
+ if (Str::startsWith($class, '\\')) {
+     $this->addImport($model, $class);
+ } else {
+     $this->addImport($model, $reference->fullyQualifiedNamespace() . '\\' . $class);
+ }

This should correctly import all fully-qualified class names.

@Jehong-Ahn
Copy link
Author

Thanks, @ghostwriter. I've learned a lot from your review.

@jasonmccreary
Copy link
Collaborator

What's the status of this PR. Seems conversations are not resolved.

@Jehong-Ahn Jehong-Ahn closed this by deleting the head repository Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants