Skip to content

Commit e6783b4

Browse files
committed
Factory: supports 'new' in parameters
For full support, reflection is missing in PHP.
1 parent c72e05b commit e6783b4

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

src/PhpGenerator/Factory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
225225
$parts[0] = Helpers::tagName($parts[0]);
226226
}
227227
$param->setDefaultValue(new Literal(implode('::', $parts)));
228+
} elseif (is_object($from->getDefaultValue())) {
229+
$param->setDefaultValue($this->fromObject($from->getDefaultValue()));
228230
} else {
229231
$param->setDefaultValue($from->getDefaultValue());
230232
}
@@ -285,6 +287,12 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
285287
}
286288

287289

290+
public function fromObject(object $obj): Literal
291+
{
292+
return new Literal('new ' . get_class($obj) . '(/* unknown */)');
293+
}
294+
295+
288296
public function fromClassCode(string $code): ClassType
289297
{
290298
$classes = $this->fromCode($code)->getClasses();
@@ -308,7 +316,13 @@ private function getAttributes($from): array
308316
return [];
309317
}
310318
return array_map(function ($attr) {
311-
return new Attribute($attr->getName(), $attr->getArguments());
319+
$args = $attr->getArguments();
320+
foreach ($args as &$arg) {
321+
if (is_object($arg)) {
322+
$arg = $this->fromObject($arg);
323+
}
324+
}
325+
return new Attribute($attr->getName(), $args);
312326
}, $from->getAttributes());
313327
}
314328

tests/PhpGenerator/Extractor.extractAll.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ sameFile(__DIR__ . '/expected/Factory.fromCode.74.expect', (string) $file);
1818
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.80.php')))->extractAll();
1919
sameFile(__DIR__ . '/expected/Factory.fromCode.80.expect', (string) $file);
2020

21-
//$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.81.php')))->extractAll();
22-
//sameFile(__DIR__ . '/expected/Factory.fromCode.81.expect', (string) $file);
21+
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/classes.81.php')))->extractAll();
22+
sameFile(__DIR__ . '/expected/Factory.fromCode.81.expect', (string) $file);
2323

2424
$file = (new Extractor(file_get_contents(__DIR__ . '/fixtures/enum.php')))->extractAll();
2525
sameFile(__DIR__ . '/expected/Factory.fromCode.enum.expect', (string) $file);

tests/PhpGenerator/expected/ClassType.from.81.expect

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[Attr(new Abc\Attr(/* unknown */))]
12
class Class11
23
{
34
final public const FOO = 10;
@@ -9,4 +10,9 @@ class Class11
910
public function foo(Foo&Bar $c): Foo&Bar
1011
{
1112
}
13+
14+
15+
public function bar($c = new stdClass(/* unknown */))
16+
{
17+
}
1218
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Abc;
6+
7+
#[Attr(new Attr(max: 6))]
8+
class Class11
9+
{
10+
final public const FOO = 10;
11+
12+
public Foo&Bar $foo;
13+
public readonly array $ro;
14+
15+
16+
public function foo(Foo&Bar $c): Foo&Bar
17+
{
18+
}
19+
20+
21+
public function bar($c = new \stdClass)
22+
{
23+
}
24+
}
25+
26+
#[\Attribute]
27+
class Attr
28+
{
29+
}

tests/PhpGenerator/fixtures/classes.81.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Abc;
66

7+
#[Attr(new Attr(max: 6))]
78
class Class11
89
{
910
final public const FOO = 10;
@@ -12,6 +13,17 @@ class Class11
1213

1314
public readonly array $ro;
1415

15-
public function foo(Foo&Bar $c): Foo&Bar {
16+
public function foo(Foo&Bar $c): Foo&Bar
17+
{
1618
}
19+
20+
public function bar($c = new \stdClass)
21+
{
22+
}
23+
}
24+
25+
26+
#[\Attribute]
27+
class Attr
28+
{
1729
}

0 commit comments

Comments
 (0)