Skip to content

Commit 206cbe0

Browse files
committed
nonDbFakerStub -fix
1 parent 6f757c3 commit 206cbe0

2 files changed

Lines changed: 19 additions & 53 deletions

File tree

src/lib/FakerStubResolver.php

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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 !== '') {

tests/specs/issue_fix/20_consider_openapi_spec_examples_in_faker_code_generation/mysql/models/PetFaker.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,18 @@ public function generateModel($attributes = [])
7878
'id' => $uniqueFaker->numberBetween(0, 1000000),
7979
'name' => $faker->sentence,
8080
'age' => $faker->numberBetween(0, 200),
81-
'user' => $faker->randomElement(\app\models\User::find()->select("id")->column()),
81+
'user_id' => $faker->randomElement(\app\models\User::find()->select("id")->column()),
8282
'user_2' => array_map(function () use ($faker, $uniqueFaker) {
8383
return (new UserFaker)->generateModel()->attributes;
84-
},
85-
range(1, 4)),
84+
}, range(1, 4)),
8685
'tags' => array_map(function () use ($faker, $uniqueFaker) {
8786
return $uniqueFaker->sentence;
88-
},
89-
range(1, 4)),
87+
}, range(1, 4)),
9088
'arr_arr_int_2' => array_map(function () use ($faker, $uniqueFaker) {
91-
return array_map(
92-
function () use ($faker, $uniqueFaker) {
93-
return $faker->numberBetween(0, 1000000);
94-
},
95-
range(1, 11)
96-
);
97-
},
98-
range(1, 4)),
89+
return array_map(function () use ($faker, $uniqueFaker) {
90+
return $faker->numberBetween(0, 1000000);
91+
}, range(1, 11));
92+
}, range(1, 4)),
9993
'appearance' => [
10094
'height' => $faker->numberBetween(0, 20),
10195
'weight' => $faker->numberBetween(0, 1000000),

0 commit comments

Comments
 (0)