Skip to content

Commit 8f8621d

Browse files
committed
Added PHP types to the DI related articles
1 parent aec1016 commit 8f8621d

12 files changed

+45
-40
lines changed

service_container.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::
340340

341341
// src/Controller/SiteController.php
342342
namespace App\Controller;
343-
343+
344344
// ...
345345
use App\Service\SiteUpdateManager;
346346

@@ -378,7 +378,7 @@ example, suppose you want to make the admin email configurable:
378378
+ private $adminEmail;
379379
380380
- public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
381-
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, $adminEmail)
381+
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, string $adminEmail)
382382
{
383383
// ...
384384
+ $this->adminEmail = $adminEmail;

service_container/autowiring.rst

+11-8
Original file line numberDiff line numberDiff line change
@@ -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

294297
Then, 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
// ...

service_container/calls.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ example::
2222
{
2323
private $logger;
2424

25-
public function setLogger(LoggerInterface $logger)
25+
public function setLogger(LoggerInterface $logger): void
2626
{
2727
$this->logger = $logger;
2828
}
@@ -97,7 +97,7 @@ instead of mutating the object they were called on::
9797
/**
9898
* @return static
9999
*/
100-
public function withLogger(LoggerInterface $logger)
100+
public function withLogger(LoggerInterface $logger): self
101101
{
102102
$new = clone $this;
103103
$new->logger = $logger;

service_container/compiler_passes.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ and process the services inside the ``process()`` method::
5252

5353
// ...
5454

55-
public function process(ContainerBuilder $container)
55+
public function process(ContainerBuilder $container): void
5656
{
5757
// in this method you can manipulate the service container:
5858
// for example, changing some container service:
@@ -81,7 +81,7 @@ method in the extension)::
8181

8282
class MyBundle extends Bundle
8383
{
84-
public function build(ContainerBuilder $container)
84+
public function build(ContainerBuilder $container): void
8585
{
8686
parent::build($container);
8787

service_container/configurators.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ You start defining a ``NewsletterManager`` class like this::
2828
{
2929
private $enabledFormatters;
3030

31-
public function setEnabledFormatters(array $enabledFormatters)
31+
public function setEnabledFormatters(array $enabledFormatters): void
3232
{
3333
$this->enabledFormatters = $enabledFormatters;
3434
}
@@ -45,7 +45,7 @@ and also a ``GreetingCardManager`` class::
4545
{
4646
private $enabledFormatters;
4747

48-
public function setEnabledFormatters(array $enabledFormatters)
48+
public function setEnabledFormatters(array $enabledFormatters): void
4949
{
5050
$this->enabledFormatters = $enabledFormatters;
5151
}
@@ -65,7 +65,7 @@ in the application::
6565
{
6666
// ...
6767

68-
public function getEnabledFormatters()
68+
public function getEnabledFormatters(): array
6969
{
7070
// code to configure which formatters to use
7171
$enabledFormatters = [...];
@@ -92,7 +92,7 @@ to create a configurator class to configure these instances::
9292
$this->formatterManager = $formatterManager;
9393
}
9494

95-
public function configure(EmailFormatterAwareInterface $emailManager)
95+
public function configure(EmailFormatterAwareInterface $emailManager): void
9696
{
9797
$emailManager->setEnabledFormatters(
9898
$this->formatterManager->getEnabledFormatters()

service_container/factories.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object by calling the static ``createNewsletterManager()`` method::
2626

2727
class NewsletterManagerStaticFactory
2828
{
29-
public static function createNewsletterManager()
29+
public static function createNewsletterManager(): NewsletterManager
3030
{
3131
$newsletterManager = new NewsletterManager();
3232

@@ -180,7 +180,7 @@ factory service can be used as a callback::
180180
// ...
181181
class InvokableNewsletterManagerFactory
182182
{
183-
public function __invoke()
183+
public function __invoke(): NewsletterManager
184184
{
185185
$newsletterManager = new NewsletterManager();
186186

service_container/injection_types.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ by cloning the original service, this approach allows you to make a service immu
130130
* @required
131131
* @return static
132132
*/
133-
public function withMailer(MailerInterface $mailer)
133+
public function withMailer(MailerInterface $mailer): self
134134
{
135135
$new = clone $this;
136136
$new->mailer = $mailer;
@@ -224,7 +224,7 @@ that accepts the dependency::
224224

225225
// src/Mail/NewsletterManager.php
226226
namespace App\Mail;
227-
227+
228228
// ...
229229
class NewsletterManager
230230
{
@@ -233,7 +233,7 @@ that accepts the dependency::
233233
/**
234234
* @required
235235
*/
236-
public function setMailer(MailerInterface $mailer)
236+
public function setMailer(MailerInterface $mailer): void
237237
{
238238
$this->mailer = $mailer;
239239
}

service_container/optional_dependencies.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ In YAML, the special ``@?`` syntax tells the service container that the
113113
dependency is optional. The ``NewsletterManager`` must also be rewritten by
114114
adding a ``setLogger()`` method::
115115

116-
public function setLogger(LoggerInterface $logger)
116+
public function setLogger(LoggerInterface $logger): void
117117
{
118118
// ...
119119
}

service_container/parent_services.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ you may have multiple repository classes which need the
2626
$this->objectManager = $objectManager;
2727
}
2828

29-
public function setLogger(LoggerInterface $logger)
29+
public function setLogger(LoggerInterface $logger): void
3030
{
3131
$this->logger = $logger;
3232
}

service_container/service_subscribers_locators.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ a PSR-11 ``ContainerInterface``::
8787
$this->locator = $locator;
8888
}
8989

90-
public static function getSubscribedServices()
90+
public static function getSubscribedServices(): array
9191
{
9292
return [
9393
'App\FooCommand' => FooHandler::class,
@@ -130,7 +130,7 @@ service locator::
130130

131131
use Psr\Log\LoggerInterface;
132132

133-
public static function getSubscribedServices()
133+
public static function getSubscribedServices(): array
134134
{
135135
return [
136136
// ...
@@ -142,7 +142,7 @@ Service types can also be keyed by a service name for internal use::
142142

143143
use Psr\Log\LoggerInterface;
144144

145-
public static function getSubscribedServices()
145+
public static function getSubscribedServices(): array
146146
{
147147
return [
148148
// ...
@@ -159,7 +159,7 @@ typically happens when extending ``AbstractController``::
159159

160160
class MyController extends AbstractController
161161
{
162-
public static function getSubscribedServices()
162+
public static function getSubscribedServices(): array
163163
{
164164
return array_merge(parent::getSubscribedServices(), [
165165
// ...
@@ -176,7 +176,7 @@ errors if there's no matching service found in the service container::
176176

177177
use Psr\Log\LoggerInterface;
178178

179-
public static function getSubscribedServices()
179+
public static function getSubscribedServices(): array
180180
{
181181
return [
182182
// ...
@@ -395,7 +395,7 @@ will share identical locators among all the services referencing them::
395395
use Symfony\Component\DependencyInjection\ContainerBuilder;
396396
use Symfony\Component\DependencyInjection\Reference;
397397

398-
public function process(ContainerBuilder $container)
398+
public function process(ContainerBuilder $container): void
399399
{
400400
// ...
401401

service_container/synthetic_services.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ from within the ``Kernel`` class::
1818
{
1919
// ...
2020

21-
protected function initializeContainer()
21+
protected function initializeContainer(): void
2222
{
2323
// ...
2424
$this->container->set('kernel', $this);

service_container/tags.rst

+10-8
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ In a Symfony application, call this method in your kernel class::
126126
{
127127
// ...
128128

129-
protected function build(ContainerBuilder $container)
129+
protected function build(ContainerBuilder $container): void
130130
{
131131
$container->registerForAutoconfiguration(CustomInterface::class)
132132
->addTag('app.custom_tag')
@@ -142,7 +142,7 @@ In a Symfony bundle, call this method in the ``load()`` method of the
142142
{
143143
// ...
144144

145-
public function load(array $configs, ContainerBuilder $container)
145+
public function load(array $configs, ContainerBuilder $container): void
146146
{
147147
$container->registerForAutoconfiguration(CustomInterface::class)
148148
->addTag('app.custom_tag')
@@ -178,7 +178,7 @@ To begin with, define the ``TransportChain`` class::
178178
$this->transports = [];
179179
}
180180

181-
public function addTransport(\Swift_Transport $transport)
181+
public function addTransport(\Swift_Transport $transport): void
182182
{
183183
$this->transports[] = $transport;
184184
}
@@ -304,7 +304,7 @@ container for any services with the ``app.mail_transport`` tag::
304304

305305
class MailTransportPass implements CompilerPassInterface
306306
{
307-
public function process(ContainerBuilder $container)
307+
public function process(ContainerBuilder $container): void
308308
{
309309
// always first check if the primary service is defined
310310
if (!$container->has(TransportChain::class)) {
@@ -341,7 +341,7 @@ or from your kernel::
341341
{
342342
// ...
343343

344-
protected function build(ContainerBuilder $container)
344+
protected function build(ContainerBuilder $container): void
345345
{
346346
$container->addCompilerPass(new MailTransportPass());
347347
}
@@ -372,16 +372,18 @@ To begin with, change the ``TransportChain`` class::
372372
$this->transports = [];
373373
}
374374

375-
public function addTransport(\Swift_Transport $transport, $alias)
375+
public function addTransport(\Swift_Transport $transport, $alias): void
376376
{
377377
$this->transports[$alias] = $transport;
378378
}
379379

380-
public function getTransport($alias)
380+
public function getTransport($alias): ?\Swift_Transport
381381
{
382382
if (array_key_exists($alias, $this->transports)) {
383383
return $this->transports[$alias];
384384
}
385+
386+
return null;
385387
}
386388
}
387389

@@ -476,7 +478,7 @@ use this, update the compiler::
476478

477479
class TransportCompilerPass implements CompilerPassInterface
478480
{
479-
public function process(ContainerBuilder $container)
481+
public function process(ContainerBuilder $container): void
480482
{
481483
// ...
482484

0 commit comments

Comments
 (0)