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);
   }

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Hardik_Patel_12 created an issue. See original summary.

Hardik_Patel_12’s picture

Status: Active » Needs review
apaderno’s picture

Issue summary: View changes
apaderno’s picture

Status: Needs review » Reviewed & tested by the community

In that class, $expected is initialized only in two methods. For what I can see, that is the only unused variable in that class.

alexpott’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
Related issues: +#3106216: Remove unused variables from core

Thank 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.

alexpott’s picture

Status: Closed (duplicate) » Needs work

In 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. Also git blame can be useful as well.

apaderno’s picture

Basing on git blame, the following code was added in #2263569: Bypass form caching by default for forms using #ajax..

    $this->renderer->expects($this->never())
      ->method('renderResponse');

    $request = new Request();
    $form = [];
    $form_state = new FormState();
    $commands = [];

    $expected = [];

Instead, the following line (the last line on that method) was added in #2822837: Replace @expectedException @expectedExceptionMessage with $this->setExpectedException.

$this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);

testBuildResponseNoTriggeringElement() doesn't have any expected result, as it is just checking the renderResponse() method is never called and the HttpException exception is instead thrown.

  public function testBuildResponseNoTriggeringElement() {
    $this->renderer->expects($this->never())
      ->method('renderResponse');

    $request = new Request();
    $form = [];
    $form_state = new FormState();
    $commands = [];

    $expected = [];
    $this->expectException(HttpException::class);
    $this->formAjaxResponseBuilder->buildResponse($request, $form, $form_state, $commands);
  }

For this reason, I think the patch provided here is correct.

longwave’s picture

Status: Needs work » Reviewed & tested by the community

Agree with @kiamlaluno's analysis, both instances were not spotted during the refactor, so this patch is valid and RTBC.

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed 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.

  • alexpott committed 0f823ff on 9.1.x
    Issue #3158292 by Hardik_Patel_12, kiamlaluno: Remove unused variables...

  • alexpott committed d59fac0 on 9.0.x
    Issue #3158292 by Hardik_Patel_12, kiamlaluno: Remove unused variables...

  • alexpott committed e0a0f48 on 8.9.x
    Issue #3158292 by Hardik_Patel_12, kiamlaluno: Remove unused variables...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.