Skip to content

PHPStan Error Analysis: 6349 Errors - Systematic Resolution Plan #203

@johnproblems

Description

@johnproblems

PHPStan Error Analysis Report

Executive Summary

The PHPStan analysis of the codebase revealed a total of 6349 file errors. While this number appears high, a significant portion of these errors are related to missing type information and dynamic property access, which can be systematically addressed. There are also critical issues related to nullability and incorrect data types that pose a direct threat to the operational stability of the application. The new "enterprise transformation" features appear to contribute to these errors, indicating a need for more rigorous static analysis and typing within these modules.

Error Categorization and Impact

The errors can be broadly categorized as follows:

1. Missing Type Information (High Count, Medium Severity)

Examples:

  • Method App\Actions\Application\GenerateConfig::handle() has no return type specified. (Numerous occurrences, 574/574 files have errors related to this)
  • Unable to resolve the template type TValue in call to function collect (93 occurrences)
  • Property App\Livewire\Project\Application\General::$parsedServices with generic class Illuminate\Support\Collection does not specify its types: TKey, TValue (Numerous occurrences)

Analysis: This category represents a large portion of the errors. While PHP itself might run without explicit type declarations, static analysis tools like PHPStan rely heavily on them for accurate checks. The lack of return type declarations, generic type specifications for collections, and un-typed properties lead to PHPStan being unable to fully verify code correctness.

Operational Impact:

  • Maintainability: Lowers code readability and makes it harder for developers to understand expected data types, increasing the risk of introducing bugs.
  • Reliability: Can lead to unexpected runtime type errors if incorrect data is passed, especially when refactoring or extending functionality.
  • Developer Experience: PHPStan provides less value in catching bugs early, requiring more manual testing.

2. Access to Undefined Properties (High Count, High Severity)

Examples:

  • Access to an undefined property App\Models\Server::$settings. (175 occurrences)
  • Access to an undefined property App\Models\Application::$settings. (143 occurrences)
  • Access to an undefined property App\Models\Organization::$activeLicense. (28 occurrences)

Analysis: This is a critical category. It often arises when Eloquent models dynamically provide properties (e.g., through relationships) that are not explicitly declared in the class. Without @property annotations or explicit property declarations, PHPStan cannot confirm their existence, leading to errors. This can also indicate actual typos or incorrect property access.

Operational Impact:

  • Runtime Errors: If the property genuinely does not exist or the relationship is not loaded, accessing it can lead to fatal "Undefined property" errors, crashing the application.
  • Debugging Difficulty: Such errors can be hard to debug as they might only manifest under specific conditions.
  • Code Fragility: Code becomes brittle as changes to underlying relationships or model structure can easily break existing logic.

3. Nullability Issues (Medium Count, High Severity)

Examples:

  • Cannot call method currentTeam() on App\Models\User|null. (66 occurrences)
  • Parameter Miss some screens coollabsio/coolify#1 $json of function json_decode expects string, string|null given. (47 occurrences)
  • Cannot access property $server on Illuminate\Database\Eloquent\Model|null. (13 occurrences)

Analysis: These errors occur when methods are called or operations are performed on variables that might be null, but the context expects a non-null value. This is a common source of TypeError or "Call to a member function on null" runtime exceptions.

Operational Impact:

  • Application Crashes: Directly leads to fatal errors and application downtime if not handled correctly.
  • Data Corruption: Can result in incorrect data processing or storage if null values are not properly validated before use.
  • Security Vulnerabilities: In some cases, unexpected null values can bypass validation logic, leading to security risks.

4. Incorrect Type Usage / Type Mismatches (Medium Count, Medium Severity)

Examples:

  • Parameter $properties of class OpenApi\Attributes\Schema constructor expects array<OpenApi\Attributes\Property>|null, array<string, array<string, string>> given. (66 occurrences)
  • Parameter Miss some screens coollabsio/coolify#1 $string of function trim expects string, string|null given. (16 occurrences)
  • Property App\Models\Server::$port (int) does not accept string. (1 occurrence)

Analysis: These indicate that a function or method is being called with arguments of a type different from what it expects, or a property is being assigned a value of an incompatible type.

Operational Impact:

  • Unexpected Behavior: Can lead to silent data coercion, incorrect logic, or runtime type errors, depending on PHP's strictness level.
  • Increased Debugging Time: Issues arising from type mismatches can be subtle and difficult to trace.

5. Unused/Unanalyzed Code (Low Count, Low Severity)

Examples:

  • Trait App\Traits\SaveFromRedirect is used zero times and is not analysed. (1 occurrence)

Analysis: These are minor issues that suggest dead code or unused traits, which can be cleaned up.

Operational Impact:

  • Code Bloat: Unused code adds to the codebase size and can slightly increase compilation/analysis time.
  • Confusion: Can confuse developers if they encounter unused code and wonder about its purpose.

6. Logic Errors (Low Count, Medium Severity)

