Skip to content

Commit 0f706aa

Browse files
Add option to generate return type declarations for relation methods
1 parent e439995 commit 0f706aa

10 files changed

+88
-5
lines changed

config/models.php

+12
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@
421421
|
422422
*/
423423
'fillable_in_base_files' => false,
424+
425+
/*
426+
|--------------------------------------------------------------------------
427+
| Generate return types for relation methods.
428+
|--------------------------------------------------------------------------
429+
| When enable_return_types is set to true, return type declarations are added
430+
| to all generated relation methods for your models.
431+
|
432+
| NOTE: This requires PHP 7.0 or later.
433+
|
434+
*/
435+
'enable_return_types' => false,
424436
],
425437

426438
/*

src/Coders/Model/Factory.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Reliese\Coders\Model;
99

10+
use Illuminate\Database\DatabaseManager;
11+
use Illuminate\Filesystem\Filesystem;
1012
use Illuminate\Support\Str;
1113
use Reliese\Meta\Blueprint;
12-
use Reliese\Support\Classify;
1314
use Reliese\Meta\SchemaManager;
14-
use Illuminate\Filesystem\Filesystem;
15-
use Illuminate\Database\DatabaseManager;
15+
use Reliese\Support\Classify;
1616

1717
class Factory
1818
{
@@ -480,7 +480,14 @@ protected function body(Model $model)
480480
}
481481

482482
foreach ($model->getRelations() as $constraint) {
483-
$body .= $this->class->method($constraint->name(), $constraint->body(), ['before' => "\n"]);
483+
$body .= $this->class->method(
484+
$constraint->name(),
485+
$constraint->body(),
486+
[
487+
'before' => "\n",
488+
'returnType' => $model->definesReturnTypes() ? $constraint->returnType() : null,
489+
]
490+
);
484491
}
485492

486493
// Make sure there not undesired line breaks

src/Coders/Model/Model.php

+15
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ class Model
162162
*/
163163
protected $relationNameStrategy = '';
164164

165+
/**
166+
* @var bool
167+
*/
168+
protected $definesReturnTypes = false;
169+
165170
/**
166171
* ModelClass constructor.
167172
*
@@ -210,6 +215,8 @@ protected function configure()
210215
// Relation name settings
211216
$this->withRelationNameStrategy($this->config('relation_name_strategy', $this->getDefaultRelationNameStrategy()));
212217

218+
$this->definesReturnTypes = $this->config('enable_return_types', false);
219+
213220
return $this;
214221
}
215222

@@ -1249,4 +1256,12 @@ public function fillableInBaseFiles(): bool
12491256
{
12501257
return $this->config('fillable_in_base_files', false);
12511258
}
1259+
1260+
/**
1261+
* @return bool
1262+
*/
1263+
public function definesReturnTypes()
1264+
{
1265+
return $this->definesReturnTypes;
1266+
}
12521267
}

src/Coders/Model/Relation.php

+5
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public function name();
2323
* @return string
2424
*/
2525
public function body();
26+
27+
/**
28+
* @return string
29+
*/
30+
public function returnType();
2631
}

src/Coders/Model/Relations/BelongsTo.php

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ public function hint()
127127
return $base;
128128
}
129129

130+
/**
131+
* @return string
132+
*/
133+
public function returnType()
134+
{
135+
return \Illuminate\Database\Eloquent\Relations\BelongsTo::class;
136+
}
137+
130138
/**
131139
* @return bool
132140
*/

src/Coders/Model/Relations/BelongsToMany.php

+8
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ public function body()
136136
return $body;
137137
}
138138

139+
/**
140+
* @return string
141+
*/
142+
public function returnType()
143+
{
144+
return \Illuminate\Database\Eloquent\Relations\BelongsToMany::class;
145+
}
146+
139147
/**
140148
* @return bool
141149
*/

src/Coders/Model/Relations/HasMany.php

+8
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ public function method()
5858
{
5959
return 'hasMany';
6060
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function returnType()
66+
{
67+
return \Illuminate\Database\Eloquent\Relations\HasMany::class;
68+
}
6169
}

src/Coders/Model/Relations/HasOne.php

+8
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ public function method()
3838
{
3939
return 'hasOne';
4040
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function returnType()
46+
{
47+
return \Illuminate\Database\Eloquent\Relations\HasOne::class;
48+
}
4149
}

src/Coders/Model/Relations/HasOneOrManyStrategy.php

+10
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ public function body()
6060
{
6161
return $this->relation->body();
6262
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function returnType()
68+
{
69+
return get_class($this->relation) === HasMany::class ?
70+
\Illuminate\Database\Eloquent\Relations\HasMany::class :
71+
\Illuminate\Database\Eloquent\Relations\HasOne::class;
72+
}
6373
}

src/Support/Classify.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ public function field($name, $value, $options = [])
6767
public function method($name, $body, $options = [])
6868
{
6969
$visibility = Arr::get($options, 'visibility', 'public');
70+
$returnType = Arr::get($options, 'returnType', null);
71+
$formattedReturnType = $returnType ? ': '.$returnType : '';
7072

71-
return "\n\t$visibility function $name()\n\t{\n\t\t$body\n\t}\n";
73+
return "\n\t$visibility function $name()$formattedReturnType\n\t{\n\t\t$body\n\t}\n";
7274
}
7375

7476
public function mixin($class)

0 commit comments

Comments
 (0)