Skip to content

Conversation

@JanTvrdik
Copy link
Member

Summary

New PHPStan rule that enforces native PHP type hints on class properties when a @var PhpDoc annotation exists and a native type is representable.

Features

  • PHP version aware: suggests types available for configured PHP version
    • PHP 7.4+: basic typed properties (int, string, bool, float, array, object, iterable, self, ?T)
    • PHP 8.0+: mixed, union types
    • PHP 8.1+: intersection types
    • PHP 8.2+: null, true, false standalone, DNF types
  • LSP safe: skips properties inherited from parent classes to avoid breaking inheritance
  • Trait safe: skips properties in traits (type may vary by usage)
  • PhpDoc driven: only suggests types when @var annotation exists

Configuration

parameters:
    shipmonkRules:
        enforceNativePropertyTypehint:
            enabled: true  # default when enableAllRules is true

Error identifier

shipmonk.missingNativePropertyTypehint

Example

class Example {
    /** @var int */
    public $count;  // Error: Missing native property typehint int
    
    /** @var string|int */
    public $value;  // Error on PHP 8.0+: Missing native property typehint string|int
    
    public int $typed;  // OK - already has native type
    
    public $noDoc;  // OK - no @var annotation
}

Test plan

  • Added test cases for PHP 7.4, 8.0, 8.1, 8.2 with version-specific type suggestions
  • Added test cases for parent class compatibility (LSP)
  • All 65 tests pass
  • PHPStan analysis passes

New rule that enforces native PHP type hints on class properties when a @var PhpDoc annotation exists and a native type is representable.

Features:
- PHP version aware: suggests types available for configured PHP version
  - PHP 7.4+: basic typed properties (int, string, bool, etc.)
  - PHP 8.0+: mixed, union types
  - PHP 8.1+: intersection types
  - PHP 8.2+: null, true, false standalone, DNF types
- Skips properties inherited from parent classes (LSP safety)
- Skips properties in traits (type may vary by usage)
- Only suggests types when @var annotation exists
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