Replies: 2 comments 4 replies
-
For regexps, I don't think those would be useful to Psalm. And there's currently no way to define new scalar subtypes. |
Beta Was this translation helpful? Give feedback.
3 replies
-
The plugin route seems more natural for me. One thing to keep in mind is that every new type in Psalm's core must be able to be compared to all others and to have rule for transformations for example: /** @var email-string $a */
$a = 'a@a.com';
$b = strtolower($a);
//what should be the type of $b here? email-string? lowercase-string?
$c = strreplace('.com', '.net', $a);
//and now? Is it still an email-string?
if($a === ''){
//maybe we should define somewhere that an email-string can't ever be empty?
}
if(is_numeric($a)){
//this shouldn't be possible either, but you have to say so somewhere
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Psalm has certain pseudo-types for strings like
numeric-string
, that give a certain meaning to the string beyond being a mere string, orlowercase-string
that enforces a certain format/structure.In my applications, I often pass around Uuids - generally as an object, which is no issue, but in some cases it has to be passed around as string, and I was imagining having a
uuid-string
psalm pseudo class would be cool for that - but that's probably too specific, and there's other things one might look for (email-string
,phone-string
, etc..) so I was wondering if it might be a worthwhile addition to generally be able to specify a string's structure, somewhat similar to how we already can specify an array structure; for example by adding a regex - something likestring</^[0-9a-f]{8}$/>
? - which we then could alias via@psalm-type
:@psalm-type UuidString = string</^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i>
Alternatively: is there a way to define our own pseudo-types via a plugin?
Beta Was this translation helpful? Give feedback.
All reactions