Skip to content

Commit 9c8c125

Browse files
committed
Factory: supports attributes in PHP 8
1 parent 1e896b3 commit 9c8c125

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

src/PhpGenerator/Factory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function fromClassReflection(\ReflectionClass $from, bool $withBodies = f
4040
$class->setImplements($ifaces);
4141

4242
$class->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
43+
$class->setAttributes(self::getAttributes($from));
4344
if ($from->getParentClass()) {
4445
$class->setExtends($from->getParentClass()->name);
4546
$class->setImplements(array_diff($class->getImplements(), $from->getParentClass()->getInterfaceNames()));
@@ -99,6 +100,7 @@ public function fromMethodReflection(\ReflectionMethod $from): Method
99100
$method->setReturnReference($from->returnsReference());
100101
$method->setVariadic($from->isVariadic());
101102
$method->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
103+
$method->setAttributes(self::getAttributes($from));
102104
if ($from->getReturnType() instanceof \ReflectionNamedType) {
103105
$method->setReturnType($from->getReturnType()->getName());
104106
$method->setReturnNullable($from->getReturnType()->allowsNull());
@@ -117,6 +119,7 @@ public function fromFunctionReflection(\ReflectionFunction $from, bool $withBody
117119
if (!$from->isClosure()) {
118120
$function->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
119121
}
122+
$function->setAttributes(self::getAttributes($from));
120123
if ($from->getReturnType() instanceof \ReflectionNamedType) {
121124
$function->setReturnType($from->getReturnType()->getName());
122125
$function->setReturnNullable($from->getReturnType()->allowsNull());
@@ -150,6 +153,7 @@ public function fromParameterReflection(\ReflectionParameter $from): Parameter
150153
: $from->getDefaultValue());
151154
$param->setNullable($param->isNullable() && $param->getDefaultValue() !== null);
152155
}
156+
$param->setAttributes(self::getAttributes($from));
153157
return $param;
154158
}
155159

@@ -164,6 +168,7 @@ public function fromConstantReflection(\ReflectionClassConstant $from): Constant
164168
: ($from->isProtected() ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC)
165169
);
166170
$const->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
171+
$const->setAttributes(self::getAttributes($from));
167172
return $const;
168173
}
169174

@@ -185,6 +190,7 @@ public function fromPropertyReflection(\ReflectionProperty $from): Property
185190
$prop->setInitialized(array_key_exists($prop->getName(), $defaults));
186191
}
187192
$prop->setComment(Helpers::unformatDocComment((string) $from->getDocComment()));
193+
$prop->setAttributes(self::getAttributes($from));
188194
return $prop;
189195
}
190196

@@ -327,4 +333,17 @@ private function parse($from): array
327333

328334
return [$code, $stmts];
329335
}
336+
337+
338+
private function getAttributes($from): array
339+
{
340+
if (PHP_VERSION_ID < 80000) {
341+
return [];
342+
}
343+
$res = [];
344+
foreach ($from->getAttributes() as $attr) {
345+
$res[] = new Attribute($attr->getName(), $attr->getArguments());
346+
}
347+
return $res;
348+
}
330349
}

tests/PhpGenerator/ClassType.from.80.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ require __DIR__ . '/../bootstrap.php';
1313
require __DIR__ . '/fixtures/classes.php80';
1414

1515
$res[] = ClassType::from(new Abc\Class8(null));
16+
$res[] = ClassType::from(new Abc\Class9);
1617

1718
sameFile(__DIR__ . '/expected/ClassType.from.80.expect', implode("\n", $res));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* @phpVersion 8.0
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\Closure;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
$closure = #[ExampleAttribute] function (stdClass $a, $b = null) {};
16+
17+
$function = Closure::from($closure);
18+
same(
19+
'#[ExampleAttribute] function (stdClass $a, $b = null) {
20+
}',
21+
(string) $function
22+
);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* @phpVersion 8.0
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\PhpGenerator\GlobalFunction;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
#[ExampleAttribute]
16+
17+
18+
function func(stdClass $a, $b = null)
19+
{
20+
return 1;
21+
}
22+
23+
24+
$function = GlobalFunction::from('func');
25+
same(
26+
'#[ExampleAttribute]
27+
function func(stdClass $a, $b = null)
28+
{
29+
}
30+
',
31+
(string) $function
32+
);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,29 @@ class Class8
77
) {
88
}
99
}
10+
11+
/**
12+
* Description of class.
13+
*/
14+
#[\ExampleAttribute]
15+
#[NamedArguments(foo: 'bar', bar: [1, 2, 3])]
16+
class Class9
17+
{
18+
/** Commented */
19+
#[ExampleAttribute]
20+
#[WithArguments(true)]
21+
public const FOO = 123;
22+
23+
/** @var resource */
24+
#[ExampleAttribute]
25+
public $handle;
26+
27+
28+
/**
29+
* Returns file handle
30+
*/
31+
#[ExampleAttribute]
32+
public function getHandle(#[WithArguments(123)] $mode)
33+
{
34+
}
35+
}

tests/PhpGenerator/fixtures/classes.php80

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,28 @@ class Class8
1313
) {
1414
}
1515
}
16+
17+
18+
/**
19+
* Description of class.
20+
*/
21+
#[\ExampleAttribute]
22+
#[NamedArguments(foo: 'bar', bar: [1, 2, 3])]
23+
class Class9
24+
{
25+
/** Commented */
26+
#[ExampleAttribute]
27+
#[WithArguments(true)]
28+
const FOO = 123;
29+
30+
/** @var resource */
31+
#[ExampleAttribute]
32+
public $handle;
33+
34+
35+
/** Returns file handle */
36+
#[ExampleAttribute]
37+
public function getHandle(#[WithArguments(123)] $mode)
38+
{
39+
}
40+
}

0 commit comments

Comments
 (0)