1.2.0: Improving the MethodMustReturnType to support unions (#7)
Release Notes
π New Features
β¨ Enhanced Method Must Return Type Rule
- New Feature: Added support for "void" as a type in addition to the legacy
void: true
approach - New Feature: Added
oneOf
functionality for union types - allows specifying an array of types where one must match - New Feature: Added
allOf
functionality for union types - allows specifying an array of types where all must be present - New Feature: Added
anyOf
as an alias foroneOf
for better readability - New Feature: Added regex pattern support in
oneOf
,allOf
, andanyOf
arrays usingregex:
prefix - Enhancement: Made configuration fields optional with sensible defaults (
nullable
,void
,objectTypePattern
) - Enhancement: Added configuration normalization to handle missing fields gracefully
π§ Configuration Improvements
- Backward Compatibility: All existing configurations continue to work without changes
- Flexible Configuration: Minimal configurations now work without requiring all fields
- Regex Support: Can use patterns like
'regex:/^App\\Entity\\/'
to match entity classes
π§ Documentation Improvements
π Enhanced Method Must Return Type Rule Documentation
- New Examples: Added comprehensive examples for
oneOf
,allOf
, andanyOf
usage - Regex Documentation: Added documentation for regex pattern support with examples
- Configuration Guide: Updated configuration examples to show new optional fields
- Usage Examples: Added real-world examples for entity validation and union types
π Updated Configuration Examples
- New Configuration Patterns: Added examples for minimal configurations
- Regex Examples: Added examples showing how to use regex patterns for class matching
- Union Type Examples: Added examples for both
oneOf
andallOf
scenarios
β New Test Cases
οΏ½οΏ½ Comprehensive Test Coverage
- AnyOfRuleTest: Tests for the new
anyOf
functionality - RegexRuleTest: Tests for regex pattern matching in
anyOf
arrays - RegexAllOfRuleTest: Tests for regex pattern matching in
allOf
arrays - EntityRegexRuleTest: Tests for realistic entity pattern matching scenarios
- FacadeRuleTest: Tests for minimal configuration scenarios
- UnionTypeRuleTest: Tests for union type functionality
- Enhanced MethodMustReturnTypeRuleTest: Updated existing tests for new functionality
π New Test Data Files
data/MethodMustReturnType/AnyOfTestClass.php
: Test cases foranyOf
functionalitydata/MethodMustReturnType/EntityRegexTestClass.php
: Test cases for entity regex patternsdata/MethodMustReturnType/FacadeTestClass.php
: Test cases for minimal configurationsdata/MethodMustReturnType/RegexAllOfTestClass.php
: Test cases forallOf
with regexdata/MethodMustReturnType/RegexTestClass.php
: Test cases for basic regex functionalitydata/MethodMustReturnType/UnionTypeTestClass.php
: Test cases for union type validation
ποΈ Code Quality Improvements
π§ Enhanced MethodMustReturnTypeRule
- New Methods: Added
normalizeConfig()
,isTypeMatchWithRegex()
,getExpectedTypeDescription()
- Improved Error Handling: Better error messages for union types and regex patterns
- Code Organization: Better separation of concerns with dedicated methods for different validation types
- Type Safety: Enhanced type checking and validation logic
π Bug Fixes
- Configuration Defaults: Fixed issues with missing configuration fields causing errors
- Regex Pattern Handling: Proper boolean conversion for regex pattern matching
- Union Type Parsing: Improved union type parsing and validation logic
- Error Message Consistency: Standardized error message formatting
π Statistics
- 16 files changed with 700+ lines added and 33 lines removed
- 17 new test files created for comprehensive coverage
- 100% backward compatibility maintained with existing configurations
π Migration Guide
For Existing Users
No changes required! All existing configurations will continue to work exactly as before.
For New Features
To use the new union type functionality:
-
class: Phauthentic\PHPStanRules\Architecture\MethodMustReturnTypeRule
arguments:
returnTypePatterns:
-
pattern: '/^MyClass::getValue$/'
anyOf: ['int', 'string', 'bool']
-
pattern: '/^MyClass::getEntity$/'
anyOf: ['regex:/^App\\Entity\\/', 'void']
tags:
- phpstan.rules.rule
This release significantly enhances the flexibility and power of the Method Must Return Type Rule while maintaining full backward compatibility.