Skip to content

Commit 4d3c8e8

Browse files
committed
fix for fetching live props from parent class
1 parent 9817abd commit 4d3c8e8

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/LiveComponent/src/Attribute/AsLiveComponent.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ final class AsLiveComponent extends AsTwigComponent
2828
*/
2929
public static function liveProps(object $component): \Traversable
3030
{
31+
$properties = [];
32+
3133
foreach (self::propertiesFor($component) as $property) {
32-
if ($attribute = $property->getAttributes(LiveProp::class)[0] ?? null) {
33-
yield new LivePropContext($attribute->newInstance(), $property);
34+
if (!$attribute = $property->getAttributes(LiveProp::class)[0] ?? null) {
35+
continue;
36+
}
37+
38+
if (\in_array($property->getName(), $properties, true)) {
39+
// property name was already used
40+
continue;
3441
}
42+
43+
$properties[] = $property->getName();
44+
45+
yield new LivePropContext($attribute->newInstance(), $property);
3546
}
3647
}
3748

src/LiveComponent/tests/Fixture/Component/Component4.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
/**
2121
* @author Kevin Bond <[email protected]>
2222
*/
23-
final class Component4
23+
class Component4
2424
{
2525
#[LiveProp]
2626
public $prop1;
2727

2828
public $prop2;
2929

3030
#[LiveProp]
31-
public $prop3;
31+
private $prop3;
3232

3333
#[LiveAction]
3434
public function method1()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\LiveComponent\Tests\Fixture\Component;
13+
14+
/**
15+
* @author Kevin Bond <[email protected]>
16+
*/
17+
final class Component5 extends Component4
18+
{
19+
}

src/LiveComponent/tests/Unit/Attribute/AsLiveComponentTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
16-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component4;
16+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component5;
1717

1818
/**
1919
* @author Kevin Bond <[email protected]>
@@ -22,7 +22,7 @@ final class AsLiveComponentTest extends TestCase
2222
{
2323
public function testCanGetLiveProps(): void
2424
{
25-
$props = iterator_to_array(AsLiveComponent::liveProps(new Component4()));
25+
$props = iterator_to_array(AsLiveComponent::liveProps(new Component5()));
2626

2727
$this->assertCount(2, $props);
2828
$this->assertSame('prop1', $props[0]->reflectionProperty()->getName());
@@ -31,31 +31,31 @@ public function testCanGetLiveProps(): void
3131

3232
public function testCanGetPreDehydrateMethods(): void
3333
{
34-
$methods = iterator_to_array(AsLiveComponent::preDehydrateMethods(new Component4()));
34+
$methods = iterator_to_array(AsLiveComponent::preDehydrateMethods(new Component5()));
3535

3636
$this->assertCount(1, $methods);
3737
$this->assertSame('method4', $methods[0]->getName());
3838
}
3939

4040
public function testCanGetPostHydrateMethods(): void
4141
{
42-
$methods = iterator_to_array(AsLiveComponent::postHydrateMethods(new Component4()));
42+
$methods = iterator_to_array(AsLiveComponent::postHydrateMethods(new Component5()));
4343

4444
$this->assertCount(1, $methods);
4545
$this->assertSame('method5', $methods[0]->getName());
4646
}
4747

4848
public function testCanGetBeforeReRenderMethods(): void
4949
{
50-
$methods = iterator_to_array(AsLiveComponent::beforeReRenderMethods(new Component4()));
50+
$methods = iterator_to_array(AsLiveComponent::beforeReRenderMethods(new Component5()));
5151

5252
$this->assertCount(1, $methods);
5353
$this->assertSame('method3', $methods[0]->getName());
5454
}
5555

5656
public function testCanCheckIfMethodIsAllowed(): void
5757
{
58-
$component = new Component4();
58+
$component = new Component5();
5959

6060
$this->assertTrue(AsLiveComponent::isActionAllowed($component, 'method1'));
6161
$this->assertFalse(AsLiveComponent::isActionAllowed($component, 'method2'));

0 commit comments

Comments
 (0)