Problem/Motivation
Part of #3404246: [META] Fix strict type errors detected by phpstan.
There are 16 violations where a parameter of a function expects an int but receives a string. Most of the time this is a documentation issue, where the function actually should take a string, or can take a numeric string, but a handful of times it should take an int that we need to cast.
Steps to reproduce
Add checkFunctionArgumentTypes: true to the parameters section of phpstan.neon.dist
Run phpstan:
$ ./vendor/bin/phpstan analyse -c core/phpstan.neon.dist --error-format=raw | grep "expects int, string given"
Proposed resolution
Fix the param docs, or cast where necessary.
core/modules/comment/src/CommentManager.php:199:Parameter #2 $error_level of function trigger_error expects int, string given.
core/modules/file/src/FileAccessControlHandler.php:97:Parameter #3 $age of function file_get_file_references expects int, string given.
core/modules/file/src/Hook/FileDownloadHook.php:48:Parameter #3 $age of function file_get_file_references expects int, string given.
core/modules/help/tests/src/Functional/HelpTopicsSyntaxTest.php:341:Parameter #2 $value of static method Drupal\help_topics_twig_tester\HelpTestTwigNodeVisitor::setStateValue() expects int, string given.
core/modules/link/tests/src/Functional/LinkFieldTest.php:388:Parameter #1 $id of method Drupal\Tests\link\Functional\LinkFieldTest::renderTestEntity() expects int, string given.
core/modules/link/tests/src/Functional/LinkFieldTest.php:401:Parameter #1 $id of method Drupal\Tests\link\Functional\LinkFieldTest::renderTestEntity() expects int, string given.
core/modules/migrate_drupal_ui/src/Form/CredentialForm.php:444:Parameter #2 $drupal_version of method Drupal\migrate_drupal_ui\Form\MigrateUpgradeFormBase::getMigrations() expects int, string given.
core/modules/system/src/Controller/AssetControllerBase.php:181:Parameter #2 $group_delta of method Drupal\system\Controller\AssetControllerBase::getGroup() expects int, string given.
core/modules/views/src/Plugin/views/field/NumericField.php:184:Parameter #1 $count of static method Drupal\Core\StringTranslation\PluralTranslatableMarkup::createFromTranslatedString() expects int, string given.
core/modules/workspaces/src/WorkspaceMerger.php:66:Parameter #1 $seconds of function set_time_limit expects int, string given.
core/modules/workspaces/src/WorkspacePublisher.php:102:Parameter #1 $seconds of function set_time_limit expects int, string given.
core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php:484:Parameter #2 $term_csv_id of method Drupal\demo_umami_content\InstallHelper::getTermId() expects int, string given.
core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php:526:Parameter #2 $term_csv_id of method Drupal\demo_umami_content\InstallHelper::getTermId() expects int, string given.
core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php:584:Parameter #2 $term_csv_id of method Drupal\demo_umami_content\InstallHelper::getTermId() expects int, string given.
core/scripts/run-tests.sh:1645:Parameter #2 $color_code of function simpletest_script_print expects int, string given.
core/scripts/run-tests.sh:1646:Parameter #2 $color_code of function simpletest_script_print expects int, string given.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3557514
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:
- 3557514-int-string
changes, plain diff MR !13813
Comments
Comment #3
mstrelan commentedLeft some commentary on the MR
Comment #4
dcam commentedI ran PHPStan with the rule turned on. I only got 16 errors instead of the 19 mentioned in the IS. I don't know if other recent updates fixed a few of them. The MR corrected all of the issues PHPStan found.
Afterward I read through all of the changes. They all seem reasonable. I would leave the
intcasts in this MR rather than splitting them off, especially the ones from Workspaces. I left a couple of replies to the comments, agreeing with the assessments.This one's looking good to me. I'll RTBC it.
Comment #5
mstrelan commentedThanks for the review.
The following issues were fixed in #3554676: Fix more incorrect phpdoc type hints (part 3):
There is also a new error introduced by #3543035: Deprecate CommentManagerInterface::getCountNewComments:
core/modules/comment/src/CommentManager.php:199:Parameter #2 $error_level of function trigger_error expects int, string given.I've updated the issue summary and will rebase and fix the new issue.
Comment #6
mstrelan commentedComment #7
dcam commentedThe new change makes sense - that's a constant after all. After downloading the latest changes PHPStan reported no errors.
Comment #8
longwaveShould we add this permanently?
Comment #9
mstrelan commented@longwave yes that's what the meta is for, there are still a few hundred violations I'm trying to solve first to keep the baseline down.
It's on by default in level 5 or 6 but we can benefit from using it manually.
Comment #10
borisson_This it looks very good and it will be helpful to get this in. I am not 100% sure about the fact that we've added type casts as well as documentation changes here, but since there are only 2, to me it seems not to be an issue.
RTBC++
Comment #11
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 #12
mstrelan commentedRebased cleanly:
Note the skipped commit was fixed in #3563876: TypeError: trigger_error(): Argument #2 ($error_level) must be of type int, string given in trigger_error().
Comment #16
catchI think the casting is OK here. Committed/pushed to main and 11.x, thanks!