@@ -373,7 +373,10 @@ public function fakeForObject(SpecObjectInterface $items, int $depth = 0): strin
373373 * Re-indents a compact wrapInArray() output string to match the correct depth inside fakeForObject().
374374 * wrapInArray() always uses hardcoded 12/8-space indentation; when its result is embedded as a
375375 * property value inside a fakeForObject() output, the indentation must be adjusted.
376- * For a nested array_map body the inner call is expanded to multi-line style via expandCompactArrayMap().
376+ *
377+ * For a simple body (single return statement): shift = bodyIndent - 12, placing the return at bodyIndent.
378+ * For a nested body (return array_map(...)): shift = bodyIndent - 8, so the inner return lands at
379+ * bodyIndent+4 and the inner closing brace lands at bodyIndent — matching wrapInArray's 12/8 ratio.
377380 */
378381 private function reindentArrayMapForObject (string $ code , int $ depth ): string
379382 {
@@ -386,47 +389,19 @@ private function reindentArrayMapForObject(string $code, int $depth): string
386389 }
387390 [$ body , $ count ] = [$ m [1 ], $ m [2 ]];
388391
389- // Shift all continuation lines by the delta between the target indent and wrapInArray's hardcoded 12 spaces.
390- $ shift = strlen ($ bodyIndent ) - 12 ;
392+ // For nested array_map: wrapInArray places the inner return at 12 spaces and the inner closing
393+ // brace at 8 spaces. We want the inner return at bodyIndent+4 and the inner brace at bodyIndent,
394+ // so the shift is bodyIndent - 8. For simple (non-nested) bodies the shift is bodyIndent - 12.
395+ $ shift = strlen ($ bodyIndent ) - (str_starts_with ($ body , 'return array_map( ' ) ? 8 : 12 );
391396 if ($ shift > 0 ) {
392397 $ body = preg_replace ('/\n/ ' , "\n" . str_repeat (' ' , $ shift ), $ body );
393398 }
394399
395- if (str_starts_with ($ body , 'return array_map( ' )) {
396- $ inner = substr ($ body , 7 , -1 ); // strip "return " prefix and trailing ";"
397- $ expanded = $ this ->expandCompactArrayMap ($ inner , $ bodyIndent );
398- return "array_map(function () use ( \$faker, \$uniqueFaker) { \n"
399- . $ bodyIndent . "return {$ expanded }; \n"
400- . $ closeIndent . "}, range(1, {$ count })) " ;
401- }
402-
403400 return "array_map(function () use ( \$faker, \$uniqueFaker) { \n"
404401 . $ bodyIndent . $ body . "\n"
405402 . $ closeIndent . "}, range(1, {$ count })) " ;
406403 }
407404
408- /**
409- * Expands a compact wrapInArray() string (single-line function + range) into multi-line style,
410- * using $baseIndent as the reference indentation level for the opening "array_map(" line.
411- */
412- private function expandCompactArrayMap (string $ code , string $ baseIndent ): string
413- {
414- $ pat = '/^array_map\(function \(\) use \(\$faker, \$uniqueFaker\) \{\n (.*)\n \}, range\(1, (\d+)\)\)$/s ' ;
415- if (!preg_match ($ pat , $ code , $ m )) {
416- return $ code ;
417- }
418- [$ body , $ count ] = [$ m [1 ], $ m [2 ]];
419- $ funcIndent = $ baseIndent . ' ' ;
420- $ innerIndent = $ baseIndent . ' ' ;
421-
422- return "array_map( \n"
423- . $ funcIndent . "function () use ( \$faker, \$uniqueFaker) { \n"
424- . $ innerIndent . $ body . "\n"
425- . $ funcIndent . "}, \n"
426- . $ funcIndent . "range(1, {$ count }) \n"
427- . $ baseIndent . ") " ;
428- }
429-
430405 /**
431406 * This method must be only used incase of array
432407 * @param SpecObjectInterface $items
@@ -449,13 +424,10 @@ public function handleOneOf(SpecObjectInterface $items, int $count): string
449424
450425 $ inp = $ aDataType instanceof Reference ? $ aDataType : ['items ' => $ aDataType ->getSerializableData ()];
451426 $ aFaker = $ this ->aElementFaker ($ inp , $ this ->attribute ->columnName );
452- /**
453- * Each $dataTypeN gets its own line (12-space indent = wrapInArray body level).
454- * wrapInArray output (array_map) gets +4 spaces on continuation lines (12→16, 8→12).
455- * fakeForObject output (starts with "[") is left as-is — depth=1 already gives 16/12.
456- * return goes on its own line.
457- */
458- if (str_contains ($ aFaker , PHP_EOL ) && !str_starts_with ($ aFaker , '[ ' )) {
427+ // Shift all continuation lines by 4 spaces so that multi-line values
428+ // (array_map or object literals from fakeForObject) are indented one level
429+ // deeper than the $dataTypeN assignment (12 → 16 for body, 8 → 12 for closing).
430+ if (str_contains ($ aFaker , PHP_EOL )) {
459431 $ aFaker = str_replace (PHP_EOL , PHP_EOL . ' ' , $ aFaker );
460432 }
461433 if ($ result !== '' ) {
0 commit comments