Open flaviovs opened 1 month ago
I found these snippets:
psalm does not let one pass ""
(a value of type string
and type empty-string
) because it is neither a supported timezone name, offset value nor timezone abbreviation and thus results in an \DateInvalidTimeZoneException
being thrown. Unfortunately, the error message shown when not specifying the type of the properties isn't helpful, as it only suggests the string
type. But the more concrete type non-empty-string
is required, which can be specified using PHPDoc @var
.
The string
type of the properties cannot be silently upgraded to non-empty-string
, because changing the value of the properties to ""
(a string
value that is also empty-string
but not non-empty-string
) would also invalidate the call further down. It is unreasonable for a call to be invalid when only a value but not its type annotation is changed.
Upgrading a value from type string
to non-empty-string
can be done safely by ensuring that it isn't equals to the ""
value of type empty-string
: https://psalm.dev/r/ef46919ad4
I found these snippets:
First and foremost: conceptually, from a programming language perspective, "empty string" is not a type, but a runtime condition. String is the type.
But I see it.
Problem is: most properly typed libraries know nothing about empty/non-empty string "types" and just use string, which would require suppressing ArgumentTypeCoercion everywhere, which is like throwing the baby out with the bathwater.
Even PHP calls can be problematic: https://psalm.dev/r/4a2419e7f2
(Of course, one could also test for an empty string, but this could be redundant in many cases — as in your and my example — and of course added coded complexity, not to say a waste of CPU cycles.)
Anyways, I appreciate your comments.
I found these snippets:
https://psalm.dev/r/19a5e603dc