Skip to content

Commit afd1aa5

Browse files
claudeondrejmirtes
authored andcommitted
Implement __set_state() for all AST node classes
Add __set_state() magic method to all 68 AST node classes to enable var_export() serialization for caching purposes. Implementation details: - All classes now support reconstruction via var_export() - Constructor parameters are properly extracted from properties array - Attributes are restored after instance creation - Optional parameters handled with null coalescing operator Classes updated: - Type nodes: 20 classes (ArrayTypeNode, GenericTypeNode, etc.) - ConstExpr nodes: 10 classes (ConstExprArrayNode, ConstFetchNode, etc.) - PhpDoc core: 4 classes (PhpDocNode, PhpDocTagNode, etc.) - PhpDoc tag values: 29 classes (ParamTagValueNode, ReturnTagValueNode, etc.) - Doctrine nodes: 5 classes (DoctrineAnnotation, DoctrineArray, etc.) All tests pass (3711 tests, 19858 assertions) PHPStan analysis passes with no errors
1 parent fc79f19 commit afd1aa5

File tree

67 files changed

+940
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+940
-0
lines changed

src/Ast/ConstExpr/ConstExprArrayItemNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public function __toString(): string
3030
return (string) $this->value;
3131
}
3232

33+
/**
34+
* @param array<string, mixed> $properties
35+
*/
36+
public static function __set_state(array $properties): self
37+
{
38+
$instance = new self($properties['key'], $properties['value']);
39+
if (isset($properties['attributes'])) {
40+
foreach ($properties['attributes'] as $key => $value) {
41+
$instance->setAttribute($key, $value);
42+
}
43+
}
44+
return $instance;
45+
}
46+
3347
}

src/Ast/ConstExpr/ConstExprArrayNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ public function __toString(): string
2626
return '[' . implode(', ', $this->items) . ']';
2727
}
2828

29+
/**
30+
* @param array<string, mixed> $properties
31+
*/
32+
public static function __set_state(array $properties): self
33+
{
34+
$instance = new self($properties['items']);
35+
if (isset($properties['attributes'])) {
36+
foreach ($properties['attributes'] as $key => $value) {
37+
$instance->setAttribute($key, $value);
38+
}
39+
}
40+
return $instance;
41+
}
42+
2943
}

src/Ast/ConstExpr/ConstExprFalseNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ public function __toString(): string
1414
return 'false';
1515
}
1616

17+
/**
18+
* @param array<string, mixed> $properties
19+
*/
20+
public static function __set_state(array $properties): self
21+
{
22+
$instance = new self();
23+
if (isset($properties['attributes'])) {
24+
foreach ($properties['attributes'] as $key => $value) {
25+
$instance->setAttribute($key, $value);
26+
}
27+
}
28+
return $instance;
29+
}
30+
1731
}

src/Ast/ConstExpr/ConstExprFloatNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@ public function __toString(): string
2121
return $this->value;
2222
}
2323

24+
/**
25+
* @param array<string, mixed> $properties
26+
*/
27+
public static function __set_state(array $properties): self
28+
{
29+
$instance = new self($properties['value']);
30+
if (isset($properties['attributes'])) {
31+
foreach ($properties['attributes'] as $key => $value) {
32+
$instance->setAttribute($key, $value);
33+
}
34+
}
35+
return $instance;
36+
}
37+
2438
}

src/Ast/ConstExpr/ConstExprIntegerNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@ public function __toString(): string
2121
return $this->value;
2222
}
2323

24+
/**
25+
* @param array<string, mixed> $properties
26+
*/
27+
public static function __set_state(array $properties): self
28+
{
29+
$instance = new self($properties['value']);
30+
if (isset($properties['attributes'])) {
31+
foreach ($properties['attributes'] as $key => $value) {
32+
$instance->setAttribute($key, $value);
33+
}
34+
}
35+
return $instance;
36+
}
37+
2438
}

src/Ast/ConstExpr/ConstExprNullNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ public function __toString(): string
1414
return 'null';
1515
}
1616

17+
/**
18+
* @param array<string, mixed> $properties
19+
*/
20+
public static function __set_state(array $properties): self
21+
{
22+
$instance = new self();
23+
if (isset($properties['attributes'])) {
24+
foreach ($properties['attributes'] as $key => $value) {
25+
$instance->setAttribute($key, $value);
26+
}
27+
}
28+
return $instance;
29+
}
30+
1731
}

src/Ast/ConstExpr/ConstExprStringNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,18 @@ private function escapeDoubleQuotedString(): string
7676
}, $escaped);
7777
}
7878

79+
/**
80+
* @param array<string, mixed> $properties
81+
*/
82+
public static function __set_state(array $properties): self
83+
{
84+
$instance = new self($properties['value'], $properties['quoteType']);
85+
if (isset($properties['attributes'])) {
86+
foreach ($properties['attributes'] as $key => $value) {
87+
$instance->setAttribute($key, $value);
88+
}
89+
}
90+
return $instance;
91+
}
92+
7993
}

src/Ast/ConstExpr/ConstExprTrueNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ public function __toString(): string
1414
return 'true';
1515
}
1616

17+
/**
18+
* @param array<string, mixed> $properties
19+
*/
20+
public static function __set_state(array $properties): self
21+
{
22+
$instance = new self();
23+
if (isset($properties['attributes'])) {
24+
foreach ($properties['attributes'] as $key => $value) {
25+
$instance->setAttribute($key, $value);
26+
}
27+
}
28+
return $instance;
29+
}
30+
1731
}

src/Ast/ConstExpr/ConstFetchNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public function __toString(): string
3030
return "{$this->className}::{$this->name}";
3131
}
3232

33+
/**
34+
* @param array<string, mixed> $properties
35+
*/
36+
public static function __set_state(array $properties): self
37+
{
38+
$instance = new self($properties['className'], $properties['name']);
39+
if (isset($properties['attributes'])) {
40+
foreach ($properties['attributes'] as $key => $value) {
41+
$instance->setAttribute($key, $value);
42+
}
43+
}
44+
return $instance;
45+
}
46+
3347
}

src/Ast/ConstExpr/DoctrineConstExprStringNode.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ private static function escape(string $value): string
3838
return sprintf('"%s"', str_replace('"', '""', $value));
3939
}
4040

41+
/**
42+
* @param array<string, mixed> $properties
43+
*/
44+
public static function __set_state(array $properties): self
45+
{
46+
$instance = new self($properties['value']);
47+
if (isset($properties['attributes'])) {
48+
foreach ($properties['attributes'] as $key => $value) {
49+
$instance->setAttribute($key, $value);
50+
}
51+
}
52+
return $instance;
53+
}
54+
4155
}

0 commit comments

Comments
 (0)