Skip to content

Commit 47ba656

Browse files
committed
Fix PHPStan
1 parent 59f44bd commit 47ba656

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Mercure\HubInterface;
1515
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1616
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
17+
use Symfony\UX\Turbo\Broadcaster\IdFormatter;
1718
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface;
1819
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
1920
use Twig\Environment;
@@ -28,14 +29,16 @@ final class TurboStreamListenRenderer implements TurboStreamListenRendererInterf
2829
private HubInterface $hub;
2930
private StimulusHelper $stimulusHelper;
3031
private IdAccessor $idAccessor;
32+
private IdFormatter $idFormatter;
3133

3234
/**
3335
* @param $stimulus StimulusHelper
3436
*/
35-
public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtension $stimulus, IdAccessor $idAccessor)
37+
public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtension $stimulus, IdAccessor $idAccessor, ?IdFormatter $idFormatter = null)
3638
{
3739
$this->hub = $hub;
3840
$this->idAccessor = $idAccessor;
41+
$this->idFormatter = $idFormatter ?? new IdFormatter();
3942

4043
if ($stimulus instanceof StimulusTwigExtension) {
4144
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);
@@ -55,7 +58,9 @@ public function renderTurboStreamListen(Environment $env, $topic): string
5558
throw new \LogicException(sprintf('Cannot listen to entity of class "%s" as the PropertyAccess component is not installed. Try running "composer require symfony/property-access".', $class));
5659
}
5760

58-
$topic = sprintf(Broadcaster::TOPIC_PATTERN, rawurlencode($class), rawurlencode(implode('-', $id)));
61+
$formattedId = $this->idFormatter->format($id);
62+
63+
$topic = sprintf(Broadcaster::TOPIC_PATTERN, rawurlencode($class), rawurlencode($formattedId));
5964
} elseif (!preg_match('/[^a-zA-Z0-9_\x7f-\xff\\\\]/', $topic) && class_exists($topic)) {
6065
// Generate a URI template to subscribe to updates for all objects of this class
6166
$topic = sprintf(Broadcaster::TOPIC_PATTERN, rawurlencode($topic), '{id}');

src/Turbo/src/Broadcaster/BroadcasterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
interface BroadcasterInterface
2020
{
2121
/**
22-
* @param array{id?: string|string[], transports?: string|string[], topics?: string|string[], template?: string, rendered_action?: string, private?: bool, sse_id?: string, sse_type?: string, sse_retry?: int} $options
22+
* @param array{id?: array<string, string>|array<string, array<string, string>>, transports?: string|string[], topics?: string|string[], template?: string, rendered_action?: string, private?: bool, sse_id?: string, sse_type?: string, sse_retry?: int} $options
2323
*/
2424
public function broadcast(object $entity, string $action, array $options): void;
2525
}

src/Turbo/src/Broadcaster/DoctrineIdAccessor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public function __construct(?ManagerRegistry $doctrine = null)
2727
$this->doctrine = $doctrine;
2828
}
2929

30+
/**
31+
* @return array<string, array<string, string>>|array<string, string>|null
32+
*/
3033
public function getEntityId(object $entity): ?array
3134
{
3235
$entityClass = $entity::class;
@@ -38,6 +41,9 @@ public function getEntityId(object $entity): ?array
3841
return null;
3942
}
4043

44+
/**
45+
* @return array<string, string>|array<string, array<string, string>>
46+
*/
4147
private function getIdentifierValues(ObjectManager $em, object $entity): array
4248
{
4349
$class = ClassUtil::getEntityClass($entity);

src/Turbo/src/Broadcaster/IdAccessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(?PropertyAccessorInterface $propertyAccessor = null,
2626
}
2727

2828
/**
29-
* @return string[]
29+
* @return array<string, array<string, string>>|array<string, string>|null
3030
*/
3131
public function getEntityId(object $entity): ?array
3232
{

src/Turbo/src/Broadcaster/IdFormatter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@
2323
*/
2424
class IdFormatter
2525
{
26-
public function format(array $id): string
26+
/**
27+
* @param array<string, array<string, string>>|array<string, string>|string $id
28+
*/
29+
public function format(array|string $id): string
2730
{
31+
if (is_string($id)) {
32+
return $id;
33+
}
34+
2835
$flatten = [];
2936

3037
array_walk_recursive($id, static function ($item) use (&$flatten) { $flatten[] = $item; });

src/Turbo/tests/app/Kernel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,12 @@ public function cartProducts(Request $request, EntityManagerInterface $doctrine,
345345

346346
if ($remove = $request->get('remove')) {
347347
$doctrine->remove($cartProduct);
348-
$doctrine->remove($cartProduct->product); // for cleanup
349-
$doctrine->remove($cartProduct->cart); // for cleanup
348+
if ($cartProduct->product) {
349+
$doctrine->remove($cartProduct->product); // for cleanup
350+
}
351+
if ($cartProduct->cart) {
352+
$doctrine->remove($cartProduct->cart); // for cleanup
353+
}
350354
} else {
351355
$doctrine->persist($cartProduct);
352356
}

0 commit comments

Comments
 (0)