Skip to content

Commit

Permalink
add isRelationMorph
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jun 18, 2023
1 parent 1728be1 commit df1e935
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ composer test

## Roadmap

- [ ] 90% coverage
- [ ] Generate TS types for `morphTo`
- [x] 90% coverage
- [x] Generate TS types for `morphTo`
- [ ] Use `appends` to define type for `Casts\Attribute` methods

## Changelog
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/typescriptable-laravel",
"description": "PHP package for Laravel to type Eloquent models and routes with autogenerated TypeScript, ready for Inertia with associated NPM package.",
"version": "1.3.0",
"version": "1.4.0",
"keywords": [
"kiwilan",
"laravel",
Expand Down
1 change: 1 addition & 0 deletions src/Typed/Eloquent/EloquentItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private function setProperties(): array
type: $relation->type,
typeTs: $relation->typeTs,
isRelation: true,
isRelationMorph: $relation->isMorph,
isNullable: true,
isArray: $relation->isArray,
);
Expand Down
1 change: 1 addition & 0 deletions src/Typed/Eloquent/Utils/EloquentProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
public bool $isHidden = false,
public bool $isEnum = false,
public bool $isRelation = false,
public bool $isRelationMorph = false,
public bool $isArray = false,
public bool $isAttribute = false,
public bool $isCount = false,
Expand Down
7 changes: 7 additions & 0 deletions src/Typed/Eloquent/Utils/EloquentRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public string $model,
public string $field,
public bool $isArray = false,
public bool $isMorph = false,
public ?string $type = null,
public ?string $typeTs = null,
) {
Expand Down Expand Up @@ -44,6 +45,7 @@ private static function make(ReflectionMethod $method): self
model: $method->getDeclaringClass()->getName(),
field: $method->getName(),
isArray: str_contains($method->getReturnType(), 'Many'),
isMorph: str_contains($method->getReturnType(), 'Morph'),
);
$return_line = $method->getEndLine() - 2;

Expand All @@ -62,6 +64,11 @@ private static function make(ReflectionMethod $method): self
? "{$relation->type}[]"
: $relation->type;

if ($relation->isMorph && ! $relation->type) {
$relation->type = 'mixed';
$relation->typeTs = 'any';
}

return $relation;
}
}

0 comments on commit df1e935

Please sign in to comment.