Skip to content

Version 1.8.0

Choose a tag to compare

@floriankraemer floriankraemer released this 27 Jan 13:59
· 24 commits to master since this release
c65fd95

🎉 New Features

✨ New Rule: Attribute Rule

New Rule for PHP 8+ Attribute Validation

  • Class: Phauthentic\PHPStanRules\Architecture\AttributeRule
  • Purpose: Validates PHP 8+ attributes on classes, methods, and properties using regex patterns
  • Key Features:
    • Allowed Attributes: Restrict which attributes can be used on matching targets
    • Forbidden Attributes: Prevent specific attributes from being used
    • Required Attributes: Enforce that certain attributes must be present
    • Flexible Targeting: Match classes, methods, and properties using regex patterns
    • Combined Rules: All three rule types can be applied simultaneously to the same target
  • Configuration: Supports three optional sections:
    • allowed - Define which attributes are permitted (whitelist)
    • forbidden - Define which attributes are prohibited (blacklist)
    • required - Define which attributes must be present

Configuration Example:

services:
    -
        class: Phauthentic\PHPStanRules\Architecture\AttributeRule
        arguments:
            config:
                allowed:
                    # Controllers can only have Route and Cache attributes on methods
                    -
                        classPattern: '/^App\\Controller\\.*/'
                        methodPattern: '/.*Action$/'
                        attributes:
                            - '/^Symfony\\Component\\Routing\\Annotation\\Route$/'
                            - '/^Symfony\\Component\\HttpKernel\\Attribute\\Cache$/'
                forbidden:
                    # Domain layer cannot have framework attributes
                    -
                        classPattern: '/^App\\Domain\\.*/'
                        methodPattern: '/.*/'
                        attributes:
                            - '/^Symfony\\.*/'
                            - '/^Doctrine\\.*/'
                required:
                    # All action methods must have a Route attribute
                    -
                        classPattern: '/^App\\Controller\\.*/'
                        methodPattern: '/.*Action$/'
                        attributes:
                            - '/^Symfony\\Component\\Routing\\Annotation\\Route$/'
        tags:
            - phpstan.rules.rule

Use Cases:

  • Enforce that controller actions only use approved routing attributes
  • Prevent framework-specific attributes in your domain layer (clean architecture)
  • Ensure entity properties only use Doctrine ORM mapping attributes
  • Require specific attributes on classes, methods, or properties
  • Combine all three rule types for comprehensive attribute validation

Error Messages:

  • Forbidden: Attribute {attribute} is forbidden on {type} {name}.
  • Not in allowed list: Attribute {attribute} is not in the allowed list for {type} {name}. Allowed patterns: {patterns}
  • Missing required: Missing required attribute matching pattern {pattern} on {type} {name}.

📚 Updated README

  • Added Attribute Rule to the Architecture Rules list
  • Sorted Architecture Rules list alphabetically for better discoverability

🚀 Migration Guide

For Existing Users

No Action Required! All existing configurations continue to work without any changes. The new Attribute Rule is opt-in and requires explicit configuration.

For New Attribute Rule

To use the new Attribute Rule, add the configuration to your phpstan.neon:

services:
    -
        class: Phauthentic\PHPStanRules\Architecture\AttributeRule
        arguments:
            config:
                allowed:
                    -
                        classPattern: '/^App\\Controller\\.*/'
                        attributes:
                            - '/^Symfony\\Component\\Routing\\Annotation\\Route$/'
                forbidden:
                    -
                        classPattern: '/^App\\Domain\\.*/'
                        attributes:
                            - '/^Symfony\\.*/'
                required:
                    -
                        classPattern: '/^App\\Entity\\.*/'
                        attributes:
                            - '/^Doctrine\\ORM\\Mapping\\Entity$/'
        tags:
            - phpstan.rules.rule

Full Changelog: 1.7.0...1.8.0