Skip to content

Commit eac4cea

Browse files
smnandrekbond
authored andcommitted
[Turbo] Modernize code (ppp, types..)
1 parent bbf0a18 commit eac4cea

File tree

8 files changed

+41
-63
lines changed

8 files changed

+41
-63
lines changed

src/Turbo/src/Attribute/Broadcast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ final class Broadcast
2929
public const ACTION_REMOVE = 'remove';
3030

3131
/**
32-
* @var mixed[]
32+
* @var array<mixed>
3333
*/
34-
public $options;
34+
public array $options;
3535

3636
/**
3737
* Options can be any option supported by the broadcaster.

src/Turbo/src/Bridge/Mercure/Broadcaster.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,13 @@ final class Broadcaster implements BroadcasterInterface
4040
*/
4141
public const TOPIC_PATTERN = 'https://symfony.com/ux-turbo/%s/%s';
4242

43-
private $name;
44-
private $hub;
43+
private ?ExpressionLanguage $expressionLanguage;
4544

46-
/** @var ExpressionLanguage|null */
47-
private $expressionLanguage;
48-
49-
public function __construct(string $name, HubInterface $hub)
50-
{
51-
$this->name = $name;
52-
$this->hub = $hub;
53-
54-
if (class_exists(ExpressionLanguage::class)) {
55-
$this->expressionLanguage = new ExpressionLanguage();
56-
}
45+
public function __construct(
46+
private string $name,
47+
private HubInterface $hub,
48+
) {
49+
$this->expressionLanguage = class_exists(ExpressionLanguage::class) ? new ExpressionLanguage() : null;
5750
}
5851

5952
public function broadcast(object $entity, string $action, array $options): void

src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@
2525
*/
2626
final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface
2727
{
28-
private HubInterface $hub;
2928
private StimulusHelper $stimulusHelper;
30-
private IdAccessor $idAccessor;
31-
32-
/**
33-
* @param $stimulus StimulusHelper
34-
*/
35-
public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtension $stimulus, IdAccessor $idAccessor)
36-
{
37-
$this->hub = $hub;
38-
$this->idAccessor = $idAccessor;
3929

30+
public function __construct(
31+
private HubInterface $hub,
32+
StimulusHelper|StimulusTwigExtension $stimulus,
33+
private IdAccessor $idAccessor,
34+
) {
4035
if ($stimulus instanceof StimulusTwigExtension) {
4136
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);
4237

4338
$stimulus = new StimulusHelper(null);
4439
}
40+
4541
/* @var StimulusHelper $stimulus */
4642
$this->stimulusHelper = $stimulus;
4743
}

src/Turbo/src/Broadcaster/IdAccessor.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818
class IdAccessor
1919
{
20-
private $propertyAccessor;
21-
private $doctrine;
20+
private ?PropertyAccessorInterface $propertyAccessor;
2221

23-
public function __construct(?PropertyAccessorInterface $propertyAccessor = null, ?ManagerRegistry $doctrine = null)
24-
{
22+
public function __construct(
23+
?PropertyAccessorInterface $propertyAccessor = null,
24+
private ?ManagerRegistry $doctrine = null,
25+
) {
2526
$this->propertyAccessor = $propertyAccessor ?? (class_exists(PropertyAccess::class) ? PropertyAccess::createPropertyAccessor() : null);
26-
$this->doctrine = $doctrine;
2727
}
2828

2929
/**
30-
* @return string[]
30+
* @return string[]|null
3131
*/
3232
public function getEntityId(object $entity): ?array
3333
{

src/Turbo/src/Broadcaster/ImuxBroadcaster.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
*/
1919
final class ImuxBroadcaster implements BroadcasterInterface
2020
{
21-
private $broadcasters;
22-
2321
/**
2422
* @param BroadcasterInterface[] $broadcasters
2523
*/
26-
public function __construct(iterable $broadcasters)
27-
{
28-
$this->broadcasters = $broadcasters;
24+
public function __construct(
25+
private iterable $broadcasters,
26+
) {
2927
}
3028

3129
public function broadcast(object $entity, string $action, array $options): void

src/Turbo/src/Broadcaster/TwigBroadcaster.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@
2121
*/
2222
final class TwigBroadcaster implements BroadcasterInterface
2323
{
24-
private $broadcaster;
25-
private $twig;
26-
private $templatePrefixes;
27-
private $idAccessor;
24+
private IdAccessor $idAccessor;
2825

2926
/**
3027
* @param array<string, string> $templatePrefixes
3128
*/
32-
public function __construct(BroadcasterInterface $broadcaster, Environment $twig, array $templatePrefixes = [], ?IdAccessor $idAccessor = null)
33-
{
34-
$this->broadcaster = $broadcaster;
35-
$this->twig = $twig;
36-
$this->templatePrefixes = $templatePrefixes;
29+
public function __construct(
30+
private BroadcasterInterface $broadcaster,
31+
private Environment $twig,
32+
private array $templatePrefixes = [],
33+
?IdAccessor $idAccessor = null,
34+
) {
3735
$this->idAccessor = $idAccessor ?? new IdAccessor();
3836
}
3937

src/Turbo/src/Doctrine/BroadcastListener.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
*/
2828
final class BroadcastListener implements ResetInterface
2929
{
30-
private $broadcaster;
31-
private $annotationReader;
32-
3330
/**
3431
* @var array<class-string, array<mixed>>
3532
*/
@@ -48,12 +45,11 @@ final class BroadcastListener implements ResetInterface
4845
*/
4946
private $removedEntities;
5047

51-
public function __construct(BroadcasterInterface $broadcaster, ?Reader $annotationReader = null)
52-
{
48+
public function __construct(
49+
private BroadcasterInterface $broadcaster,
50+
private ?Reader $annotationReader = null,
51+
) {
5352
$this->reset();
54-
55-
$this->broadcaster = $broadcaster;
56-
$this->annotationReader = $annotationReader;
5753
}
5854

5955
/**

src/Turbo/src/Twig/TwigExtension.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,26 @@
2121
*/
2222
final class TwigExtension extends AbstractExtension
2323
{
24-
private $turboStreamListenRenderers;
25-
private $default;
26-
27-
public function __construct(ContainerInterface $turboStreamListenRenderers, string $default)
28-
{
29-
$this->turboStreamListenRenderers = $turboStreamListenRenderers;
30-
$this->default = $default;
24+
public function __construct(
25+
private ContainerInterface $turboStreamListenRenderers,
26+
private string $default,
27+
) {
3128
}
3229

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

3835
/**
3936
* @param object|string $topic
4037
*/
4138
public function turboStreamListen(Environment $env, $topic, ?string $transport = null): string
4239
{
43-
$transport = $transport ?? $this->default;
40+
$transport ??= $this->default;
4441

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

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

0 commit comments

Comments
 (0)