Skip to content

[RFC] Require strict typing for new components and/or classes #28423

Closed
@alcaeus

Description

@alcaeus

With Symfony 4 requiring PHP 7.1, strict typing could be introduce to reduce potential bugs, help with documentation and make the code more robust in general. However, since most of the code in Symfony also needs to run on older PHP versions until 2021 (when Symfony 3.4 is EOL'd), it's not yet practical to enforce strict typing in existing code, even for Symfony 5 where BC breaks would be acceptable.

To work around this issue, strict typing could be required for new components (e.g. the messenger component), since they won't be ported to a Symfony version designed to run on PHP < 7.1. Optionally, the requirement could be extended to new classes for existing components to ensure new code is strictly typed in general: in most cases, new classes even for existing components wouldn't be ported to older Symfony versions. If so, the requirement should be relaxed for those cases.

What would strict typing entail:

  • declare(strict_types=1) on top of the file to completely disable type coercion,
  • scalar type hints (including nullable, void, excluding object since PHP 7.2 isn't required)
  • Strict comparisons should be the norm with operators like == and != only being used in exceptional cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    RFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions