Skip to content

Support union + intersection return type#26

Merged
makomweb merged 5 commits intomasterfrom
next
May 4, 2026
Merged

Support union + intersection return type#26
makomweb merged 5 commits intomasterfrom
next

Conversation

@makomweb
Copy link
Copy Markdown
Owner

@makomweb makomweb commented May 4, 2026

Issue

The Tactix library's ReturnTypeFactory threw a LogicException when analyzing code with union return types (e.g., Response|Error). It only supported scalar types, class names, and nullable types.

Solution

Extended Tactix to fully parse and preserve union and intersection type information.

Files Modified

1. ReturnType.php

Location: src/Analyzer/Class/ReturnType.php

Changes:

  • Added $unionTypes: Name[] property to store all types in a union
  • Added $intersectionTypes: Name[] property to store all types in an intersection
  • Updated union(array $types) factory method to accept array of types (signature change from single Name to Name[])
  • Updated intersection(array $types) factory method to accept array of types (signature change from single Name to Name[])
  • Added isUnion(): bool and isIntersection(): bool helper methods
  • Updated __toString() to render unions as Type1|Type2|Type3 and intersections as Type1&Type2&Type3

Impact: Backward compatible for clients that check isUnion() or isIntersection() on the enum. The union() and intersection() factory methods now require arrays, which is a breaking change to that API surface.

2. ReturnTypeFactory.php

Location: src/Analyzer/Class/ReturnTypeFactory.php

Changes:

  • Added imports for UnionType and IntersectionType from PhpParser\Node
  • Added branch to handle UnionType nodes: extracts all member types and passes array to ReturnType::union()
  • Added branch to handle IntersectionType nodes: extracts all member types and passes array to ReturnType::intersection()
  • Improved error message from generic to more specific

Impact: No breaking changes. Factory properly handles all modern PHP type syntax.

Summary of Impact

Aspect Before After
Union types ❌ Error ✅ Parsed, all members stored
Intersection types ❌ Error ✅ Parsed, all members stored
Type information N/A ✅ Full access via $unionTypes / $intersectionTypes
String representation N/A Response|Error, Foo&Bar
Helper methods N/A isUnion(), isIntersection()

@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

❌ Patch coverage is 90.90909% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.11%. Comparing base (a42647e) to head (d240802).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
src/Analyzer/Class/ReturnTypeFactory.php 81.25% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master      #26      +/-   ##
============================================
+ Coverage     72.77%   78.11%   +5.34%     
- Complexity      378      386       +8     
============================================
  Files            39       39              
  Lines           922      946      +24     
============================================
+ Hits            671      739      +68     
+ Misses          251      207      -44     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@makomweb makomweb merged commit 7df7539 into master May 4, 2026
3 checks passed
@makomweb makomweb deleted the next branch May 4, 2026 09:08
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.

1 participant