Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75b1157

Browse filesBrowse files
bug #31020 [VarExporter] fix exporting classes with private constructors (nicolas-grekas)
This PR was merged into the 4.2 branch. Discussion ---------- [VarExporter] fix exporting classes with private constructors | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Reported by @theofidry Commits ------- e354d54 [VarExporter] fix exporting classes with private constructors
2 parents c248646 + e354d54 commit 75b1157
Copy full SHA for 75b1157

File tree

3 files changed

+36
-2
lines changed
Filter options

3 files changed

+36
-2
lines changed

‎src/Symfony/Component/VarExporter/Internal/Registry.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Internal/Registry.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ public static function f($class)
6565

6666
public static function getClassReflector($class, $instantiableWithoutConstructor = false, $cloneable = null)
6767
{
68-
if (!\class_exists($class) && !\interface_exists($class, false) && !\trait_exists($class, false)) {
68+
if (!($isClass = \class_exists($class)) && !\interface_exists($class, false) && !\trait_exists($class, false)) {
6969
throw new ClassNotFoundException($class);
7070
}
7171
$reflector = new \ReflectionClass($class);
7272

7373
if ($instantiableWithoutConstructor) {
7474
$proto = $reflector->newInstanceWithoutConstructor();
75-
} elseif (!$reflector->isInstantiable()) {
75+
} elseif (!$isClass || $reflector->isAbstract()) {
7676
throw new NotInstantiableTypeException($class);
7777
} elseif ($reflector->name !== $class) {
7878
$reflector = self::$reflectors[$name = $reflector->name] ?? self::getClassReflector($name, $instantiableWithoutConstructor, $cloneable);
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
4+
$o = [
5+
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\PrivateConstructor'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\PrivateConstructor')),
6+
],
7+
null,
8+
[
9+
'stdClass' => [
10+
'prop' => [
11+
'bar',
12+
],
13+
],
14+
],
15+
$o[0],
16+
[]
17+
);

‎src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ public function provideExport()
197197
yield ['abstract-parent', new ConcreteClass()];
198198

199199
yield ['foo-serializable', new FooSerializable('bar')];
200+
201+
yield ['private-constructor', PrivateConstructor::create('bar')];
200202
}
201203
}
202204

@@ -250,6 +252,21 @@ private function __clone()
250252
}
251253
}
252254

255+
class PrivateConstructor
256+
{
257+
public $prop;
258+
259+
public static function create($prop): self
260+
{
261+
return new self($prop);
262+
}
263+
264+
private function __construct($prop)
265+
{
266+
$this->prop = $prop;
267+
}
268+
}
269+
253270
class MyPrivateValue
254271
{
255272
protected $prot;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.