• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DoclerLabs / api-client-generator / 21418891888

27 Jan 2026 11:51PM UTC coverage: 86.69% (-0.3%) from 86.973%
21418891888

Pull #131

github

web-flow
Bump phpunit/phpunit from 9.5.9 to 9.6.33

Bumps [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit) from 9.5.9 to 9.6.33.
- [Release notes](https://github.com/sebastianbergmann/phpunit/releases)
- [Changelog](https://github.com/sebastianbergmann/phpunit/blob/9.6.33/ChangeLog-9.6.md)
- [Commits](https://github.com/sebastianbergmann/phpunit/compare/9.5.9...9.6.33)

---
updated-dependencies:
- dependency-name: phpunit/phpunit
  dependency-version: 9.6.33
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #131: Bump phpunit/phpunit from 9.5.9 to 9.6.33

3465 of 3997 relevant lines covered (86.69%)

7.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.81
/src/Generator/SchemaMapperGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Generator;
6

7
use DateTimeImmutable;
8
use DoclerLabs\ApiClientException\UnexpectedResponseBodyException;
9
use DoclerLabs\ApiClientGenerator\Ast\Builder\ParameterBuilder;
10
use DoclerLabs\ApiClientGenerator\Ast\ParameterNode;
11
use DoclerLabs\ApiClientGenerator\Entity\Field;
12
use DoclerLabs\ApiClientGenerator\Input\Specification;
13
use DoclerLabs\ApiClientGenerator\Naming\SchemaMapperNaming;
14
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
15
use PhpParser\Node\Expr\Variable;
16
use PhpParser\Node\Stmt;
17
use PhpParser\Node\Stmt\ClassMethod;
18

19
class SchemaMapperGenerator extends MutatorAccessorClassGeneratorAbstract
20
{
21
    public const NAMESPACE_SUBPATH = '\\Schema\\Mapper';
22

23
    public const SUBDIRECTORY = 'Schema/Mapper/';
24

25
    private array $mapMethodThrownExceptions;
26

27
    public function generate(Specification $specification, PhpFileCollection $fileRegistry): void
28
    {
29
        foreach ($specification->getCompositeResponseFields() as $field) {
25✔
30
            /** @var Field $field */
31
            $this->generateMapper($fileRegistry, $field);
25✔
32
        }
33
    }
34

35
    protected function generateMapper(PhpFileCollection $fileRegistry, Field $root): void
36
    {
37
        $this->mapMethodThrownExceptions = [];
25✔
38

39
        $className    = SchemaMapperNaming::getClassName($root);
25✔
40
        $classBuilder = $this->builder
25✔
41
            ->class($className)
25✔
42
            ->implement('SchemaMapperInterface')
25✔
43
            ->addStmts($this->generateProperties($root))
25✔
44
            ->addStmt($this->generateConstructor($root))
25✔
45
            ->addStmt($this->generateMap($root));
25✔
46

47
        $this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
25✔
48
    }
49

50
    protected function generateProperties(Field $root): array
51
    {
52
        if ($this->phpVersion->isConstructorPropertyPromotionSupported()) {
25✔
53
            return [];
15✔
54
        }
55

56
        $properties = [];
10✔
57
        if ($root->isObject()) {
10✔
58
            $alreadyInjected = [];
10✔
59
            foreach ($root->getObjectProperties() as $child) {
10✔
60
                if ($child->isComposite()) {
9✔
61
                    $childClassName = SchemaMapperNaming::getClassName($child);
7✔
62
                    if (!isset($alreadyInjected[$childClassName])) {
7✔
63
                        $propertyName = SchemaMapperNaming::getPropertyName($child);
7✔
64
                        $properties[] = $this->builder->localProperty($propertyName, $childClassName, $childClassName, readonly: true);
7✔
65

66
                        $alreadyInjected[$childClassName] = true;
7✔
67
                    }
68
                }
69
            }
70
        }
71

72
        if ($root->isArrayOfObjects()) {
10✔
73
            $child = $root->getArrayItem();
3✔
74
            if ($child !== null && $child->isComposite()) {
3✔
75
                $propertyName   = SchemaMapperNaming::getPropertyName($child);
3✔
76
                $childClassName = SchemaMapperNaming::getClassName($child);
3✔
77
                $properties[]   = $this->builder->localProperty(
3✔
78
                    $propertyName,
3✔
79
                    $childClassName,
3✔
80
                    $childClassName,
3✔
81
                    readonly: true
3✔
82
                );
3✔
83
            }
84
        }
85

86
        return $properties;
10✔
87
    }
88

89
    protected function generateConstructor(Field $root): ?ClassMethod
90
    {
91
        $params     = [];
25✔
92
        $paramInits = [];
25✔
93

94
        if ($root->isObject()) {
25✔
95
            $alreadyInjected = [];
25✔
96
            foreach ($root->getObjectProperties() as $child) {
25✔
97
                if ($child->isComposite()) {
22✔
98
                    $childClassName = SchemaMapperNaming::getClassName($child);
17✔
99
                    if (!isset($alreadyInjected[$childClassName])) {
17✔
100
                        $propertyName = SchemaMapperNaming::getPropertyName($child);
17✔
101
                        $params[]     = $this->builder
17✔
102
                            ->param($propertyName)
17✔
103
                            ->setType($childClassName);
17✔
104

105
                        $paramInits[] = $this->builder->assign(
17✔
106
                            $this->builder->localPropertyFetch($propertyName),
17✔
107
                            $this->builder->var($propertyName)
17✔
108
                        );
17✔
109
                        $alreadyInjected[$childClassName] = true;
17✔
110
                    }
111
                }
112
            }
113
        }
114

115
        if ($root->isArrayOfObjects()) {
25✔
116
            $child = $root->getArrayItem();
5✔
117
            if ($child !== null && $child->isComposite()) {
5✔
118
                $propertyName = SchemaMapperNaming::getPropertyName($child);
5✔
119
                $params[]     = $this->builder
5✔
120
                    ->param($propertyName)
5✔
121
                    ->setType(SchemaMapperNaming::getClassName($child));
5✔
122

123
                $paramInits[] = $this->builder->assign(
5✔
124
                    $this->builder->localPropertyFetch($propertyName),
5✔
125
                    $this->builder->var($propertyName)
5✔
126
                );
5✔
127
            }
128
        }
129

130
        if (empty($params)) {
25✔
131
            return null;
25✔
132
        }
133

134
        if ($this->phpVersion->isConstructorPropertyPromotionSupported()) {
17✔
135
            foreach ($params as $param) {
10✔
136
                $param->makePrivate();
10✔
137
            }
138
        }
139
        if ($this->phpVersion->isReadonlyPropertySupported()) {
17✔
140
            foreach ($params as $param) {
5✔
141
                $param->makeReadonly();
5✔
142
            }
143
        }
144

145
        $params = array_map(
17✔
146
            static fn (ParameterBuilder $param): ParameterNode => $param->getNode(),
17✔
147
            $params
17✔
148
        );
17✔
149

150
        $constructor = $this->builder
17✔
151
            ->method('__construct')
17✔
152
            ->makePublic()
17✔
153
            ->addParams($params)
17✔
154
            ->composeDocBlock($params);
17✔
155

156
        if (!$this->phpVersion->isConstructorPropertyPromotionSupported()) {
17✔
157
            $constructor->addStmts($paramInits);
7✔
158
        }
159

160
        return $constructor->getNode();
17✔
161
    }
162

163
    protected function generateMap(Field $root): ClassMethod
164
    {
165
        $statements = [];
25✔
166
        $builder    = $this->builder->param('payload');
25✔
167
        $builder->setType('array');
25✔
168
        $payloadParam = $builder->getNode();
25✔
169

170
        $payloadVariable = $this->builder->var('payload');
25✔
171
        $this->addImport(
25✔
172
            sprintf(
25✔
173
                '%s%s\\%s',
25✔
174
                $this->baseNamespace,
25✔
175
                SchemaGenerator::NAMESPACE_SUBPATH,
25✔
176
                $root->getPhpClassName()
25✔
177
            )
25✔
178
        );
25✔
179

180
        if ($root->isFreeFormObject()) {
25✔
181
            $statements[] = $this->generateMapStatementForFreeFormObject($root, $payloadVariable);
5✔
182
        } elseif ($root->isObject()) {
22✔
183
            $statements = array_merge(
22✔
184
                $statements,
22✔
185
                $this->generateMapStatementsForObject($root, $payloadVariable)
22✔
186
            );
22✔
187
        }
188

189
        if ($root->isArrayOfObjects()) {
25✔
190
            $statements = array_merge(
5✔
191
                $statements,
5✔
192
                $this->generateMapStatementsForArrayOfObjects($root, $payloadVariable)
5✔
193
            );
5✔
194
        }
195

196
        return $this->builder
25✔
197
            ->method('toSchema')
25✔
198
            ->makePublic()
25✔
199
            ->addParam($payloadParam)
25✔
200
            ->addStmts($statements)
25✔
201
            ->setReturnType($root->getPhpTypeHint())
25✔
202
            ->composeDocBlock(
25✔
203
                [$payloadParam],
25✔
204
                $root->getPhpDocType(false),
25✔
205
                array_keys($this->mapMethodThrownExceptions)
25✔
206
            )
25✔
207
            ->getNode();
25✔
208
    }
209

210
    protected function generateMapStatementsForArrayOfObjects(
211
        Field $field,
212
        Variable $payloadVariable
213
    ): array {
214
        $itemsVar     = $this->builder->var('items');
5✔
215
        $statements[] = $this->builder->assign($itemsVar, $this->builder->array([]));
5✔
216

217
        $payloadItemVariable = $this->builder->var('payloadItem');
5✔
218
        $itemMapper          = $this->builder->localPropertyFetch(
5✔
219
            SchemaMapperNaming::getPropertyName($field->getArrayItem())
5✔
220
        );
5✔
221
        $itemMapperCall      = $this->builder->methodCall($itemMapper, 'toSchema', [$payloadItemVariable]);
5✔
222
        $foreachStatements[] = $this->builder->appendToArray($itemsVar, $itemMapperCall);
5✔
223

224
        $statements[] = $this->builder->foreach($payloadVariable, $payloadItemVariable, $foreachStatements);
5✔
225

226
        $statements[] = $this->builder->return(
5✔
227
            $this->builder->new(
5✔
228
                $field->getPhpClassName(),
5✔
229
                [$this->builder->argument($itemsVar, false, true)]
5✔
230
            )
5✔
231
        );
5✔
232

233
        return $statements;
5✔
234
    }
235

236
    protected function generateMapStatementForFreeFormObject(Field $root, Variable $payloadVariable): Stmt
237
    {
238
        $schemaInit = $this->builder->new($root->getPhpClassName(), [$payloadVariable]);
5✔
239

240
        return $this->builder->return($schemaInit);
5✔
241
    }
242

243
    protected function hasComposite(array $fields): bool
244
    {
245
        foreach ($fields as $field) {
19✔
246
            if ($field->isComposite()) {
19✔
247
                return true;
17✔
248
            }
249
        }
250

251
        return false;
19✔
252
    }
253

254
    protected function generateMapStatementsForObject(Field $root, Variable $payloadVariable): array
255
    {
256
        $statements = [];
22✔
257

258
        $requiredFields        = [];
22✔
259
        $requiredItemsNames    = [];
22✔
260
        $requiredResponseItems = [];
22✔
261
        $optionalFields        = [];
22✔
262
        $optionalResponseItems = [];
22✔
263
        foreach ($root->getObjectProperties() as $property) {
22✔
264
            if ($property->isRequired()) {
22✔
265
                $requiredFields[]        = $property;
19✔
266
                $requiredItemName        = $this->builder->val($property->getName());
19✔
267
                $requiredItemsNames[]    = $requiredItemName;
19✔
268
                $requiredResponseItems[] = $this->builder->getArrayItem($payloadVariable, $requiredItemName);
19✔
269
            } else {
270
                $optionalFields[] = $property;
19✔
271
                if ($root->hasOneOf() || $root->hasAnyOf()) {
19✔
272
                    $optionalResponseItems[] = $payloadVariable;
9✔
273
                } else {
274
                    $optionalResponseItems[] = $this->builder->getArrayItem(
19✔
275
                        $payloadVariable,
19✔
276
                        $this->builder->val($property->getName())
19✔
277
                    );
19✔
278
                }
279
            }
280
        }
281

282
        if (!empty($requiredFields)) {
22✔
283
            $unexpectedResponseBodyException = 'UnexpectedResponseBodyException';
19✔
284
            $this->addImport(UnexpectedResponseBodyException::class);
19✔
285

286
            $missingFieldsVariable  = $this->builder->var('missingFields');
19✔
287
            $missingFieldsArrayKeys = $this->builder->funcCall('array_keys', [$payloadVariable]);
19✔
288
            $missingFieldsArrayDiff = $this->builder->funcCall(
19✔
289
                'array_diff',
19✔
290
                [$this->builder->array($requiredItemsNames), $missingFieldsArrayKeys]
19✔
291
            );
19✔
292
            $missingFieldsImplode = $this->builder->funcCall(
19✔
293
                'implode',
19✔
294
                [$this->builder->val(', '), $missingFieldsArrayDiff]
19✔
295
            );
19✔
296

297
            $statements[] = $this->builder->expr(
19✔
298
                $this->builder->assign($missingFieldsVariable, $missingFieldsImplode)
19✔
299
            );
19✔
300

301
            $requiredFieldsIfCondition = $this->builder->not(
19✔
302
                $this->builder->funcCall('empty', [$missingFieldsVariable])
19✔
303
            );
19✔
304

305
            $exceptionMsg = sprintf(
19✔
306
                'Required attributes for `%s` missing in the response body: ',
19✔
307
                $root->getPhpClassName()
19✔
308
            );
19✔
309

310
            $requiredFieldsIfStatements[] = $this->builder->throw(
19✔
311
                $unexpectedResponseBodyException,
19✔
312
                $this->builder->concat($this->builder->val($exceptionMsg), $missingFieldsVariable)
19✔
313
            );
19✔
314

315
            $statements[] = $this->builder->if($requiredFieldsIfCondition, $requiredFieldsIfStatements);
19✔
316

317
            $this->mapMethodThrownExceptions[$unexpectedResponseBodyException] = true;
19✔
318
        }
319

320
        $requiredVars = [];
22✔
321
        foreach ($requiredFields as $i => $field) {
22✔
322
            /** @var Field $field */
323
            if ($field->isComposite()) {
19✔
324
                if ($field->isNullable()) {
3✔
325
                    $requiredVars[] = $this->builder->ternary(
3✔
326
                        $this->builder->notEquals($requiredResponseItems[$i], $this->builder->val(null)),
3✔
327
                        $this->builder->methodCall(
3✔
328
                            $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field)),
3✔
329
                            'toSchema',
3✔
330
                            [$requiredResponseItems[$i]]
3✔
331
                        ),
3✔
332
                        $this->builder->val(null)
3✔
333
                    );
3✔
334
                } else {
335
                    $requiredVars[] = $this->builder->methodCall(
3✔
336
                        $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field)),
3✔
337
                        'toSchema',
3✔
338
                        [$requiredResponseItems[$i]]
3✔
339
                    );
3✔
340
                }
341
            } elseif ($field->isDate()) {
19✔
342
                $this->addImport(DateTimeImmutable::class);
6✔
343
                $newDateTime = $this->builder->new('DateTimeImmutable', [$requiredResponseItems[$i]]);
6✔
344
                if ($field->isNullable()) {
6✔
345
                    $requiredVars[] = $this->builder->ternary(
3✔
346
                        $this->builder->notEquals($requiredResponseItems[$i], $this->builder->val(null)),
3✔
347
                        $newDateTime,
3✔
348
                        $this->builder->val(null)
3✔
349
                    );
3✔
350
                } else {
351
                    $requiredVars[] = $newDateTime;
6✔
352
                }
353
            } elseif ($field->isEnum() && $this->phpVersion->isEnumSupported()) {
19✔
354
                $this->addImport($this->fqdn($this->withSubNamespace(SchemaGenerator::NAMESPACE_SUBPATH), $field->getPhpClassName()));
5✔
355
                $newEnum = $this->builder->staticCall($field->getPhpClassName(), 'from', [$requiredResponseItems[$i]]);
5✔
356
                if ($field->isNullable()) {
5✔
357
                    $requiredVars[] = $this->builder->ternary(
×
358
                        $this->builder->notEquals($requiredResponseItems[$i], $this->builder->val(null)),
×
359
                        $newEnum,
×
360
                        $this->builder->val(null)
×
361
                    );
×
362
                } else {
363
                    $requiredVars[] = $newEnum;
5✔
364
                }
365
            } elseif ($field->isArrayOfEnums() && $this->phpVersion->isEnumSupported()) {
19✔
366
                $enumField = $field->getArrayItem();
1✔
367
                $this->addImport($this->fqdn($this->withSubNamespace(SchemaGenerator::NAMESPACE_SUBPATH), $enumField->getPhpClassName()));
1✔
368

369
                $arrowFunctionParam = $this->builder->param('item')->setType($enumField->getType()->toPhpType())->getNode();
1✔
370
                $arrayMapCall       = $this->builder->funcCall(
1✔
371
                    'array_map',
1✔
372
                    [
1✔
373
                        $this->builder->arrowFunction(
1✔
374
                            $this->builder->staticCall($enumField->getPhpClassName(), 'from', [$this->builder->var('item')]),
1✔
375
                            [$arrowFunctionParam],
1✔
376
                            $enumField->getPhpClassName(),
1✔
377
                        ),
1✔
378
                        $requiredResponseItems[$i],
1✔
379
                    ]
1✔
380
                );
1✔
381
                $requiredVars[] = $field->isNullable()
1✔
382
                    ? $this->builder->ternary(
1✔
383
                        $this->builder->notEquals($requiredResponseItems[$i], $this->builder->val(null)),
1✔
384
                        $arrayMapCall,
1✔
385
                        $this->builder->val(null)
1✔
386
                    )
1✔
387
                    : $arrayMapCall;
1✔
388
            } else {
389
                $requiredVars[] = $requiredResponseItems[$i];
18✔
390
            }
391
        }
392
        $schemaInit = $this->builder->new($root->getPhpClassName(), $requiredVars);
22✔
393

394
        if (!empty($optionalFields)) {
22✔
395
            $schemaVar    = $this->builder->var('schema');
19✔
396
            $matchesVar   = $this->builder->var('matches');
19✔
397
            $statements[] = $this->builder->assign($schemaVar, $schemaInit);
19✔
398

399
            $tryCatchStatements = [];
19✔
400
            foreach ($optionalFields as $i => $field) {
19✔
401
                /** @var Field $field */
402
                if ($field->isComposite()) {
19✔
403
                    $mapper = $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field));
17✔
404
                    if ($field->isNullable()) {
17✔
405
                        $optionalVar = $this->builder->ternary(
3✔
406
                            $this->builder->notEquals($optionalResponseItems[$i], $this->builder->val(null)),
3✔
407
                            $this->builder->methodCall(
3✔
408
                                $mapper,
3✔
409
                                'toSchema',
3✔
410
                                [$optionalResponseItems[$i]]
3✔
411
                            ),
3✔
412
                            $this->builder->val(null)
3✔
413
                        );
3✔
414
                    } else {
415
                        $optionalVar = $this->builder->methodCall(
17✔
416
                            $mapper,
17✔
417
                            'toSchema',
17✔
418
                            [$optionalResponseItems[$i]]
17✔
419
                        );
17✔
420
                    }
421
                } elseif ($field->isDate()) {
19✔
422
                    $this->addImport(DateTimeImmutable::class);
5✔
423
                    if ($field->isNullable()) {
5✔
424
                        $optionalVar = $this->builder->ternary(
3✔
425
                            $this->builder->notEquals($optionalResponseItems[$i], $this->builder->val(null)),
3✔
426
                            $this->builder->new('DateTimeImmutable', [$optionalResponseItems[$i]]),
3✔
427
                            $this->builder->val(null)
3✔
428
                        );
3✔
429
                    } else {
430
                        $optionalVar = $this->builder->new('DateTimeImmutable', [$optionalResponseItems[$i]]);
5✔
431
                    }
432
                } elseif ($field->isEnum() && $this->phpVersion->isEnumSupported()) {
19✔
433
                    $this->addImport($this->fqdn($this->withSubNamespace(SchemaGenerator::NAMESPACE_SUBPATH), $field->getPhpClassName()));
1✔
434
                    $optionalVar = $this->builder->staticCall($field->getPhpClassName(), 'from', [$optionalResponseItems[$i]]);
1✔
435
                } elseif ($field->isArrayOfEnums() && $this->phpVersion->isEnumSupported()) {
19✔
436
                    $enumField = $field->getArrayItem();
1✔
437
                    $this->addImport($this->fqdn($this->withSubNamespace(SchemaGenerator::NAMESPACE_SUBPATH), $enumField->getPhpClassName()));
1✔
438

439
                    $arrowFunctionParam = $this->builder->param('item')->setType($enumField->getType()->toPhpType())->getNode();
1✔
440
                    $arrayMapCall       = $this->builder->funcCall(
1✔
441
                        'array_map',
1✔
442
                        [
1✔
443
                            $this->builder->arrowFunction(
1✔
444
                                $this->builder->staticCall($enumField->getPhpClassName(), 'from', [$this->builder->var('item')]),
1✔
445
                                [$arrowFunctionParam],
1✔
446
                                $enumField->getPhpClassName(),
1✔
447
                            ),
1✔
448
                            $optionalResponseItems[$i],
1✔
449
                        ]
1✔
450
                    );
1✔
451
                    $optionalVar = $field->isNullable()
1✔
452
                        ? $this->builder->ternary(
1✔
453
                            $this->builder->notEquals($optionalResponseItems[$i], $this->builder->val(null)),
1✔
454
                            $arrayMapCall,
1✔
455
                            $this->builder->val(null)
1✔
456
                        )
1✔
457
                        : $arrayMapCall;
1✔
458
                } else {
459
                    $optionalVar = $optionalResponseItems[$i];
18✔
460
                }
461

462
                if ($root->hasOneOf() || $root->hasAnyOf()) {
19✔
463
                    $tryStatements = [
9✔
464
                        $this->builder->expr(
9✔
465
                            $this->builder->methodCall(
9✔
466
                                $schemaVar,
9✔
467
                                $this->getSetMethodName($field),
9✔
468
                                [$optionalVar]
9✔
469
                            )
9✔
470
                        ),
9✔
471
                    ];
9✔
472

473
                    $tryStatements[] = $this->builder->expr($this->builder->assign(
9✔
474
                        $this->builder->var('matches'),
9✔
475
                        $this->builder->operation($this->builder->var('matches'), '+', $this->builder->val(1))
9✔
476
                    ));
9✔
477

478
                    $this->addImport(UnexpectedResponseBodyException::class);
9✔
479
                    $catchStatement = $this->builder->catch(
9✔
480
                        [$this->builder->className('UnexpectedResponseBodyException')],
9✔
481
                        $this->builder->var('exception'),
9✔
482
                        []
9✔
483
                    );
9✔
484
                    $tryCatchStatements[] = $this->builder->tryCatch($tryStatements, [$catchStatement]);
9✔
485
                } else {
486
                    $ifCondition = $field->isNullable()
19✔
487
                        ? $this->builder->funcCall('array_key_exists', [$field->getName(), $payloadVariable])
7✔
488
                        : $this->builder->funcCall('isset', [$optionalResponseItems[$i]]);
19✔
489

490
                    $ifStmt = $this->builder->expr(
19✔
491
                        $this->builder->methodCall(
19✔
492
                            $schemaVar,
19✔
493
                            $this->getSetMethodName($field),
19✔
494
                            [$optionalVar]
19✔
495
                        )
19✔
496
                    );
19✔
497

498
                    $statements[] = $this->builder->if($ifCondition, [$ifStmt]);
19✔
499
                }
500
            }
