Skip to content

Commit

Permalink
minor #1931 [Turbo] Modernize code (ppp, types..) (smnandre)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Turbo] Modernize code (ppp, types..)

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  |no
| Issues        | Fix #...
| License       | MIT

Commits
-------

eac4cea [Turbo] Modernize code (ppp, types..)
  • Loading branch information
kbond committed Jun 26, 2024
2 parents b6e4d16 + eac4cea commit 3e2b89a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/Turbo/src/Attribute/Broadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ final class Broadcast
public const ACTION_REMOVE = 'remove';

/**
* @var mixed[]
* @var array<mixed>
*/
public $options;
public array $options;

/**
* Options can be any option supported by the broadcaster.
Expand Down
19 changes: 6 additions & 13 deletions src/Turbo/src/Bridge/Mercure/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,13 @@ final class Broadcaster implements BroadcasterInterface
*/
public const TOPIC_PATTERN = 'https://symfony.com/ux-turbo/%s/%s';

private $name;
private $hub;
private ?ExpressionLanguage $expressionLanguage;

/** @var ExpressionLanguage|null */
private $expressionLanguage;

public function __construct(string $name, HubInterface $hub)
{
$this->name = $name;
$this->hub = $hub;

if (class_exists(ExpressionLanguage::class)) {
$this->expressionLanguage = new ExpressionLanguage();
}
public function __construct(
private string $name,
private HubInterface $hub,
) {
$this->expressionLanguage = class_exists(ExpressionLanguage::class) ? new ExpressionLanguage() : null;
}

public function broadcast(object $entity, string $action, array $options): void
Expand Down
16 changes: 6 additions & 10 deletions src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@
*/
final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface
{
private HubInterface $hub;
private StimulusHelper $stimulusHelper;
private IdAccessor $idAccessor;

/**
* @param $stimulus StimulusHelper
*/
public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtension $stimulus, IdAccessor $idAccessor)
{
$this->hub = $hub;
$this->idAccessor = $idAccessor;

public function __construct(
private HubInterface $hub,
StimulusHelper|StimulusTwigExtension $stimulus,
private IdAccessor $idAccessor,
) {
if ($stimulus instanceof StimulusTwigExtension) {
trigger_deprecation('symfony/ux-turbo', '2.9', 'Passing an instance of "%s" as second argument of "%s" is deprecated, pass an instance of "%s" instead.', StimulusTwigExtension::class, __CLASS__, StimulusHelper::class);

$stimulus = new StimulusHelper(null);
}

/* @var StimulusHelper $stimulus */
$this->stimulusHelper = $stimulus;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Turbo/src/Broadcaster/IdAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

class IdAccessor
{
private $propertyAccessor;
private $doctrine;
private ?PropertyAccessorInterface $propertyAccessor;

public function __construct(?PropertyAccessorInterface $propertyAccessor = null, ?ManagerRegistry $doctrine = null)
{
public function __construct(
?PropertyAccessorInterface $propertyAccessor = null,
private ?ManagerRegistry $doctrine = null,
) {
$this->propertyAccessor = $propertyAccessor ?? (class_exists(PropertyAccess::class) ? PropertyAccess::createPropertyAccessor() : null);
$this->doctrine = $doctrine;
}

/**
* @return string[]
* @return string[]|null
*/
public function getEntityId(object $entity): ?array
{
Expand Down
8 changes: 3 additions & 5 deletions src/Turbo/src/Broadcaster/ImuxBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
*/
final class ImuxBroadcaster implements BroadcasterInterface
{
private $broadcasters;

/**
* @param BroadcasterInterface[] $broadcasters
*/
public function __construct(iterable $broadcasters)
{
$this->broadcasters = $broadcasters;
public function __construct(
private iterable $broadcasters,
) {
}

public function broadcast(object $entity, string $action, array $options): void
Expand Down
16 changes: 7 additions & 9 deletions src/Turbo/src/Broadcaster/TwigBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
*/
final class TwigBroadcaster implements BroadcasterInterface
{
private $broadcaster;
private $twig;
private $templatePrefixes;
private $idAccessor;
private IdAccessor $idAccessor;

/**
* @param array<string, string> $templatePrefixes
*/
public function __construct(BroadcasterInterface $broadcaster, Environment $twig, array $templatePrefixes = [], ?IdAccessor $idAccessor = null)
{
$this->broadcaster = $broadcaster;
$this->twig = $twig;
$this->templatePrefixes = $templatePrefixes;
public function __construct(
private BroadcasterInterface $broadcaster,
private Environment $twig,
private array $templatePrefixes = [],
?IdAccessor $idAccessor = null,
) {
$this->idAccessor = $idAccessor ?? new IdAccessor();
}

Expand Down
12 changes: 4 additions & 8 deletions src/Turbo/src/Doctrine/BroadcastListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
*/
final class BroadcastListener implements ResetInterface
{
private $broadcaster;
private $annotationReader;

/**
* @var array<class-string, array<mixed>>
*/
Expand All @@ -48,12 +45,11 @@ final class BroadcastListener implements ResetInterface
*/
private $removedEntities;

public function __construct(BroadcasterInterface $broadcaster, ?Reader $annotationReader = null)
{
public function __construct(
private BroadcasterInterface $broadcaster,
private ?Reader $annotationReader = null,
) {
$this->reset();

$this->broadcaster = $broadcaster;
$this->annotationReader = $annotationReader;
}

/**
Expand Down
17 changes: 7 additions & 10 deletions src/Turbo/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,26 @@
*/
final class TwigExtension extends AbstractExtension
{
private $turboStreamListenRenderers;
private $default;

public function __construct(ContainerInterface $turboStreamListenRenderers, string $default)
{
$this->turboStreamListenRenderers = $turboStreamListenRenderers;
$this->default = $default;
public function __construct(
private ContainerInterface $turboStreamListenRenderers,
private string $default,
) {
}

public function getFunctions(): iterable
{
yield new TwigFunction('turbo_stream_listen', [$this, 'turboStreamListen'], ['needs_environment' => true, 'is_safe' => ['html']]);
yield new TwigFunction('turbo_stream_listen', $this->turboStreamListen(...), ['needs_environment' => true, 'is_safe' => ['html']]);
}

/**
* @param object|string $topic
*/
public function turboStreamListen(Environment $env, $topic, ?string $transport = null): string
{
$transport = $transport ?? $this->default;
$transport ??= $this->default;

if (!$this->turboStreamListenRenderers->has($transport)) {
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" doesn\'t exist.', $transport));
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport));
}

return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic);
Expand Down

0 comments on commit 3e2b89a

Please sign in to comment.