@@ -30,21 +30,33 @@ public function __construct(
3030
3131 public function validate (\ReflectionParameter $ parameter , $ argument )
3232 {
33- if ($ parameter ->isArray ()) {
34- if ($ parameter ->allowsNull () && is_null ($ argument )) {
35- return ;
36- }
37- $ this ->validateArrayArgument ($ argument );
38- } elseif ($ parameter ->getClass ()) {
39- $ this ->validateObjectArgument ($ parameter ->getClass ()->getName (), $ argument , $ parameter ->allowsNull ());
33+ $ parameterType = $ parameter ->getType ();
34+ if ($ parameterType === null ) {
35+ return ;
36+ }
37+
38+ if ($ parameterType ->getName () === 'array ' ) {
39+ $ this ->validateArrayArgument ($ parameter , $ argument );
40+ } elseif (class_exists ($ parameterType ->getName ())) {
41+ $ this ->validateObjectArgument ($ parameterType ->getName (), $ argument , $ parameter ->allowsNull ());
4042 }
4143
4244 // other arguments don't need to be or can't be validated
4345 }
4446
45- private function validateArrayArgument ($ argument )
47+ private function validateArrayArgument (\ ReflectionParameter $ parameter , $ argument )
4648 {
47- if (!is_array ($ argument )) {
49+ if ($ parameter ->allowsNull () && is_null ($ argument )) {
50+ return ;
51+ }
52+
53+ if (class_exists ('Symfony\Component\ExpressionLanguage\Expression ' ) && $ argument instanceof Expression) {
54+ $ this ->validateExpressionArgument ('array ' , $ argument , $ parameter ->allowsNull ());
55+ } else {
56+ if (is_array ($ argument )) {
57+ return ;
58+ }
59+
4860 throw new TypeHintMismatchException (sprintf (
4961 'Argument of type "%s" should have been an array ' ,
5062 gettype ($ argument )
@@ -95,14 +107,14 @@ private function validateDefinitionArgument($className, Definition $definition)
95107 $ this ->validateClass ($ className , $ resultingClass );
96108 }
97109
98- private function validateExpressionArgument ($ className , Expression $ expression , $ allowsNull )
110+ private function validateExpressionArgument ($ type , Expression $ expression , $ allowsNull )
99111 {
100112 $ expressionLanguage = new ExpressionLanguage ();
101113
102114 $ this ->validateExpressionSyntax ($ expression , $ expressionLanguage );
103115
104116 if ($ this ->evaluateExpressions ) {
105- $ this ->validateExpressionResult ($ className , $ expression , $ allowsNull , $ expressionLanguage );
117+ $ this ->validateExpressionResult ($ type , $ expression , $ allowsNull , $ expressionLanguage );
106118 }
107119 }
108120
@@ -115,7 +127,7 @@ private function validateExpressionSyntax(Expression $expression, ExpressionLang
115127 }
116128 }
117129
118- private function validateExpressionResult ($ className , Expression $ expression , $ allowsNull , ExpressionLanguage $ expressionLanguage )
130+ private function validateExpressionResult ($ expectedType , Expression $ expression , $ allowsNull , ExpressionLanguage $ expressionLanguage )
119131 {
120132 try {
121133 $ result = $ expressionLanguage ->evaluate ($ expression , array ('container ' => $ this ->containerBuilder ));
@@ -130,20 +142,29 @@ private function validateExpressionResult($className, Expression $expression, $a
130142
131143 throw new TypeHintMismatchException (sprintf (
132144 'Argument for type-hint "%s" is an expression that evaluates to null, which is not allowed ' ,
133- $ className
145+ $ expectedType
134146 ));
135147 }
136148
137- if (! is_object ($ result )) {
149+ if ($ expectedType === ' array ' && ! is_array ($ result )) {
138150 throw new TypeHintMismatchException (sprintf (
139- 'Argument for type-hint "%s" is an expression that evaluates to a non-object ' ,
140- $ className
151+ 'Argument for type-hint "%s" is an expression that evaluates to a non-array ' ,
152+ $ expectedType
141153 ));
142154 }
143155
144- $ resultingClass = get_class ($ result );
156+ if (class_exists ($ expectedType )) {
157+ if (!is_object ($ result )) {
158+ throw new TypeHintMismatchException (sprintf (
159+ 'Argument for type-hint "%s" is an expression that evaluates to a non-object ' ,
160+ $ expectedType
161+ ));
162+ }
145163
146- $ this ->validateClass ($ className , $ resultingClass );
164+ $ resultingClass = get_class ($ result );
165+
166+ $ this ->validateClass ($ expectedType , $ resultingClass );
167+ }
147168 }
148169
149170 private function validateClass ($ expectedClassName , $ actualClassName )
0 commit comments