Skip to content

Get rid of instanceof in StringOrArray extension#163

Closed
herndlm wants to merge 1 commit intoszepeviktor:masterfrom
herndlm:StringOrArray
Closed

Get rid of instanceof in StringOrArray extension#163
herndlm wants to merge 1 commit intoszepeviktor:masterfrom
herndlm:StringOrArray

Conversation

@herndlm
Copy link
Copy Markdown
Contributor

@herndlm herndlm commented Feb 26, 2023

No description provided.

@IanDelMar
Copy link
Copy Markdown
Contributor

I'm not quite sure that this is sufficient. While $keyType instanceof StringType was a boolean $keyType->isString() is a trinary logic. We should cover $keyType->isString()->maybe() as well, don't we?

@herndlm
Copy link
Copy Markdown
Contributor Author

herndlm commented Feb 26, 2023

Not sure tbh, this does not change behaviour at least to before, except e.g. supporting unions of strings too. Maybes like mixed were also not considered before. It could still be improved any time I guess

if ($keyType->isString()->yes()) {
return new ArrayType(new StringType(), new StringType());
}
return new ArrayType(new IntegerType(), new StringType());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new ArrayType(new IntegerType(), new StringType());
if ($keyType->isString()->no()) {
return new ArrayType(new IntegerType(), new StringType());
}
return TypeCombinator::union(
new ArrayType(new StringType(), new StringType()),
new ArrayType(new IntegerType(), new StringType())
);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're going for I think. I don't really understand though what this extension is even doing :) I was just replacing stuff without changing much behaviour.
Why does it not just pass through the array but recreate it for example. Unfortunately not on my computer anymore to dig deeper..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sets the value type to string no matter what. However, this is not what the extension was intended to I think. See #154.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check it later and try to add tests too it not existing yet. But does it make sense to you to directly pass through the key type? It should noy be modified, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea, give me a bit of time :)

@IanDelMar
Copy link
Copy Markdown
Contributor

I agree, it is as good as before, it just makes it more obvious that the unknown key type case was not covered.

@herndlm herndlm closed this Feb 27, 2023
@herndlm herndlm deleted the StringOrArray branch April 13, 2023 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants