[policy, no patch] Use declare(strict_types=1) in new code

Created on 23 November 2021, about 3 years ago
Updated 6 May 2023, over 1 year ago

Problem/Motivation

As per the PHP documentation at https://www.php.net/manual/en/language.types.declarations.php#language.t...

By default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible. For example, a function that is given an int for a parameter that expects a string will get a variable of type string.

It is possible to enable strict mode on a per-file basis. In strict mode, only a value corresponding exactly to the type declaration will be accepted, otherwise a TypeError will be thrown. The only exception to this rule is that an int value will pass a float type declaration.

This is already used in core across the CKEditor5 module, and also in an unrelated test (\Drupal\KernelTests\Core\ParamConverter\EntityConverterTest).

This issue proposes that it should be used for all new files added to core to improve type safety. This only really takes effect if 🌱 [policy, no patch] All new Drupal 10 code should use appropriate type hints wherever possible Needs review is accepted as well.

Steps to reproduce

Proposed resolution

Start using declare(strict_types=1); in all new code.

Remaining tasks

Decide whether this is a good idea.
Decide on a standard for adding declare(strict_types=1); to files.

User interface changes

API changes

Data model changes

Release notes snippet

🌱 Plan
Status

Active

Version

10.1 ✨

Component
BaseΒ  β†’

Last updated 2 days ago

Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024