Examples:

  • If condition is always true. (8 occurrences)
  • Expression on left side of ?? is not nullable. (48 occurrences)

Analysis: These indicate redundant or incorrect logical expressions which, while not always breaking, show areas where the code's intent might be misunderstood or could be simplified.

Operational Impact:

  • Inefficiency: Redundant checks can lead to slightly less efficient code.
  • Maintainability: Makes code harder to reason about and can hide potential bugs.

Enterprise Transformation Related Errors

Analyzing the error messages and file paths, a significant number of errors are directly or indirectly related to the "enterprise transformation" work.

Directly Related:

  • Access to an undefined property App\Models\Organization::$activeLicense. (28 occurrences)
  • Method App\Http\Middleware\LicenseValidationMiddleware::handle() should return Illuminate\Http\RedirectResponse|Illuminate\Http\Response but returns Illuminate\Http\JsonResponse. (6 occurrences)
  • Access to an undefined property App\Models\EnterpriseLicense::$organization. (6 occurrences)
  • Numerous entries related to App\Services\Enterprise\* methods having issues with return types or parameter types (e.g., Method App\Services\Enterprise\WhiteLabelService::validateImportData() has parameter $data with no value type specified in iterable type array.).
  • Errors in App\Models\Team::$subscription, App\Models\Subscription::$team, etc., which are likely part of enterprise licensing/subscription features.

Indirectly Related (High impact on enterprise features):

  • The widespread "Access to an undefined property" and "Cannot call method on null" errors found in App\Models\Server, App\Models\Application, App\Models\User, and App\Models\Team could severely impact enterprise features that rely on these core models and their relationships. For instance, if App\Models\User::$currentOrganization is null when an enterprise feature tries to access it, it will lead to a crash.
  • Errors related to API/OpenAPI specifications (Parameter $properties of class OpenApi\Attributes\Schema constructor expects...) suggest issues in how the API for enterprise features might be documented or implemented, potentially affecting integrations.

Conclusion for Enterprise Transformation: The new enterprise modules show a clear need for improved type-hinting, null-safety, and property declarations. The current state suggests that these features might be prone to runtime errors and could be difficult to maintain or extend. The errors are not just cosmetic; they represent potential vulnerabilities in the core logic of the enterprise offerings.

Path to Fix PHPStan Errors

Addressing 6349 errors requires a strategic, phased approach.

Phase 1: Low-Hanging Fruit & Critical Stability (Immediate Priority)

  1. Add @property annotations to Models: For all models frequently cited in "Access to an undefined property" errors (e.g., App\Models\Server, App\Models\Application, App\Models\Organization, App\Models\User, App\Models\Team, App\Models\StandaloneDocker, App\Models\SwarmDocker, etc.). This can quickly resolve hundreds of errors without changing logic.
  2. Fix Cannot call method on Null issues: Implement null checks or use null-safe operators (?->) where appropriate. These are direct causes of runtime crashes. Focus on:
    • Cannot call method currentTeam() on App\Models\User|null.
    • Cannot access property $server on Illuminate\Database\Eloquent\Model|null.
    • Other similar errors involving potentially null objects.
  3. Add missing return types to high-impact methods: Prioritize methods in core business logic or frequently called actions (App\Actions, App\Services) where missing return types could lead to unexpected behavior downstream.

Phase 2: Improve Type Safety and Maintainability (High Priority)

  1. Introduce Scalar and Object Type Hints: Systematically add scalar (string, int, bool, float) and object type hints to method parameters and return types where they are missing.
  2. Refine Collection Generics: Address "Unable to resolve the template type TValue/TKey in call to function collect" and similar errors by explicitly defining generic types for Illuminate\Support\Collection where possible (e.g., Collection<int, string>).
  3. Correct OpenApi Attribute Definitions: Fix the "Parameter $properties of class OpenApi\Attributes\Schema constructor expects..." errors to ensure accurate API documentation and maintainability.

Phase 3: Clean-up and Refinement (Medium Priority)

  1. Address Logic Errors: Review and correct errors like "If condition is always true." or "Expression on left side of ?? is not nullable." to simplify and clarify code logic.
  2. Remove Unused Code: Delete or comment out unused traits, properties, or methods identified by PHPStan.
  3. Review remaining type mismatches: Systematically go through the remaining type-related errors and resolve them by either correcting the types or adjusting the code to match expectations.

Ongoing Process:

  • Integrate PHPStan into CI/CD: Prevent new errors from being introduced by adding PHPStan checks to the continuous integration pipeline.
  • Gradual Refactoring: Address errors incrementally, focusing on the most problematic files or modules first.
  • Update Documentation: Ensure that any changes in type expectations or property usage are reflected in code documentation.

This structured approach will allow the team to progressively improve the code quality, reduce the risk of critical bugs, and make the codebase more maintainable for future development, especially for the evolving enterprise features.


Source: docs/phpstan analysis.md

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions