@@ -29,7 +29,7 @@ Start by creating a ROT13 transformer class::
29
29
30
30
class Rot13Transformer
31
31
{
32
- public function transform($value)
32
+ public function transform(string $value): string
33
33
{
34
34
return str_rot13($value);
35
35
}
@@ -41,6 +41,7 @@ And now a Twitter client using this transformer::
41
41
namespace App\Service;
42
42
43
43
use App\Util\Rot13Transformer;
44
+ // ...
44
45
45
46
class TwitterClient
46
47
{
@@ -51,7 +52,7 @@ And now a Twitter client using this transformer::
51
52
$this->transformer = $transformer;
52
53
}
53
54
54
- public function tweet($user, $key, $status)
55
+ public function tweet(User $user, string $key, string $status): void
55
56
{
56
57
$transformedStatus = $this->transformer->transform($status);
57
58
@@ -129,14 +130,16 @@ Now, you can use the ``TwitterClient`` service immediately in a controller::
129
130
130
131
use App\Service\TwitterClient;
131
132
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
133
+ use Symfony\Component\HttpFoundation\Request;
134
+ use Symfony\Component\HttpFoundation\Response;
132
135
use Symfony\Component\Routing\Annotation\Route;
133
136
134
137
class DefaultController extends AbstractController
135
138
{
136
139
/**
137
140
* @Route("/tweet", methods={"POST"})
138
141
*/
139
- public function tweet(TwitterClient $twitterClient)
142
+ public function tweet(TwitterClient $twitterClient, Request $request): Response
140
143
{
141
144
// fetch $user, $key, $status from the POST'ed data
142
145
@@ -288,7 +291,7 @@ To follow this best practice, suppose you decide to create a ``TransformerInterf
288
291
289
292
interface TransformerInterface
290
293
{
291
- public function transform($value);
294
+ public function transform(string $value): string ;
292
295
}
293
296
294
297
Then, you update ``Rot13Transformer `` to implement it::
@@ -388,7 +391,7 @@ Suppose you create a second class - ``UppercaseTransformer`` that implements
388
391
389
392
class UppercaseTransformer implements TransformerInterface
390
393
{
391
- public function transform($value)
394
+ public function transform(string $value): string
392
395
{
393
396
return strtoupper($value);
394
397
}
@@ -426,7 +429,7 @@ the injection::
426
429
$this->transformer = $shoutyTransformer;
427
430
}
428
431
429
- public function toot($user, $key, $status)
432
+ public function toot(User $user, string $key, string $status): void
430
433
{
431
434
$transformedStatus = $this->transformer->transform($status);
432
435
@@ -565,12 +568,12 @@ to inject the ``logger`` service, and decide to use setter-injection::
565
568
/**
566
569
* @required
567
570
*/
568
- public function setLogger(LoggerInterface $logger)
571
+ public function setLogger(LoggerInterface $logger): void
569
572
{
570
573
$this->logger = $logger;
571
574
}
572
575
573
- public function transform($value)
576
+ public function transform(string $value): string
574
577
{
575
578
$this->logger->info('Transforming '.$value);
576
579
// ...
0 commit comments