Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 4, 2024
1 parent 0184045 commit e5e8659
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/ErdFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class ErdFinder

private string $directory;

public function __construct(SchemaBuilder $schemaBuilder, ModelFinder $modelFinder)
public function __construct(SchemaBuilder $schemaBuilder, ModelFinder $modelFinder, RelationFinder $relationFinder)
{
$this->schemaBuilder = $schemaBuilder;
$this->modelFinder = $modelFinder;
$this->relationFinder = $relationFinder;
}

public function in(string $directory): ErdFinder
Expand Down Expand Up @@ -77,7 +78,7 @@ private function findByModels(Collection $models, array $excludes = []): Collect
{
$models = $this->mergeMissing($models);
$relations = $models
->flatMap(fn (string $model) => RelationFinder::generate($model)->collapse())
->flatMap(fn (string $model) => $this->relationFinder->generate($model)->collapse())
->flatMap(fn (Relation $relation) => [$relation, $relation->relatedRelation()]);

$relationGroupByConnection = $relations
Expand Down Expand Up @@ -123,7 +124,7 @@ private function findByModels(Collection $models, array $excludes = []): Collect
private function mergeMissing(Collection $models): Collection
{
return $models->merge($models
->flatMap(fn (string $model) => RelationFinder::generate($model)->collapse())
->flatMap(fn (string $model) => $this->relationFinder->generate($model)->collapse())
->map(fn (Relation $relation) => $relation->related())
->filter()
->diff($models));
Expand Down
4 changes: 3 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function create(?string $name = null): ErdFinder
}

return $this->cache[$key] = new ErdFinder(
$this->getSchemaBuilder($name), new ModelFinder($name)
$this->getSchemaBuilder($name),
new ModelFinder($name),
new RelationFinder()
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/RelationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class RelationFinder
*
* @throws ReflectionException
*/
public static function generate(string $className): Collection
public function generate(string $className): Collection
{
$class = new ReflectionClass($className);
$model = new $className;
Expand Down
2 changes: 1 addition & 1 deletion tests/RelationFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public function test_draw_user_role_permission_relations(): void
*/
private function givenRelations(string $model): Collection
{
return RelationFinder::generate($model);
return (new RelationFinder)->generate($model);
}

private function draw(Collection $relations, $method): array
Expand Down

0 comments on commit e5e8659

Please sign in to comment.