@@ -127,11 +127,11 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
127127 $ this ->classes [] = $ childClass ;
128128
129129 $ nodeTypes = $ this ->getParameterTypes ($ node );
130- $ paramType = $ this ->getParamType ($ nodeTypes );
130+ $ paramType = implode ('| ' , $ nodeTypes );
131+ $ acceptScalar = 'array ' !== $ paramType ;
131132
132- $ hasNormalizationClosures = $ this ->hasNormalizationClosures ($ node );
133133 $ comment = $ this ->getComment ($ node );
134- if ($ hasNormalizationClosures && ' array ' !== $ paramType ) {
134+ if ($ acceptScalar ) {
135135 $ comment = \sprintf (" * @template TValue of %s \n * @param TValue \$value \n%s " , $ paramType , $ comment );
136136 $ comment .= \sprintf (' * @return %s|$this ' ."\n" , $ childClass ->getFqcn ());
137137 $ comment .= \sprintf (' * @psalm-return (TValue is array ? %s : static) ' ."\n " , $ childClass ->getFqcn ());
@@ -142,9 +142,9 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
142142
143143 $ property = $ class ->addProperty (
144144 $ node ->getName (),
145- $ this -> getType ( $ childClass ->getFqcn (), $ hasNormalizationClosures )
145+ $ childClass ->getFqcn ().( $ acceptScalar ? ' |scalar ' : '' )
146146 );
147- $ body = $ hasNormalizationClosures && ' array ' !== $ paramType ? '
147+ $ body = $ acceptScalar ? '
148148COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
149149{
150150 if (!\is_array($value)) {
@@ -215,17 +215,18 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
215215 $ name = $ this ->getSingularName ($ node );
216216 $ prototype = $ node ->getPrototype ();
217217 $ methodName = $ name ;
218- $ hasNormalizationClosures = $ this ->hasNormalizationClosures ($ node ) || $ this ->hasNormalizationClosures ($ prototype );
219218
220219 $ nodeParameterTypes = $ this ->getParameterTypes ($ node );
221220 $ prototypeParameterTypes = $ this ->getParameterTypes ($ prototype );
222221 $ noKey = null === $ key = $ node ->getKeyAttribute ();
222+ $ acceptScalar = ['array ' ] !== $ nodeParameterTypes || ['array ' ] !== $ prototypeParameterTypes ;
223+
223224 if (!$ prototype instanceof ArrayNode || ($ prototype instanceof PrototypedArrayNode && $ prototype ->getPrototype () instanceof ScalarNode)) {
224225 $ class ->addUse (ParamConfigurator::class);
225226 $ property = $ class ->addProperty ($ node ->getName ());
226227 if ($ noKey ) {
227228 // This is an array of values; don't use singular name
228- $ nodeTypesWithoutArray = array_filter ($ nodeParameterTypes , static fn ( $ type ) => 'array ' !== $ type );
229+ $ nodeTypesWithoutArray = array_diff ($ nodeParameterTypes , [ 'array ' ] );
229230 $ body = '
230231/**
231232 * @param ParamConfigurator|list<ParamConfigurator|PROTOTYPE_TYPE>EXTRA_TYPE $value
@@ -244,7 +245,7 @@ public function NAME(PARAM_TYPE $value): static
244245 'PROPERTY ' => $ property ->getName (),
245246 'PROTOTYPE_TYPE ' => implode ('| ' , $ prototypeParameterTypes ),
246247 'EXTRA_TYPE ' => $ nodeTypesWithoutArray ? '| ' .implode ('| ' , $ nodeTypesWithoutArray ) : '' ,
247- 'PARAM_TYPE ' => $ this -> getParamType ( $ nodeParameterTypes, true ) ,
248+ 'PARAM_TYPE ' => [ ' mixed ' ] !== $ nodeParameterTypes ? ' ParamConfigurator| ' . implode ( ' | ' , $ nodeParameterTypes ) : ' mixed ' ,
248249 ]);
249250 } else {
250251 $ body = '
@@ -261,7 +262,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
261262
262263 $ class ->addMethod ($ methodName , $ body , [
263264 'PROPERTY ' => $ property ->getName (),
264- 'TYPE ' => $ this -> getParamType ( $ prototypeParameterTypes, true ) ,
265+ 'TYPE ' => [ ' mixed ' ] !== $ prototypeParameterTypes ? ' ParamConfigurator| ' . implode ( ' | ' , $ prototypeParameterTypes ) : ' mixed ' ,
265266 'VAR ' => '' === $ key ? 'key ' : $ key ,
266267 'VALUE ' => 'value ' === $ key ? 'data ' : 'value ' ,
267268 ]);
@@ -279,13 +280,14 @@ public function NAME(string $VAR, TYPE $VALUE): static
279280
280281 $ property = $ class ->addProperty (
281282 $ node ->getName (),
282- $ this -> getType ( $ childClass ->getFqcn ().'[] ' , $ hasNormalizationClosures )
283+ $ childClass ->getFqcn ().'[] ' .( $ acceptScalar ? ' |scalar ' : '' )
283284 );
284285
285- $ paramType = $ this ->getParamType ($ noKey ? $ nodeParameterTypes : $ prototypeParameterTypes );
286+ $ paramType = implode ('| ' , $ noKey ? $ nodeParameterTypes : $ prototypeParameterTypes );
287+ $ acceptScalar = 'array ' !== $ paramType ;
286288
287289 $ comment = $ this ->getComment ($ node );
288- if ($ hasNormalizationClosures && ' array ' !== $ paramType ) {
290+ if ($ acceptScalar ) {
289291 $ comment = \sprintf (" * @template TValue of %s \n * @param TValue \$value \n%s " , $ paramType , $ comment );
290292 $ comment .= \sprintf (' * @return %s|$this ' ."\n" , $ childClass ->getFqcn ());
291293 $ comment .= \sprintf (' * @psalm-return (TValue is array ? %s : static) ' ."\n " , $ childClass ->getFqcn ());
@@ -295,7 +297,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
295297 }
296298
297299 if ($ noKey ) {
298- $ body = $ hasNormalizationClosures && ' array ' !== $ paramType ? '
300+ $ body = $ acceptScalar ? '
299301COMMENTpublic function NAME(PARAM_TYPE $value = []): CLASS|static
300302{
301303 $this->_usedProperties[ \'PROPERTY \'] = true;
@@ -320,7 +322,7 @@ public function NAME(string $VAR, TYPE $VALUE): static
320322 'PARAM_TYPE ' => $ paramType ,
321323 ]);
322324 } else {
323- $ body = $ hasNormalizationClosures && ' array ' !== $ paramType ? '
325+ $ body = $ acceptScalar ? '
324326COMMENTpublic function NAME(string $VAR, PARAM_TYPE $VALUE = []): CLASS|static
325327{
326328 if (!\is_array($VALUE)) {
@@ -389,8 +391,11 @@ private function getParameterTypes(NodeInterface $node): array
389391 $ paramTypes = [];
390392 if ($ node instanceof BaseNode) {
391393 foreach ($ node ->getNormalizedTypes () as $ type ) {
394+ if (ExprBuilder::TYPE_ANY === $ type ) {
395+ return ['mixed ' ];
396+ }
397+
392398 $ paramTypes [] = match ($ type ) {
393- ExprBuilder::TYPE_ANY => 'mixed ' ,
394399 ExprBuilder::TYPE_STRING => 'string ' ,
395400 ExprBuilder::TYPE_NULL => 'null ' ,
396401 ExprBuilder::TYPE_ARRAY => 'array ' ,
@@ -407,15 +412,9 @@ private function getParameterTypes(NodeInterface $node): array
407412 $ paramTypes [] = 'int ' ;
408413 } elseif ($ node instanceof FloatNode) {
409414 $ paramTypes [] = 'float ' ;
410- } elseif ($ node instanceof EnumNode) {
411- $ paramTypes [] = 'mixed ' ;
412415 } elseif ($ node instanceof ArrayNode) {
413416 $ paramTypes [] = 'array ' ;
414- } elseif ($ node instanceof VariableNode) {
415- $ paramTypes [] = 'mixed ' ;
416- }
417-
418- if (\in_array ('mixed ' , $ paramTypes , true )) {
417+ } else {
419418 return ['mixed ' ];
420419 }
421420
@@ -594,25 +593,4 @@ private function getSubNamespace(ClassBuilder $rootClass): string
594593 {
595594 return \sprintf ('%s \\%s ' , $ rootClass ->getNamespace (), substr ($ rootClass ->getName (), 0 , -6 ));
596595 }
597-
598- private function hasNormalizationClosures (NodeInterface $ node ): bool
599- {
600- try {
601- $ r = new \ReflectionProperty ($ node , 'normalizationClosures ' );
602- } catch (\ReflectionException ) {
603- return false ;
604- }
605-
606- return [] !== $ r ->getValue ($ node );
607- }
608-
609- private function getType (string $ classType , bool $ hasNormalizationClosures ): string
610- {
611- return $ classType .($ hasNormalizationClosures ? '|scalar ' : '' );
612- }
613-
614- private function getParamType (array $ types , bool $ withParamConfigurator = false ): string
615- {
616- return \in_array ('mixed ' , $ types , true ) ? 'mixed ' : ($ withParamConfigurator ? 'ParamConfigurator| ' : '' ).implode ('| ' , $ types );
617- }
618596}
0 commit comments