Problem/Motivation
This is slightly different from #3050720: [Meta] Implement strict typing in existing code which is about adding type hints to code that doesn't have it.
I got caught out by a bool type hint accepting a string (which casts to TRUE) in a file that didn't have declare(strict_types = 1)
Steps to reproduce
N/A
Proposed resolution
Strict types doesn't require that every parameter/return has a type hint, it just makes the existing ones behave more strictly. Adding declare(strict_types = 1) to tests introduces over 3000 errors. Before we can add this we need to fix the errors.
Fix strict type errors
To allow for meaningful reviews the scope has been limited and this has been split in to the following issues:
- #3397890: Fix strict type errors in unit tests
- #3397905: [meta] Fix strict type errors in kernel tests
- #3399413: [meta] Fix strict type errors in functional tests
- #3399754: Fix strict type errors in functional JavaScript tests
- #3399992: Fix strict type errors in test traits
- #3402007: Fix strict type errors in test modules
- Fix strict type errors in miscellaneous test classes
Decide on the standard for the declaration
#3303206: Define a standard for adding declare(strict_types=1)
Add declaration
Once each of the above issues are fixed we can work on the corresponding issue to add the declaration. Currently cspell fails when there are over 1000 files touched in a single MR, so splitting these up helps with this also.
- #3403711: Add declare(strict_types=1) to all Build tests
- #3399373: Add declare(strict_types=1) to all Unit tests
- #3399388: Add declare(strict_types=1) to all Kernel tests
- #3399746: Add declare(strict_types=1) to all Functional tests
- #3400018: Add declare(strict_types=1) to all Functional JavaScript tests
- #3401994: Add declare(strict_types=1) to all test traits
- #3400334: Add declare(strict_types=1) to all test modules
- #3407793: Add declare(strict_types=1) to all miscellaneous test classes
Enforce strict types
Remaining tasks
User interface changes
N/A
API changes
Data model changes
N/A
Release notes snippet
Drupal core automated tests and test APIs require strict typing. Tests must add the line declare(strict_types=1); after the opening php tag.
Issue fork drupal-3376057
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:
- 3376057-strict-types
changes, plain diff MR !5195
Comments
Comment #2
larowlanWe went all in on strict types at work and there's a grace period where you end up with a few TypeErrors at run time while you iron out stuff. And we already had phpstan at level 7 which is something core doesn't have the luxury of.
Just saying we need to be cautious here
Comment #3
longwaveDuplicate of #3250827: [policy, no patch] Use declare(strict_types=1) in new code?
Comment #4
longwaveFrom that issue, Symfony rejected the same idea: https://github.com/symfony/symfony/issues/28423
Comment #5
catchhmm #3250827: [policy, no patch] Use declare(strict_types=1) in new code was supposed to be for new code, but ended up discussing adding it to all existing code too. I think we might want to keep that as a policy issue (for eventual phpcs rule etc.) and move implementation over here?
https://externals.io/message/112230#112233 shows that the Symfony decision might be getting outdated, but it's going to depend on the next few PHP 8 and eventually 9 releases as to what happens. At the moment we hit new type deprecation errors every PHP 8 minor, some of which aren't caught by tests, however it's probably better that they're deprecation notices than fatal errors.
Maybe we could start with adding strict types to every test file? That would have caught my silly issue, and it's guaranteed not to break production sites.
Comment #6
larowlan+1 to adding them to all tests. Should we repurpose this issue for that and bring it under the existing meta?
Comment #7
catchYep let's do that.
Comment #8
mstrelan commentedWe can use
\Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRectorfor this.rector.php:
We may want to adjust the pattern as it includes test modules, fixtures etc which may or may not be in scope.
Comment #10
dwwThanks for automating this and opening the MR! Moving to NR, although beware -- the GitLab tab to review the diff says: "Changes 1000+" 😂
Comment #11
dwwOh hah, the GitLab pipeline is having all sorts of trouble trying to validate this MR, e.g, the cspell job dies thusly:
Not sure where to go from here. I imagine MRs touching this many files are rare enough that we don't want to go down the rabbit hole of trying to fix our tooling to handle this kind of change set.
However, I doubt we want to split this up into N different MRs so each one will actually pass.
Would love some feedback from a framework manager (or release manager) on how to proceed. Tagging for framework manager for now.
Thanks/sorry,
-Derek
Comment #12
mstrelan commentedI've disabled spellcheck for now and manually revert files that phpcs complained about (adding
declarebefore@file). We now have 78 tests to fix up!Comment #13
mstrelan commentedI misread the output, there were 78 tests that failed but around 3000 errors. This seems like it might need splitting up in to much smaller portions.
Comment #14
mstrelan commentedGiven unit tests alone require code fixes to 26 files I think we can split that off to a child issue, then determine what makes sense for remaining tests. I've opened #3397890: Fix strict type errors in unit tests for this.
Comment #15
catchYeah makes sense to split off any actual code changes then see how we're looking after that.
Comment #16
andypostThere's existing rector rule so the patch becomes reproducible and CR can give contrib maintainers a guide to follow
Comment #17
mstrelan commented@andypost yes it's described in #8. See related issue where unit tests are all fixed.
Comment #18
andypostI mean Gitlab CI needs a rector's run with subset of dirs (which already converted) to prevent regressions
This way will allow maintain a list of tests/files to convert or already converted and prevent regressions same time
Also it will allow to sort out tests which needs code changes to apply the rector
Comment #19
mstrelan commented@andypost I'm not sure about adding rector to GitLab CI. We can use the existing phpcs job with
\SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniffbut again, not sure about limiting that to just tests.@catch do you mean the child issues should only make the code fixes and we leave adding
declare(strict_types=1)to this parent issue? That would definitely make it easier to review those child issues.Comment #21
mstrelan commentedChanging to a meta, adding child issues to IS.
Comment #22
mstrelan commentedComment #23
mstrelan commentedComment #24
mstrelan commentedAdded #3400434: Enforce strict types in tests with phpcs config
Comment #25
mstrelan commentedComment #26
mstrelan commentedComment #27
mstrelan commentedComment #28
dwwPer @xjm in #3401994-13: Add declare(strict_types=1) to all test traits we're going with spaces around the =
Documenting that here and updating the title and summary accordingly.
Comment #29
larowlanIs that a decision for the coding standards committee? A change like that impacts more than core.
Comment #30
dwwProbably, yes, says one of the new members of that committee. 😂
We can still proceed with fixing strict type errors and such, but yeah, we probably shouldn't proceed with #3401994 until that's formally decided.
Comment #31
dwwAnd lo, such an issue already exists: #3303206: Define a standard for adding declare(strict_types=1). Adding that as a new phase to the summary.
Comment #32
mstrelan commentedComment #33
mstrelan commentedComment #34
xjmBelated "oopsie" as the committed changes should have been tagged for release notes due to the impact on tests and the new phpcs rules (it is my fault for not catching this). We'll need a similar note again in 10.3 for the rest of the conversions, so adding it here in a minute..
Comment #35
xjmAnd I guess a lightweight CR for that matter.
Comment #36
xjmAdded the draft snippet. It can be iterated on when we create the CR and when we finish all the conversions for 10.3.
Comment #37
xjmIt looks like the child issues are not complete yet, so let's move this to the next pair of minor releases.
Comment #38
mstrelan commentedDown to the last issue #3400434: Enforce strict types in tests. Added a CR.
Comment #39
quietone commentedI was looking at issues tagged for some manager review. 10 months ago this was tagged, 'Needs framework manager review' for problem with the size of the MR. In the interim that has been resolved using child issues to scope the work. As @mstrelan points out in #38, this is now down to the last issue so I think it is safe to remove the tag.
Comment #40
mstrelan commentedAll the child issues are complete. Marking NR rather than Fixed as there are release notes and a change record to be finalised.
Comment #41
smustgrave commentedReading the notes and CR they're pretty straight forward.
Comment #42
quietone commentedI moved the CR from here to the issue that enabled the change in phpcs.xml.dist and published it. I updated the release note snippet.
Thanks everyone for completing this in under 1 year!
Comment #44
quietone commentedThis wasn't completed for 10.4.0.
Comment #45
xjmAmending attribution.
Comment #46
quietone commentedUpdate version to the branch this applies to.