Skip to content

Commit e966567

Browse files
authored
Generate return type declarations (#404)
1 parent 6cca54d commit e966567

38 files changed

+774
-25
lines changed

config/blueprint.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@
8787
*/
8888
'fake_nullables' => true,
8989

90+
/*
91+
|--------------------------------------------------------------------------
92+
| Method Return Type Declarations
93+
|--------------------------------------------------------------------------
94+
|
95+
| Enable or disable method return typehinting for blueprint generated
96+
| methods. Enabling this will enforce code strictness which increases
97+
| readability of code and will lower maintenance cost. This will only
98+
| Work for projects running PHP v7.0 or higher.
99+
|
100+
*/
101+
'use_return_types' => false,
102+
90103
/*
91104
|--------------------------------------------------------------------------
92105
| Use Guarded

src/Blueprint.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public static function appPath()
3030
return str_replace('\\', '/', config('blueprint.app_path'));
3131
}
3232

33+
public static function supportsReturnTypeHits()
34+
{
35+
return boolval(config('blueprint.use_return_types')) && self::isPHP7OrHigher();
36+
}
37+
38+
public static function isPHP7OrHigher()
39+
{
40+
return version_compare(PHP_VERSION, '7.0.0', '>=');
41+
}
42+
3343
public static function isLaravel8OrHigher()
3444
{
3545
return version_compare(App::version(), '8.0.0', '>=');

src/Generators/ControllerGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ protected function buildMethods(Controller $controller)
163163
$method = str_replace('{{ body }}', trim($body), $method);
164164
}
165165

166+
if (Blueprint::supportsReturnTypeHits()) {
167+
$method = str_replace('request)', 'request): \Illuminate\Http\Response', $method);
168+
}
169+
166170
$methods .= PHP_EOL.$method;
167171
}
168172

src/Generators/FactoryGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ protected function populateStub(string $stub, Model $model)
8282
], "use", $stub);
8383
}
8484

85+
if (Blueprint::supportsReturnTypeHits()) {
86+
$stub = str_replace('definition()', 'definition(): array', $stub);
87+
}
88+
8589
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);
8690

8791
return $stub;

src/Generators/MigrationGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Blueprint;
56
use Blueprint\Contracts\Generator;
67
use Blueprint\Models\Model;
78
use Blueprint\Tree;
@@ -106,6 +107,10 @@ protected function populateStub(string $stub, Model $model)
106107
$stub = str_replace('{{ table }}', $model->tableName(), $stub);
107108
$stub = str_replace('{{ definition }}', $this->buildDefinition($model), $stub);
108109

110+
if (Blueprint::supportsReturnTypeHits()) {
111+
$stub = str_replace(['up()','down()'], ['up(): void','down(): void'], $stub);
112+
}
113+
109114
if ($this->hasForeignKeyConstraints) {
110115
$stub = $this->disableForeignKeyConstraints($stub);
111116
}

src/Generators/ModelGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ protected function buildRelationships(Model $model)
162162
}
163163

164164
foreach ($model->relationships() as $type => $references) {
165+
$custom_template = $template;
165166
foreach ($references as $reference) {
166167
$key = null;
167168
$class = null;
@@ -207,7 +208,14 @@ protected function buildRelationships(Model $model)
207208
} elseif (in_array($type, ['hasMany', 'belongsToMany', 'morphMany'])) {
208209
$method_name = Str::plural($column_name);
209210
}
210-
$method = str_replace('{{ method }}', Str::camel($method_name), $template);
211+
if (Blueprint::supportsReturnTypeHits()) {
212+
$custom_template = str_replace(
213+
'{{ method }}()',
214+
'{{ method }}(): ' . Str::of('\Illuminate\Database\Eloquent\Relations\\')->append(Str::studly($type)),
215+
$custom_template
216+
);
217+
}
218+
$method = str_replace('{{ method }}', Str::camel($method_name), $custom_template);
211219
$method = str_replace('null', $relationship, $method);
212220

213221
$phpDoc = str_replace('{{ namespacedReturnClass }}', '\Illuminate\Database\Eloquent\Relations\\'.Str::ucfirst($type), $commentTemplate);

src/Generators/SeederGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ protected function populateStub(string $stub, string $model)
6363
} else {
6464
$stub = str_replace('{{ body }}', $this->build($model), $stub);
6565
}
66+
67+
if (Blueprint::supportsReturnTypeHits()) {
68+
$stub = str_replace('public function run()', 'public function run(): void', $stub);
69+
}
70+
6671
return $stub;
6772
}
6873

src/Generators/StatementGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Blueprint\Generators;
44

5+
use Blueprint\Blueprint;
56
use Blueprint\Contracts\Generator;
67

78
abstract class StatementGenerator implements Generator

src/Generators/Statements/FormRequestGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ protected function populateStub(string $stub, string $name, $context, ValidateSt
8181
$stub = str_replace('{{ class }}', $name, $stub);
8282
$stub = str_replace('{{ rules }}', $this->buildRules($context, $validateStatement), $stub);
8383

84+
if (Blueprint::supportsReturnTypeHits()) {
85+
$stub = str_replace(['authorize()','rules()'], ['authorize(): bool','rules(): array'], $stub);
86+
}
87+
8488
return $stub;
8589
}
8690

src/Generators/Statements/JobGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ protected function populateStub(string $stub, DispatchStatement $dispatchStateme
6161
$stub = str_replace('{{ class }}', $dispatchStatement->job(), $stub);
6262
$stub = str_replace('{{ properties }}', $this->buildConstructor($dispatchStatement), $stub);
6363

64+
if (Blueprint::supportsReturnTypeHits()) {
65+
$stub = str_replace('public function handle()', 'public function handle(): void', $stub);
66+
}
67+
6468
return $stub;
6569
}
6670
}

0 commit comments

Comments
 (0)