Skip to content

Commit 944bc15

Browse files
committed
Fix useless string cast on already-string-typed map keys in normalize()
MapType::createNormalizationLoopOutputAssignement() unconditionally cast the loop key to string, but normalization always iterates a getter whose declared return type is this MapType's own array<string, ...> contract (see getDocTypeHint()) -- the key is already provably string, so PHPStan correctly flags the cast as dead code (cast.useless). Denormalization's createLoopOutputAssignement() is untouched: it processes raw mixed-typed external data where the key's type isn't statically known, so the cast there is legitimately defensive. Surfaced by Zoho CRM's record.json spec (a project consuming this fork via require-dev) generating a normalizer for a free-form additionalProperties map for the first time -- Desk's specs never exercised this MapType normalization path.
1 parent 1bbb439 commit 944bc15

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/Component/GeneratorCore/Guesser/Guess/MapType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ protected function createNormalizationLoopOutputAssignement(Expr $valuesVar, ?Ex
7171
throw new LogicException('MapType requires a loop key expression');
7272
}
7373

74-
return new Expr\ArrayDimFetch($valuesVar, new Expr\Cast\String_($loopKeyVar));
74+
// Unlike createLoopOutputAssignement() (denormalization, where the source is
75+
// raw mixed-typed external data and the key's runtime type isn't statically
76+
// known), normalization always iterates a getter whose declared return type
77+
// is this very MapType's own array<string, ...> (see getDocTypeHint() above)
78+
// -- so $loopKeyVar is already provably `string` and casting it is dead code
79+
// PHPStan correctly flags as cast.useless.
80+
return new Expr\ArrayDimFetch($valuesVar, $loopKeyVar);
7581
}
7682
}

0 commit comments

Comments
 (0)