Skip to content

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:

  1. Should it be nullable?
  2. Should it be initialized in constructor?
  3. Should it have a default value?
  4. Is it always set before use?
  5. Is initialization missing by mistake?

Implementation Plan

Step 1: Get Complete List

make phpstan 2>&1 | grep "property.uninitialized" > /tmp/uninitialized-props.txt

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

// Before
private string $name;

// After
private ?string $name = null;

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

// Before
private int $count;

// After
private int $count = 0;

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.uninitialized errors 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:

  1. Extract full list from PHPStan
  2. Analyze each property's usage
  3. Determine appropriate fix strategy
  4. Check for database schema implications