Skip to content

Commit f65b0a4

Browse files
bug #51071 [VarExporter] Fix calling scope detection inside magic accessors (vtsykun)
This PR was submitted for the 6.3 branch but it was merged into the 6.2 branch instead. Discussion ---------- [VarExporter] Fix calling scope detection inside magic accessors | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | - | Tickets | Fix #51048 | License | MIT | Doc PR | - This PR provides fixes related to detection of class scope for which the magic method was called. this fixes are related to the issue described in this RFC https://wiki.php.net/rfc/access_scope_from_magic_accessors more accurate STR for the bug: ```php class A1Class { private $prop1; public function __construct($prop1) { $this->prop1 = $prop1; } public function getProp1() { return $this->prop1; } } class B1Class extends A1Class { protected $prop1; protected $prop2; public function __construct($prop1) { parent::__construct($prop1); $this->prop1 = $prop1; } public function test() { return $this->prop1; } public function test2() { return $this->prop2; } public function setProp2($prop2) { $this->prop2 = $prop2; } } ``` ```yml App\B1Class: arguments: [ 'test1' ] calls: - [ setProp2, [ 'test2' ] ] lazy: true ``` Call `$this->b1Class->test2();` Actual result: ![Selection_1645](https://github.com/symfony/symfony/assets/21358010/db71f9c3-cfec-4f1e-b638-e10b9384fef9) Commits ------- 41e3615 [VarDumper] Fix calling scope detection inside magic accessors
2 parents 1307d87 + 41e3615 commit f65b0a4

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static function getScope($propertyScopes, $class, $property, $readonlySco
141141
if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
142142
$scope = $frame['object']->class;
143143
}
144-
if (null === $readonlyScope && '*' === $k[1] && ($class === $scope || is_subclass_of($class, $scope))) {
144+
if (null === $readonlyScope && '*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
145145
return null;
146146
}
147147

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Component\VarExporter\Tests\Fixtures\LazyProxy;
13+
14+
class TestOverwritePropClass extends FinalPublicClass
15+
{
16+
public function __construct(
17+
protected string $dep,
18+
protected int $count,
19+
) {
20+
}
21+
22+
public function getDep(): string
23+
{
24+
return $this->dep;
25+
}
26+
}

src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
2020
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
2121
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestClass;
22+
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestOverwritePropClass;
2223
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestUnserializeClass;
2324
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\TestWakeupClass;
2425

@@ -194,6 +195,14 @@ public function testFinalPublicClass()
194195
$this->assertSame(1, $proxy->decrement());
195196
}
196197

198+
public function testOverwritePropClass()
199+
{
200+
$proxy = $this->createLazyProxy(TestOverwritePropClass::class, fn () => new TestOverwritePropClass('123', 5));
201+
202+
$this->assertSame('123', $proxy->getDep());
203+
$this->assertSame(1, $proxy->increment());
204+
}
205+
197206
public function testWither()
198207
{
199208
$obj = new class() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy