Skip to content

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented Feb 9, 2026

After looking again at #4435 I think this minor simplification makes sense

@staabm staabm marked this pull request as ready for review February 9, 2026 06:50
@staabm staabm requested a review from VincentLanglet February 9, 2026 06:50
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

Copy link
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

I was wondering if this simplification was interesting enough or if we should/could include the whole if ($leftTypesCount > 0 ...) given the fact the only changed line were

$resultType = $this->getTypeFromValue($leftTypeInner->getValue() | $rightTypeInner->getValue());
$resultType = $this->getTypeFromValue($leftTypeInner->getValue() & $rightTypeInner->getValue());
$resultType = $this->getTypeFromValue($leftTypeInner->getValue() ^ $rightTypeInner->getValue());

and could be resolve with a callback ; WDYT ?

@staabm
Copy link
Contributor Author

staabm commented Feb 9, 2026

and could be resolve with a callback ; WDYT ?

good idea - did it like that now.

my initial idea was to turn the pattern

		if ($leftType instanceof IntegerRangeType) {
			$leftTypes = $leftType->getFiniteTypes();
		} else {
			$leftTypes = $leftType->getConstantScalarTypes();
		}

into a TypeUtils method, so we can re-use it even outside of InitializerExprTypeResolver.
but for the InitializerExprTypeResolver-local refactoring it makes sense to go into your idea first.

thank you

*
* @return self::IS_UNKNOWN|self::IS_SCALAR_TYPE|Type
*/
private function getFiniteOrConstantScalarTypes(Type $leftType, Type $rightType, callable $operationCallable): int|Type
Copy link
Contributor

Choose a reason for hiding this comment

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

The method might be easier to read with early return ?

if ($leftTypesCount === 0 || $rightTypesCount === 0) {
     return self::IS_UNKNOWN;
}
...
if ($generalize) {
     return self::IS_SCALAR_TYPE;
}
...
return TypeCombinator::union(...$resultTypes);
```

I also wonder if a Type|bool isn't enough (and avoid introducing two constant). True when it need to be generalized.
Another solution could be including the generalize call inside this method and returning `[$leftType, $rightType, $result]` but I'm not sure it should be preferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The method might be easier to read with early return ?

done.

I also wonder if a Type|bool isn't enough (and avoid introducing two constant).

I had it like that before I introduced constants. was not happy with the client-side code of this method as it was pretty unreadable.

@staabm staabm merged commit eb4e247 into phpstan:2.1.x Feb 9, 2026
635 of 639 checks passed
@staabm staabm deleted the efactor-ietr branch February 9, 2026 18:17
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.

3 participants