Generate timestamp parsing that static analysis accepts without an annotation - #2132
Open
Amoifr wants to merge 1 commit into
Open
Generate timestamp parsing that static analysis accepts without an annotation#2132Amoifr wants to merge 1 commit into
Amoifr wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2128
The generated code relied on
/** @var \DateTimeImmutable $d */ $d = ...to say thatcreateFromFormat()cannot return false, which neither Psalm nor PHPStan honours. The generator now emits code that carries the same information in the type system, so no annotation and no baseline entry are needed:It has to be an expression, since the generated code sits inside array literals, which rules out
assert()and any statement form. A throw expression is the only shape that narrows the type there, and it happens to be the right behaviour: today a malformed timestamp reaches a\DateTimeImmutabletyped property and dies on aTypeErrorwith nothing pointing at the response.UnparsableResponsealready exists in the core for exactly this and was simply never used by the generator.Only the required branch changes. The optional one already narrows correctly through
($d = ...) ? $d : null, which is why it never needed a baseline entry.Result. The two
PossiblyFalsePropertyAssignmentValueentries are gone frompsalm.baseline.xml, andpsalm.pharreportsNo errors found!. On the 16 regenerated files, PHPStan at level 7 goes from 16 errors to none, all of the shapeRestoreDateTime: DateTimeImmutable|false; the project's own level 6 stays green../generate --allis stable, a second run changes nothing.One trap worth flagging for future generator work.
RestJsonParser::$importsis private, so the$this->imports[] = ...I first wrote inJsonRpcParsercreated a dynamic property, deprecated since 8.2, and the import silently never reached the generated file. The five json-rpc clients came out referencing an unimported class, which PHPStan caught. Hence the sharedrejectUnparsableTimestamp()on the parent rather than the same code in both parsers.Touched clients: DynamoDb, Kinesis, S3Vectors, Ses and StepFunctions, with a changelog entry each.