@@ -29,7 +29,7 @@ Start by creating a ROT13 transformer class::
2929
3030 class Rot13Transformer
3131 {
32- public function transform($value)
32+ public function transform(string $value): string
3333 {
3434 return str_rot13($value);
3535 }
@@ -41,6 +41,7 @@ And now a Twitter client using this transformer::
4141 namespace App\Service;
4242
4343 use App\Util\Rot13Transformer;
44+ // ...
4445
4546 class TwitterClient
4647 {
@@ -51,7 +52,7 @@ And now a Twitter client using this transformer::
5152 $this->transformer = $transformer;
5253 }
5354
54- public function tweet($user, $key, $status)
55+ public function tweet(User $user, string $key, string $status): void
5556 {
5657 $transformedStatus = $this->transformer->transform($status);
5758
@@ -129,14 +130,16 @@ Now, you can use the ``TwitterClient`` service immediately in a controller::
129130
130131 use App\Service\TwitterClient;
131132 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
133+ use Symfony\Component\HttpFoundation\Request;
134+ use Symfony\Component\HttpFoundation\Response;
132135 use Symfony\Component\Routing\Annotation\Route;
133136
134137 class DefaultController extends AbstractController
135138 {
136139 /**
137140 * @Route("/tweet", methods={"POST"})
138141 */
139- public function tweet(TwitterClient $twitterClient)
142+ public function tweet(TwitterClient $twitterClient, Request $request): Response
140143 {
141144 // fetch $user, $key, $status from the POST'ed data
142145
@@ -288,7 +291,7 @@ To follow this best practice, suppose you decide to create a ``TransformerInterf
288291
289292 interface TransformerInterface
290293 {
291- public function transform($value);
294+ public function transform(string $value): string ;
292295 }
293296
294297Then, you update ``Rot13Transformer `` to implement it::
@@ -388,7 +391,7 @@ Suppose you create a second class - ``UppercaseTransformer`` that implements
388391
389392 class UppercaseTransformer implements TransformerInterface
390393 {
391- public function transform($value)
394+ public function transform(string $value): string
392395 {
393396 return strtoupper($value);
394397 }
@@ -426,7 +429,7 @@ the injection::
426429 $this->transformer = $shoutyTransformer;
427430 }
428431
429- public function toot($user, $key, $status)
432+ public function toot(User $user, string $key, string $status): void
430433 {
431434 $transformedStatus = $this->transformer->transform($status);
432435
@@ -565,12 +568,12 @@ to inject the ``logger`` service, and decide to use setter-injection::
565568 /**
566569 * @required
567570 */
568- public function setLogger(LoggerInterface $logger)
571+ public function setLogger(LoggerInterface $logger): void
569572 {
570573 $this->logger = $logger;
571574 }
572575
573- public function transform($value)
576+ public function transform(string $value): string
574577 {
575578 $this->logger->info('Transforming '.$value);
576579 // ...
0 commit comments