Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
In the core/tests/Drupal/Tests/Core/Form/FormAjaxResponseBuilderTest.php file, testBuildResponseNoTriggeringElement()
and testBuildResponseNoCallable()
initialize $expected
but never use it.
Proposed resolution
Remove the unused $expected
.
public function testBuildResponseNoTriggeringElement() {
.....
$form_state = new FormState();
$commands = [];
- $expected = [];
$this->expectException(HttpException::class);
$this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
}
@@ -73,7 +72,6 @@ public function testBuildResponseNoCallable() {
.....
$form_state->setTriggeringElement($triggering_element);
$commands = [];
- $expected = [];
$this->expectException(HttpException::class);
$this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
}
Comment | File | Size | Author |
---|---|---|---|
unsused-variable-removed.patch | 951 bytes | Hardik_Patel_12 | |
Comments
Comment #2
Hardik_Patel_12 CreditAttribution: Hardik_Patel_12 at QED42 commentedComment #3
apadernoComment #4
apadernoIn that class,
$expected
is initialized only in two methods. For what I can see, that is the only unused variable in that class.Comment #5
alexpottThank you for your work on cleaning up Drupal core's code style!
In order to fix core coding standards in a maintainable way, all our coding standards issues should be done on a per-rule basis across all of core, rather than fixing standards in individual modules or files. We should also separate fixes where we need to write new documentation from fixes where we need to correct existing standards. This all should be done as part of #2571965: [meta] Fix PHP coding standards in core. A good place to for unused variables is #3106216: Remove unused variables from core.
For background information on why we usually will not commit coding standards fixes that aren't scoped in that way, see the core issue scope guidelines, especially the note about coding standards cleanups. That document also includes numerous suggestions for scoping issues including documentation coding standards cleanups.
Contributing to the overall plan above will help ensure that your fixes for core's coding standards remain in core the long term.
Comment #6
alexpottIn discussion with xim, catch and larowlan, my earlier comment is incorrect. We should handle each unused variable on its own merit and do the work to work out why it is not used.
This can point to broken code or incomplete testing see #3157369: Use unused variable $filters from DateTimeSchemaTest for example. A useful tool for this is
git log -S “SOME TEXT”
which will search git commits for matching text to find out when the variable might have become unused. Without doing the work to show why the variable is unused the patch will not be committed. Alsogit blame
can be useful as well.Comment #7
apadernoBasing on
git blame
, the following code was added in #2263569: Bypass form caching by default for forms using #ajax..Instead, the following line (the last line on that method) was added in #2822837: Replace @expectedException @expectedExceptionMessage with $this->setExpectedException.
testBuildResponseNoTriggeringElement()
doesn't have any expected result, as it is just checking therenderResponse()
method is never called and theHttpException
exception is instead thrown.For this reason, I think the patch provided here is correct.
Comment #8
longwaveAgree with @kiamlaluno's analysis, both instances were not spotted during the refactor, so this patch is valid and RTBC.
Comment #9
alexpottCommitted and pushed 0f823ffd28 to 9.1.x and d59fac03a9 to 9.0.x and e0a0f48bc7 to 8.9.x. Thanks!
@kiamlaluno thanks for doing the git archaeology. Nice work.
Backported to 8.9.x as this is test only and this keeps ours test code in-sync.