Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Jan 27, 2026

analog #4554

assertType("non-empty-array<'bar'|'foo'|int, string>", array_replace($array1, $array3));
assertType("non-empty-array<'bar'|'foo'|int, string>", array_replace($array3, $array1));
assertType("non-empty-array<'bar'|'foo'|int, string>&hasOffset('bar')&hasOffset('foo')", array_replace($array1, $array3));
assertType("non-empty-array<'bar'|'foo'|int, string>&hasOffsetValue('bar', '2')&hasOffsetValue('foo', '1')", array_replace($array3, $array1));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this kind of overlaps. should we do something about it, or is it fine?

@staabm
Copy link
Contributor Author

staabm commented Jan 28, 2026

@claudepache @VincentLanglet you might be interessted in reviewing this one

Comment on lines +111 to +112
$hasOffsetValue = TrinaryLogic::createFromBoolean($argType->hasOffsetValueType($keyType)->yes());
$offsetTypes[$keyType->getValue()] = [
Copy link
Contributor

Choose a reason for hiding this comment

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

If multiple constantArray has the same Key, like array{1: int, 2: string}|array{1: int, 3: string} you will call hasOffsetValueType and getOffsetValueType two times to set the same value ; not sure if we could avoid the extra calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as long as this is not identified as a bottleneck I don't want to complicate it further

foreach ($offsetTypes as $key => [$hasOffsetValue, $offsetValueType]) {
$offsetTypes[$key] = [
$hasOffsetValue->and(TrinaryLogic::createMaybe()),
new MixedType(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically we could provide better than Mixed here.
Maybe a comment to explain there is no need to provide Union($offsetValueType, $argType->getArrayValues) since the value won't be used since we're only instantiating HasOffsetType

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added a comment

if ($constArrays !== []) {
foreach ($constArrays as $constantArray) {
foreach ($constantArray->getKeyTypes() as $keyType) {
$hasOffsetValue = TrinaryLogic::createFromBoolean($argType->hasOffsetValueType($keyType)->yes());
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unclear to me reading this why you're doing

TrinaryLogic::createFromBoolean($argType->hasOffsetValueType($keyType)->yes())

and not directly

$argType->hasOffsetValueType($keyType)

You're transforming the Maybe into a No ?

Copy link
Contributor Author

@staabm staabm Jan 28, 2026

Choose a reason for hiding this comment

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

correct. I need the trinary, as I later on combine it with $hasOffsetValue->and(TrinaryLogic::createMaybe()) for non constant arrays (else case)

}
if ($offsetTypes !== []) {
$knownOffsetValues = [];
foreach ($offsetTypes as $key => [$hasOffsetValue, $offsetType]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually, looking at the usage you could have use array<Type|null> as offsetTypes:

  • a Type means HasOffsetValueType (the yes case)
  • null means HasOffsetType (the maybe case)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree we could transform it. not sure this will lead to logic which is easier to follow?

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