mess
mess copied to clipboard
feature: asArrayOfStringToNullabeString(): array<string, string|null>
Is this kind of methods acceptable for this lib?
I believe it's not. Since client-side code won't get any confidence about the value under each key (would have to do if-else every iteration).
$mess = new Mess($whatever);
foreach ($mess->getArrayOfStringToMixed() as $k => $_) {
$mess[$k]->findString();
}
// $whatever array<string,string|null>
// Even though Psalm might give a warning here, so consider saving loop's result into a new variable
Just kind of assertion-like style. Maybe something like this would work for you? What's your use case?
Thank you, I get your point.
Thats looks good, but in this case consumer code deals with Mess
object.
In my case, I want to get from my Config::getOption('fb')
something like array<string, string>
or array<string, string|null>
, or array<string, string()>
(empty string value), so I can use it like this:
$options = ConfigStorage::getOption('fb');
$connectorConfig = new ConnectorConfig($options['app_id'] ?? '', $options['secret'] ?? '');
if($connectorConfig->isValid()) {
// ....
}
so keep Mess
object inside ConfigStorage
, and my ConnectorConfig
is operate only string
's.
Now, I just add converter for findArrayOfStringToMixed
to ConfigStorage
, and I is ok.
Also maybe we should consider something like this:
Mess::getAsArrayOfStringToCustom(callable $caster)
And a few default callables like the one you've mentioned:
$mess->getArrayOfStringToCustom(Type::stringOrNull()); // array<string,string|null>
Just thinking.
Though Psalm won't work here :/
Actually, it can be typed with this feature I thnk - https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/