File tree Expand file tree Collapse file tree 4 files changed +71
-3
lines changed Expand file tree Collapse file tree 4 files changed +71
-3
lines changed Original file line number Diff line number Diff line change 9
9
10
10
namespace Nette \PhpGenerator ;
11
11
12
+ use Nette ;
13
+
12
14
13
15
/**
14
16
* Global function.
@@ -20,9 +22,9 @@ final class GlobalFunction
20
22
use Traits \CommentAware;
21
23
use Traits \AttributeAware;
22
24
23
- public static function from (string $ function , bool $ withBody = false ): self
25
+ public static function from (string | \ Closure $ function , bool $ withBody = false ): self
24
26
{
25
- return (new Factory )->fromFunctionReflection (new \ ReflectionFunction ($ function ), $ withBody );
27
+ return (new Factory )->fromFunctionReflection (Nette \ Utils \Callback:: toReflection ($ function ), $ withBody );
26
28
}
27
29
28
30
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ final class Method
28
28
private bool $ abstract = false ;
29
29
30
30
31
- public static function from (string |array $ method ): static
31
+ public static function from (string |array | \ Closure $ method ): static
32
32
{
33
33
return (new Factory )->fromMethodReflection (Nette \Utils \Callback::toReflection ($ method ));
34
34
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * @phpVersion 8.1
5
+ */
6
+
7
+ declare (strict_types=1 );
8
+
9
+ use Nette \PhpGenerator \GlobalFunction ;
10
+
11
+ require __DIR__ . '/../bootstrap.php ' ;
12
+
13
+
14
+ /** global */
15
+ #[ExampleAttribute]
16
+ function func (stdClass $ a , $ b = null )
17
+ {
18
+ echo sprintf ('hello, %s ' , 'world ' );
19
+ return 1 ;
20
+ }
21
+
22
+
23
+ $ function = GlobalFunction::from (func (...));
24
+ same (
25
+ <<<'XX'
26
+ /**
27
+ * global
28
+ */
29
+ #[ExampleAttribute]
30
+ function func(stdClass $a, $b = null)
31
+ {
32
+ }
33
+
34
+ XX,
35
+ (string ) $ function ,
36
+ );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * @phpVersion 8.1
5
+ */
6
+
7
+ declare (strict_types=1 );
8
+
9
+ use Nette \PhpGenerator \Method ;
10
+
11
+ require __DIR__ . '/../bootstrap.php ' ;
12
+
13
+
14
+ class Foo
15
+ {
16
+ public static function bar (int $ a , ...$ b ): void
17
+ {
18
+ }
19
+ }
20
+
21
+ $ method = Method::from (Foo::bar (...));
22
+ same (
23
+ <<<'XX'
24
+ public static function bar(int $a, ...$b): void
25
+ {
26
+ }
27
+
28
+ XX,
29
+ (string ) $ method ,
30
+ );
You can’t perform that action at this time.
0 commit comments