501

502
            if ($root->hasOneOf() || $root->hasAnyOf()) {
19✔
503
                if ($root->getDiscriminator()) {
9✔
504
                    $ifCondition = $this->builder->funcCall('array_key_exists', [
3✔
505
                        /** @phpstan-ignore-next-line */
506
                        $root->getDiscriminator()->propertyName,
3✔
507
                        $payloadVariable,
3✔
508
                    ]);
3✔
509

510
                    $payloadDiscriminator = $this->builder->getArrayItem(
3✔
511
                        $payloadVariable,
3✔
512
                        /** @phpstan-ignore-next-line */
513
                        $this->builder->val($root->getDiscriminator()->propertyName)
3✔
514
                    );
3✔
515

516
                    $assignMethodName = $this->builder->expr(
3✔
517
                        $this->builder->assign(
3✔
518
                            $this->builder->var('methodName'),
3✔
519
                            $this->builder->concat(
3✔
520
                                $this->builder->val('set'),
3✔
521
                                $this->builder->funcCall('ucfirst', [$payloadDiscriminator])
3✔
522
                            )
3✔
523
                        )
3✔
524
                    );
3✔
525

526
                    $assignMapperName = $this->builder->expr(
3✔
527
                        $this->builder->assign(
3✔
528
                            $this->builder->var('mapperName'),
3✔
529
                            $this->builder->concat(
3✔
530
                                $payloadDiscriminator,
3✔
531
                                $this->builder->val('Mapper')
3✔
532
                            )
3✔
533
                        )
3✔
534
                    );
3✔
535

536
                    $schemaMethodCall = $this->builder->expr(
3✔
537
                        $this->builder->methodCall(
3✔
538
                            $this->builder->var('schema'),
3✔
539
                            '$methodName',
3✔
540
                            [
3✔
541
                                $this->builder->methodCall(
3✔
542
                                    $this->builder->localPropertyFetch('$mapperName'),
3✔
543
                                    'toSchema',
3✔
544
                                    [$payloadVariable]
3✔
545
                                ),
3✔
546
                            ]
3✔
547
                        )
3✔
548
                    );
3✔
549

550
                    $statements[] = $this->builder->if($ifCondition, [$assignMethodName, $assignMapperName, $schemaMethodCall]);
3✔
551
                } else {
552
                    $statements[] = $this->builder->assign($matchesVar, $this->builder->val(0));
6✔
553

554
                    $statements = [...$statements, ...$tryCatchStatements];
6✔
555

556
                    if ($root->hasAnyOf()) {
6✔
557
                        $statements[] = $this->builder->if(
3✔
558
                            $this->builder->equals($matchesVar, $this->builder->val(0)),
3✔
559
                            [
3✔
560
                                $this->builder->throw(
3✔
561
                                    'UnexpectedResponseBodyException'
3✔
562
                                ),
3✔
563
                            ]
3✔
564
                        );
3✔
565
                    }
566
                    if ($root->hasOneOf()) {
6✔
567
                        $statements[] = $this->builder->if(
3✔
568
                            $this->builder->notEquals($matchesVar, $this->builder->val(1)),
3✔
569
                            [
3✔
570
                                $this->builder->throw(
3✔
571
                                    'UnexpectedResponseBodyException'
3✔
572
                                ),
3✔
573
                            ]
3✔
574
                        );
3✔
575
                    }
576
                    $this->mapMethodThrownExceptions['UnexpectedResponseBodyException'] = true;
6✔
577
                }
578
            }
579

580
            if (!$this->hasComposite($optionalFields)) {
19✔
581
                $this->addImport(UnexpectedResponseBodyException::class);
19✔
582
                $statements[] = $this->builder->if(
19✔
583
                    $this->builder->funcCall('empty', [$this->builder->methodCall($schemaVar, 'toArray')]),
19✔
584
                    [
19✔
585
                        $this->builder->throw(
19✔
586
                            'UnexpectedResponseBodyException'
19✔
587
                        ),
19✔
588
                    ]
19✔
589
                );
19✔
590
                $this->mapMethodThrownExceptions['UnexpectedResponseBodyException'] = true;
19✔
591
            }
592

593
            $statements[] = $this->builder->return($schemaVar);
19✔
594
        } else {
595
            $statements[] = $this->builder->return($schemaInit);
12✔
596
        }
597

598
        return $statements;
22✔
599
    }
600
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc