33namespace Phug \DependencyInjection ;
44
55use ReflectionFunction ;
6+ use ReflectionParameter ;
67
78class FunctionWrapper extends ReflectionFunction
89{
@@ -16,18 +17,22 @@ public function dumpParameters()
1617 $ parameters = [];
1718 foreach ($ this ->getParameters () as $ parameter ) {
1819 $ string = '' ;
19- if ( $ parameter -> isArray ()) {
20- $ string .= ' array ' ;
21- } elseif ( $ parameter -> getClass () ) {
22- $ string .= $ parameter -> getClass ()-> name . ' ' ;
20+ $ type = $ this -> getTypeAsString ( $ parameter );
21+
22+ if ( $ type ) {
23+ $ string .= " $ type " ;
2324 }
25+
2426 if ($ parameter ->isPassedByReference ()) {
2527 $ string .= '& ' ;
2628 }
29+
2730 $ string .= '$ ' .$ parameter ->name ;
31+
2832 if ($ parameter ->isOptional ()) {
2933 $ string .= ' = ' .var_export ($ parameter ->getDefaultValue (), true );
3034 }
35+
3136 $ parameters [] = $ string ;
3237 }
3338
@@ -59,4 +64,28 @@ public function dumpBody()
5964
6065 return $ code ;
6166 }
67+
68+ /**
69+ * Return the type as a string in a way compatible from PHP 5.5 to 8.0.
70+ *
71+ * @param ReflectionParameter $parameter
72+ *
73+ * @return string|null
74+ */
75+ protected function getTypeAsString (ReflectionParameter $ parameter )
76+ {
77+ if (method_exists ($ parameter , 'getType ' )) {
78+ /** @var mixed $parameter ReflectionParameter has hasType and getType methods since PHP 7.0. */
79+
80+ return $ parameter ->hasType () ? strval ($ parameter ->getType ()) : null ;
81+ }
82+
83+ if ($ parameter ->isArray ()) {
84+ return 'array ' ;
85+ }
86+
87+ $ class = $ parameter ->getClass ();
88+
89+ return $ class ? $ class ->name : null ;
90+ }
6291}
0 commit comments