From f7e1af2b238c60ce47e4f746359c0f7bcbbd7ad2 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Fri, 12 May 2023 09:42:28 +0200 Subject: [PATCH] Add some typehint --- controller/service.rst | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index 1510f7b8278..50ee34a1aac 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -31,6 +31,7 @@ apply the ``controller.service_arguments`` tag to your controller services:: // src/Controller/HelloController.php namespace App\Controller; + use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\Routing\Annotation\Route; @@ -38,7 +39,7 @@ apply the ``controller.service_arguments`` tag to your controller services:: class HelloController { #[Route('/hello', name: 'hello', methods: ['GET'])] - public function index() + public function index(): Response { // ... } @@ -71,7 +72,7 @@ a service like: ``App\Controller\HelloController::index``: /** * @Route("/hello", name="hello", methods={"GET"}) */ - public function index() + public function index(): Response { // ... } @@ -82,12 +83,13 @@ a service like: ``App\Controller\HelloController::index``: // src/Controller/HelloController.php namespace App\Controller; + use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class HelloController { #[Route('/hello', name: 'hello', methods: ['GET'])] - public function index() + public function index(): Response { // ... } @@ -151,7 +153,7 @@ which is a common practice when following the `ADR pattern`_ */ class Hello { - public function __invoke($name = 'World') + public function __invoke(string $name = 'World'): Response { return new Response(sprintf('Hello %s!', $name)); } @@ -168,7 +170,7 @@ which is a common practice when following the `ADR pattern`_ #[Route('/hello/{name}', name: 'hello')] class Hello { - public function __invoke($name = 'World') + public function __invoke(string $name = 'World'): Response { return new Response(sprintf('Hello %s!', $name)); } @@ -228,14 +230,14 @@ service and use it directly:: class HelloController { - private $twig; + private Environment $twig; public function __construct(Environment $twig) { $this->twig = $twig; } - public function index($name) + public function index(string $name): Response { $content = $this->twig->render( 'hello/index.html.twig',