Fix Uninitialized Properties¶
Priority: 🟠P1 - HIGH Status: Planning Related Analysis: phpstan-rector-analysis-overview.md
Problem Statement¶
18 PHPStan errors for uninitialized properties - properties declared but not set in constructor or with default values.
Impact¶
- Bug Risk: High - can cause null reference errors
- Type Safety: Properties may be null when not expected
- Production Risk: Runtime null pointer exceptions
- Maintainability: Unclear initialization requirements
PHPStan Error Category¶
- Error ID:
property.uninitialized - Count: 18
- Priority: High - prevents runtime null errors
Investigation Required¶
For each uninitialized property:
- Should it be nullable?
- Should it be initialized in constructor?
- Should it have a default value?
- Is it always set before use?
- Is initialization missing by mistake?
Implementation Plan¶
Step 1: Get Complete List¶
Step 2: Categorize Properties¶
For each property, determine:
- Entity/Class name
- Property name
- Property type
- Usage pattern
Step 3: Fix Strategies¶
Strategy A: Make Nullable
Use when: Property is optional
Strategy B: Initialize in Constructor
// Before
private DateTimeImmutable $createdAt;
// After
private DateTimeImmutable $createdAt;
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
}
Use when: Property should always have a value
Strategy C: Provide Default Value
Use when: There's a sensible default
Strategy D: Use Promoted Constructor Parameter
// Before
private string $name;
public function __construct(string $name) {
$this->name = $name;
}
// After
public function __construct(
private string $name
) {}
Use when: Property is required at construction
Common Patterns¶
Entity Properties¶
Often uninitialized properties in entities are:
- Timestamps (createdAt, updatedAt)
- Collections (should be initialized in constructor)
- Optional relationships (should be nullable)
Service Properties¶
- May be set via setter injection
- May need to be nullable
- May need lazy initialization
Success Criteria¶
- All
property.uninitializederrors resolved - No null reference errors introduced
- Clear initialization contract
- All tests passing
Risk Assessment¶
Medium Risk:
- Changing nullability can affect logic
- May reveal hidden bugs
- Could require database migration for entities
Mitigation:
- Analyze usage before changing
- Add tests for edge cases
- Review all usages of property
- Check for null checks in existing code
Estimated Effort¶
Per Property:
- Investigation: 15 min
- Fix: 15-30 min
- Testing: 15 min
- Average: 30-45 min per property
Total for 18 properties:
- Estimate: 9-13 hours
Dependencies¶
None - can be done independently
Notes¶
This plan needs the complete list of uninitialized properties to be specific about fixes.
TODO:
- Extract full list from PHPStan
- Analyze each property's usage
- Determine appropriate fix strategy
- Check for database schema implications