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 #3154762: [policy, no patch] All new Drupal 10 code should use appropriate type hints wherever possible is accepted as well.
Steps to reproduce
Proposed resolution
Start using declare(strict_types=1); in all new code.
Documentation discussion in Coding Standards #3630579: Use declare(strict_types=1) in new code
Implementation at #3529705: Enforce strict types in new code
Remaining tasks
Decide whether this is a good idea.
Decide on a standard for adding declare(strict_types=1); to files.
Decide about documentation
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | 3250827-nr-bot.txt | 91 bytes | needs-review-queue-bot |
Issue fork drupal-3250827
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3250827-policy-no-patch
changes, plain diff MR !10003
Comments
Comment #2
longwaveIt might also be worth a test patch that changes this across all of core in one go, and seeing what breaks.
Comment #4
chi commented+1
Re #2. I guess many tests will be broken because of passing null values to string functions.
Comment #6
bbralaSometimes you just want to create a fun patch hehe.
Comment #7
wim leers+1 — existing code won't be affected if we start doing this for new files only for now.
Comment #8
bbralarebased, moving files confused it.
Comment #9
bbralaWhitespace.
Comment #10
pfrenssen+1
Comment #11
bbrala+1 here also.
Bit unfrotunate cspell fails, locallty it doesnt :/
Comment #12
chi commentedSo far no one objected, so the first part of the issue can be considered as completed. Right?
As of coding standard it actually consists of two independent rules.
1. Whether or not put spaces around
=.2. Where to place the declaration. On the same line as opening php tag or on a new line below.
Comment #13
chi commentedI like the way it is placed in PhpUnit.
https://github.com/sebastianbergmann/phpunit/blob/9.5/src/Exception.php#L1
Though I would prefer it having spaces around
=.Example:
<?php declare(strict_types = 1);Comment #14
longwaveI would also prefer it on the opening line, it just wastes vertical space if we had it on line 3 (presumably line 2 is always blank). I have no preference over the spacing around the equals.
Interestingly this was also raised with Symfony some time ago and they turned down the idea: https://github.com/symfony/symfony/issues/28423
There is an argument that if we use PHPStan we should not need PHP's strict typing, as PHPStan can do all this and more.
Comment #15
bbralaThat issue is an interesting read. Would recommend.
Comment #16
sweetchuckNot exactly.
PHPStan is a static code analyzer.
declare(strict_types = 1)works on runtime.In most of the cases PHPStan can give a warning, but not all the time.
Comment #17
sweetchuckI know that, a lot of library (PHPUnit was mentioned before) puts the declare statement in the same line.
<?php declare(...);I am not fun of that style.
strict_typesis not the only thing that can be declared.https://www.php.net/manual/en/control-structures.declare.php
Right now there is only three of them, maybe more to come in the future.
ticksandencodingwon't be used in core and contrib any time soon,but I would like to use a one statement per line style.
Comment #18
chi commentedRe #17. In reality, strict_types is the only declare statement in use.
Comment #19
andypostThis declares can go away in PHP 9.0 https://externals.io/message/112230#112233
Comment #23
quietone commentedCame across this and I decided to create an MR to see the failures.
Comment #24
mstrelan commentedGood idea @quetone, I had thought about doing the same. I think fixing most of #3404246: [META] Fix strict type errors detected by phpstan would go a long way to getting this green. From memory there were no reported errors in core/lib/Drupal/Component, so maybe we could try limit to just that area first.
Comment #25
quietone commentedSeems I didn't do Components in the MR, so I have added that now.
Comment #26
quietone commentedThere is consensus here that this is a good idea and the standard for adding the declaration has also been agreed to. So, the policy has been agreed to.
Does this require documentation? If so, where?
FYI, the latest MR added strict types to Components and that is failing. The MR is information only and is not needed to resolve this policy issue.
Comment #27
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #28
quietone commentedAs stated in #26, the MR is for information only.
There is one remaining question, where to document?
Comment #29
dwwI don't know of any docs specific to "how to write good code for Core" anywhere. 😅 Here are some of the spots I found while searching for a home:
https://www.drupal.org/community/contributor-guide/skill/php-codingprogr...
https://www.drupal.org/community/contributor-guide/task/create-a-merge-r...
Neither of those are particularly discoverable or exactly relevant.
Probably more likely to be found if we considered this a "Coding standard" not a core-only policy, but that's a bit of a slippery slope.
Maybe we want a new child page under https://www.drupal.org/about/core/policies for stuff like this. Something like "Core coding policies". Would also be somewhere to document the outcome of #3488476: [policy, no patch] Allow using @final when a class is not considered as an extension point vs. #3019332: Use final to define classes that are NOT extension points. We could write up #3154762: [policy, no patch] All new Drupal 10 code should use appropriate type hints wherever possible there, too.
Comment #30
mstrelan commentedWhile we're discussing if we want to do this, I figured we could do this fairly simply with a git diff in a gitlab ci job. Opened #3529705: Enforce strict types in new code with a proof of concept.
Comment #32
nicxvan commentedI think the easiest thing is to just place it in the coding standards.
We already do this in practice, we should document this and make it a standard.
Comment #33
quietone commentedCreated a follow up in Coding standards to discuss making this a requirement for all new code. #3630579: Use declare(strict_types=1) in new code
Comment #34
quietone commentedcatch brought this up in committer channels where I and @mstrelan participated in the discussion. There is agreement to do this, and it is already being done in core. There is now a follow up in Coding Standards to discuss implementing in the standards, and there #3529705: Enforce strict types in new code to implement this for core.
Therefor, closing this policy issue as fixed.
Thanks everyone for helping to resolve this.