Skip to content

Commit 8db6637

Browse files
authored
Return correct return-type hints (#591)
1 parent c1218a1 commit 8db6637

16 files changed

+178
-15
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,16 @@ protected function buildMethods(Controller $controller)
158158
if (isset($fqcn) && $name !== 'destroy' && $controller->isApiResource()) {
159159
$method = str_replace(')' . PHP_EOL, '): \\' . $fqcn . PHP_EOL, $method);
160160
} else {
161-
$method = str_replace(')' . PHP_EOL, '): \Illuminate\Http\Response' . PHP_EOL, $method);
161+
$returnType = match (true) {
162+
$statement instanceof RenderStatement => 'Illuminate\View\View',
163+
$statement instanceof RedirectStatement => 'Illuminate\Routing\Redirector',
164+
default => 'Illuminate\Http\Response'
165+
};
166+
167+
$method = Str::of($method)
168+
->replace('* @return \\Illuminate\\Http\\Response', '* @return \\' . $returnType)
169+
->replace(')' . PHP_EOL, '): \\' . $returnType . PHP_EOL)
170+
->toString();
162171
}
163172
}
164173

tests/Feature/BlueprintTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ public function generate_should_skip_specific_types()
662662

663663
/**
664664
* @test
665+
*
665666
* @dataProvider namespacesDataProvider
666667
*/
667668
public function relative_namespace_removes_namespace_prefix_from_reference($namespace, $expected, $reference)

tests/Feature/Generators/ControllerGeneratorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function output_writes_nothing_for_empty_tree()
4646

4747
/**
4848
* @test
49+
*
4950
* @dataProvider controllerTreeDataProvider
5051
*/
5152
public function output_writes_migration_for_controller_tree($definition, $path, $controller)
@@ -240,4 +241,31 @@ public function controllerTreeDataProvider()
240241
['drafts/invokable-controller-shorthand.yaml', 'app/Http/Controllers/ReportController.php', 'controllers/invokable-controller-shorthand.php'],
241242
];
242243
}
244+
245+
public function testOutputGeneratesControllersWithTypehints(): void
246+
{
247+
$definition = 'drafts/controller-returns-view-typehint.yaml';
248+
$path = 'app/Http/Controllers/UserController.php';
249+
$controller = 'controllers/controller-returns-view-typehint.php';
250+
251+
$this->app['config']->set('blueprint.use_return_types', true);
252+
253+
$this->filesystem->expects('stub')
254+
->with('controller.class.stub')
255+
->andReturn($this->stub('controller.class.stub'));
256+
$this->filesystem->expects('stub')
257+
->with('controller.method.stub')
258+
->andReturn($this->stub('controller.method.stub'));
259+
260+
$this->filesystem->expects('exists')
261+
->with(dirname($path))
262+
->andReturnTrue();
263+
$this->filesystem->expects('put')
264+
->with($path, $this->fixture($controller));
265+
266+
$tokens = $this->blueprint->parse($this->fixture($definition));
267+
$tree = $this->blueprint->analyze($tokens);
268+
269+
self::assertSame(['created' => [$path]], $this->subject->output($tree));
270+
}
243271
}

tests/Feature/Generators/FactoryGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function output_writes_nothing_for_empty_tree()
4747

4848
/**
4949
* @test
50+
*
5051
* @dataProvider modelTreeDataProvider
5152
*/
5253
public function output_writes_factory_for_model_tree($definition, $path, $factory)

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function output_writes_nothing_for_empty_tree()
4848

4949
/**
5050
* @test
51+
*
5152
* @dataProvider modelTreeDataProvider
5253
*/
5354
public function output_writes_migration_for_model_tree($definition, $path, $migration)
@@ -80,6 +81,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
8081

8182
/**
8283
* @test
84+
*
8385
* @dataProvider modelTreeDataProvider
8486
*/
8587
public function output_updates_migration_for_model_tree($definition, $path, $migration)

tests/Feature/Generators/ModelGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function output_generates_nothing_for_empty_tree()
4343

4444
/**
4545
* @test
46+
*
4647
* @dataProvider modelTreeDataProvider
4748
*/
4849
public function output_generates_models($definition, $path, $model)
@@ -393,6 +394,7 @@ public function output_respects_configuration()
393394

394395
/**
395396
* @test
397+
*
396398
* @dataProvider docBlockModelsDataProvider
397399
*/
398400
public function output_generates_phpdoc_for_model($definition, $path, $model)

tests/Feature/Generators/RouteGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function output_generates_nothing_for_empty_tree()
4343

4444
/**
4545
* @test
46+
*
4647
* @dataProvider controllerTreeDataProvider
4748
*/
4849
public function output_generates_web_routes($definition, $routes)

tests/Feature/Generators/Statements/NotificationGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function output_writes_nothing_tree_without_validate_statements()
6464

6565
/**
6666
* @test
67+
*
6768
* @dataProvider notificationDraftProvider
6869
*/
6970
public function output_writes_notifications($draft)

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function output_writes_nothing_for_empty_tree()
4646

4747
/**
4848
* @test
49+
*
4950
* @dataProvider controllerTreeDataProvider
5051
*/
5152
public function output_generates_test_for_controller_tree_l8($definition, $path, $test)

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public function it_accepts_lowercase_keywords()
269269

270270
/**
271271
* @test
272+
*
272273
* @dataProvider dataTypeAttributesDataProvider
273274
*/
274275
public function it_handles_data_type_attributes($definition, $data_type, $attributes)
@@ -304,6 +305,7 @@ public function it_handles_data_type_attributes($definition, $data_type, $attrib
304305

305306
/**
306307
* @test
308+
*
307309
* @dataProvider modifierAttributesProvider
308310
*/
309311
public function it_handles_modifier_attributes($definition, $modifier, $attributes)

0 commit comments

Comments
 (0)