mess icon indicating copy to clipboard operation
mess copied to clipboard

feature: asArrayOfStringToNullabeString(): array<string, string|null>

Open the-toster opened this issue 4 years ago • 5 comments

Is this kind of methods acceptable for this lib?

the-toster avatar Sep 01 '20 07:09 the-toster

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?

zakirullin avatar Sep 01 '20 15:09 zakirullin

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.

the-toster avatar Sep 01 '20 15:09 the-toster

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.

zakirullin avatar Sep 01 '20 16:09 zakirullin

Though Psalm won't work here :/

zakirullin avatar Sep 01 '20 16:09 zakirullin

Actually, it can be typed with this feature I thnk - https://psalm.dev/docs/annotating_code/type_syntax/conditional_types/

the-toster avatar Sep 02 '20 07:09 the-toster