Skip to content

Introduce UnaryOperatorTypeSpecifyingExtension interface#5284

Open
Firehed wants to merge 1 commit intophpstan:2.1.xfrom
Firehed:unary-operator-type-specifying-extension
Open

Introduce UnaryOperatorTypeSpecifyingExtension interface#5284
Firehed wants to merge 1 commit intophpstan:2.1.xfrom
Firehed:unary-operator-type-specifying-extension

Conversation

@Firehed
Copy link
Contributor

@Firehed Firehed commented Mar 23, 2026

Summary

  • Adds UnaryOperatorTypeSpecifyingExtension interface as the unary operator counterpart to the existing OperatorTypeSpecifyingExtension for binary operators
  • Allows extensions to customize the types returned by unary -, +, and ~ operators
  • Integrates with InitializerExprTypeResolver to call registered extensions before falling back to built-in type resolution

This PR extracts the infrastructure portion from #5223 to allow the API to be reviewed and merged independently before the GMP extension that uses it.

Test plan

  • Added UnaryOperatorTypeSpecifyingExtensionTypeInferenceTest with a synthetic test extension
  • Test extension verifies all three unary operators (-, +, ~) call extensions correctly
  • All existing tests pass

🤖 Generated with Claude Code

This adds an extension point for unary operators (-, +, ~), similar to the
existing OperatorTypeSpecifyingExtension for binary operators.

New types:
- UnaryOperatorTypeSpecifyingExtension interface
- UnaryOperatorTypeSpecifyingExtensionRegistry
- UnaryOperatorTypeSpecifyingExtensionRegistryProvider interface
- LazyUnaryOperatorTypeSpecifyingExtensionRegistryProvider implementation

Integration:
- Hook into InitializerExprTypeResolver for getUnaryPlusType, getUnaryMinusType,
  getBitwiseNotType
- Update UnaryPlusHandler to use InitializerExprTypeResolver (like UnaryMinusHandler)
- Add service tag: phpstan.broker.unaryOperatorTypeSpecifyingExtension

Testing:
- TestUnaryOperand fixture class
- TestUnaryOperatorTypeSpecifyingExtension synthetic extension
- UnaryOperatorTypeSpecifyingExtensionTypeInferenceTest

This enables extensions like GMP to specify return types for unary operators.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@VincentLanglet
Copy link
Contributor

@Firehed There is just the lint build to fix.

And since this is a new API, I prefer to get the approval from @ondrejmirtes too.

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