Skip to content

Commit e41010f

Browse files
authored
Use quoted literal in phpdoc when psalm needs them (#1518)
1 parent 03c4ca4 commit e41010f

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Generator/CodeGenerator/TypeGenerator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,23 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
103103
}
104104
}
105105

106+
$phpdocMemberName = $member->getName();
107+
108+
// Psalm treats a scalar type names as keywords and makes them lowercase even in array shape keys which are not types.
109+
// When a different case is needed, it requires using a quoted literal instead of an identifier.
110+
// TODO remove that code once https://github.com/vimeo/psalm/issues/10008 is solved (in a release)
111+
if (\in_array(strtolower($phpdocMemberName), ['bool', 'null', 'int', 'float', 'double', 'scalar']) && $phpdocMemberName !== strtolower($phpdocMemberName)) {
112+
$phpdocMemberName = "'" . $phpdocMemberName . "'";
113+
}
114+
106115
if ($nullable) {
107-
$body[] = sprintf(' %s?: %s,', $member->getName(), 'null|' . $param);
116+
$body[] = sprintf(' %s?: %s,', $phpdocMemberName, 'null|' . $param);
108117
} elseif ($allNullable) {
109118
// For input objects, the constructor allows to omit all members to set them later. But when provided,
110119
// they should respect the nullability of the member.
111-
$body[] = sprintf(' %s?: %s,', $member->getName(), $param);
120+
$body[] = sprintf(' %s?: %s,', $phpdocMemberName, $param);
112121
} else {
113-
$body[] = sprintf(' %s: %s,', $member->getName(), $param);
122+
$body[] = sprintf(' %s: %s,', $phpdocMemberName, $param);
114123
}
115124
}
116125
$body = array_merge($body, $extra);

0 commit comments

Comments
 (0)