Skip to content

1.2.0: Improving the MethodMustReturnType to support unions (#7)

Compare
Choose a tag to compare
@floriankraemer floriankraemer released this 29 Jul 13:30
· 3 commits to master since this release
23e5d35

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 for oneOf for better readability
  • New Feature: Added regex pattern support in oneOf, allOf, and anyOf arrays using regex: 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, and anyOf 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 and allOf 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 for anyOf functionality
  • data/MethodMustReturnType/EntityRegexTestClass.php: Test cases for entity regex patterns
  • data/MethodMustReturnType/FacadeTestClass.php: Test cases for minimal configurations
  • data/MethodMustReturnType/RegexAllOfTestClass.php: Test cases for allOf with regex
  • data/MethodMustReturnType/RegexTestClass.php: Test cases for basic regex functionality
  • data/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.