Skip to content

Commit

Permalink
Added PHP types to the DI related articles
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 21, 2020
1 parent aec1016 commit 8f8621d
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::

// src/Controller/SiteController.php
namespace App\Controller;

// ...
use App\Service\SiteUpdateManager;

Expand Down Expand Up @@ -378,7 +378,7 @@ example, suppose you want to make the admin email configurable:
+ private $adminEmail;
- public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, $adminEmail)
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, string $adminEmail)
{
// ...
+ $this->adminEmail = $adminEmail;
Expand Down
19 changes: 11 additions & 8 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Start by creating a ROT13 transformer class::

class Rot13Transformer
{
public function transform($value)
public function transform(string $value): string
{
return str_rot13($value);
}
Expand All @@ -41,6 +41,7 @@ And now a Twitter client using this transformer::
namespace App\Service;

use App\Util\Rot13Transformer;
// ...

class TwitterClient
{
Expand All @@ -51,7 +52,7 @@ And now a Twitter client using this transformer::
$this->transformer = $transformer;
}

public function tweet($user, $key, $status)
public function tweet(User $user, string $key, string $status): void
{
$transformedStatus = $this->transformer->transform($status);

Expand Down Expand Up @@ -129,14 +130,16 @@ Now, you can use the ``TwitterClient`` service immediately in a controller::

use App\Service\TwitterClient;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
/**
* @Route("/tweet", methods={"POST"})
*/
public function tweet(TwitterClient $twitterClient)
public function tweet(TwitterClient $twitterClient, Request $request): Response
{
// fetch $user, $key, $status from the POST'ed data

Expand Down Expand Up @@ -288,7 +291,7 @@ To follow this best practice, suppose you decide to create a ``TransformerInterf

interface TransformerInterface
{
public function transform($value);
public function transform(string $value): string;
}

Then, you update ``Rot13Transformer`` to implement it::
Expand Down Expand Up @@ -388,7 +391,7 @@ Suppose you create a second class - ``UppercaseTransformer`` that implements

class UppercaseTransformer implements TransformerInterface
{
public function transform($value)
public function transform(string $value): string
{
return strtoupper($value);
}
Expand Down Expand Up @@ -426,7 +429,7 @@ the injection::
$this->transformer = $shoutyTransformer;
}

public function toot($user, $key, $status)
public function toot(User $user, string $key, string $status): void
{
$transformedStatus = $this->transformer->transform($status);

Expand Down Expand Up @@ -565,12 +568,12 @@ to inject the ``logger`` service, and decide to use setter-injection::
/**
* @required
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

public function transform($value)
public function transform(string $value): string
{
$this->logger->info('Transforming '.$value);
// ...
Expand Down
4 changes: 2 additions & 2 deletions service_container/calls.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ example::
{
private $logger;

public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ instead of mutating the object they were called on::
/**
* @return static
*/
public function withLogger(LoggerInterface $logger)
public function withLogger(LoggerInterface $logger): self
{
$new = clone $this;
$new->logger = $logger;
Expand Down
4 changes: 2 additions & 2 deletions service_container/compiler_passes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ and process the services inside the ``process()`` method::

// ...

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
// in this method you can manipulate the service container:
// for example, changing some container service:
Expand Down Expand Up @@ -81,7 +81,7 @@ method in the extension)::

class MyBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);

Expand Down
8 changes: 4 additions & 4 deletions service_container/configurators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ You start defining a ``NewsletterManager`` class like this::
{
private $enabledFormatters;

public function setEnabledFormatters(array $enabledFormatters)
public function setEnabledFormatters(array $enabledFormatters): void
{
$this->enabledFormatters = $enabledFormatters;
}
Expand All @@ -45,7 +45,7 @@ and also a ``GreetingCardManager`` class::
{
private $enabledFormatters;

public function setEnabledFormatters(array $enabledFormatters)
public function setEnabledFormatters(array $enabledFormatters): void
{
$this->enabledFormatters = $enabledFormatters;
}
Expand All @@ -65,7 +65,7 @@ in the application::
{
// ...

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

public function configure(EmailFormatterAwareInterface $emailManager)
public function configure(EmailFormatterAwareInterface $emailManager): void
{
$emailManager->setEnabledFormatters(
$this->formatterManager->getEnabledFormatters()
Expand Down
4 changes: 2 additions & 2 deletions service_container/factories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object by calling the static ``createNewsletterManager()`` method::

class NewsletterManagerStaticFactory
{
public static function createNewsletterManager()
public static function createNewsletterManager(): NewsletterManager
{
$newsletterManager = new NewsletterManager();

Expand Down Expand Up @@ -180,7 +180,7 @@ factory service can be used as a callback::
// ...
class InvokableNewsletterManagerFactory
{
public function __invoke()
public function __invoke(): NewsletterManager
{
$newsletterManager = new NewsletterManager();

Expand Down
6 changes: 3 additions & 3 deletions service_container/injection_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ by cloning the original service, this approach allows you to make a service immu
* @required
* @return static
*/
public function withMailer(MailerInterface $mailer)
public function withMailer(MailerInterface $mailer): self
{
$new = clone $this;
$new->mailer = $mailer;
Expand Down Expand Up @@ -224,7 +224,7 @@ that accepts the dependency::

// src/Mail/NewsletterManager.php
namespace App\Mail;

// ...
class NewsletterManager
{
Expand All @@ -233,7 +233,7 @@ that accepts the dependency::
/**
* @required
*/
public function setMailer(MailerInterface $mailer)
public function setMailer(MailerInterface $mailer): void
{
$this->mailer = $mailer;
}
Expand Down
2 changes: 1 addition & 1 deletion service_container/optional_dependencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ In YAML, the special ``@?`` syntax tells the service container that the
dependency is optional. The ``NewsletterManager`` must also be rewritten by
adding a ``setLogger()`` method::

public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
// ...
}
2 changes: 1 addition & 1 deletion service_container/parent_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ you may have multiple repository classes which need the
$this->objectManager = $objectManager;
}

public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
Expand Down
12 changes: 6 additions & 6 deletions service_container/service_subscribers_locators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ a PSR-11 ``ContainerInterface``::
$this->locator = $locator;
}

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [
'App\FooCommand' => FooHandler::class,
Expand Down Expand Up @@ -130,7 +130,7 @@ service locator::

use Psr\Log\LoggerInterface;

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

use Psr\Log\LoggerInterface;

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [
// ...
Expand All @@ -159,7 +159,7 @@ typically happens when extending ``AbstractController``::

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

use Psr\Log\LoggerInterface;

public static function getSubscribedServices()
public static function getSubscribedServices(): array
{
return [
// ...
Expand Down Expand Up @@ -395,7 +395,7 @@ will share identical locators among all the services referencing them::
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
// ...

Expand Down
2 changes: 1 addition & 1 deletion service_container/synthetic_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from within the ``Kernel`` class::
{
// ...

protected function initializeContainer()
protected function initializeContainer(): void
{
// ...
$this->container->set('kernel', $this);
Expand Down
18 changes: 10 additions & 8 deletions service_container/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ In a Symfony application, call this method in your kernel class::
{
// ...

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

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

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

class MailTransportPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
// always first check if the primary service is defined
if (!$container->has(TransportChain::class)) {
Expand Down Expand Up @@ -341,7 +341,7 @@ or from your kernel::
{
// ...

protected function build(ContainerBuilder $container)
protected function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new MailTransportPass());
}
Expand Down Expand Up @@ -372,16 +372,18 @@ To begin with, change the ``TransportChain`` class::
$this->transports = [];
}

public function addTransport(\Swift_Transport $transport, $alias)
public function addTransport(\Swift_Transport $transport, $alias): void
{
$this->transports[$alias] = $transport;
}

public function getTransport($alias)
public function getTransport($alias): ?\Swift_Transport
{
if (array_key_exists($alias, $this->transports)) {
return $this->transports[$alias];
}

return null;
}
}

Expand Down Expand Up @@ -476,7 +478,7 @@ use this, update the compiler::

class TransportCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
// ...

Expand Down

0 comments on commit 8f8621d

Please sign in to comment.