Problem/Motivation

Part of meta-issue #2002650: [meta, no patch] improve maintainability by removing unused local variables

When this issue was originally opened it listed a number of unused local variables. They have been removed via patches in other issues. The remaining unused variables are:

File core/modules/node/lib/Drupal/node/NodeForm.php (previously named NodeFormController.php)
Line 325: Unused variable $account.

File core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php
Line 92: Unused variable $changed.

File core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php
Line 32: Unused variable $forum_disable.

Proposed resolution

Patch in #56 removes the last few unused local variables to resolve the issue.

Remaining tasks

User interface changes

None.

API changes

None.

Original report by @jlindsey15

File /core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php
Line 92: Unused local variable $changed

File core/modules/node/lib/Drupal/node/NodeFormController.php
Line 325: Unused variable $account.

File core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php
Line 92: Unused variable $changed.

File core/modules/node/node.tokens.inc
Line 136: Unused variable $field_langcode.

File core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php
Line 32: Unused variable $forum_disable.

File core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php
Line 47: Unused variable $instance.
Line 95: Unused variable $instance.

File core/modules/node/lib/Drupal/node/Plugin/Search/NodeSearch.php
Line 408: Unused variable $node_types.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jlindsey15’s picture

Status: Active » Needs review
FileSize
513 bytes
jlindsey15’s picture

Category: task » bug
Priority: Normal » Minor

Status: Needs review » Needs work
Issue tags: -Novice

The last submitted patch, unused-local-variables-2064639-1.patch, failed testing.

jlindsey15’s picture

Status: Needs work » Needs review
Issue tags: +Novice
sergeypavlenko’s picture

Assigned: jlindsey15 » Unassigned
Status: Needs review » Reviewed & tested by the community

All is well.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Actually, in this case, I wonder if this is actually highlighting a missing test below? And if not, then we should adjust the comment above because we are no longer preserving timestampS.

akshaan’s picture

Assigned: Unassigned » akshaan
CarlHinton’s picture

Simply removing the s would resolve #6

    // Store the timestamp.
    $created = $node->getCreatedTime();

    $node->save();
CarlHinton’s picture

A patch matching #8 is attached

rhm5000’s picture

Status: Needs work » Needs review
Anonymous’s picture

#9 still applies OK however as @webchick suggests in comment #6 I wonder if this is missing a test. The code is as follows:

    // Store the timestamps.
    $created = $node->getCreatedTime();
    $changed = $node->getChangedTime();

    $node->save();
    $node = $this->drupalGetNodeByTitle($edit['title'], TRUE);
    $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');

The proposed test would be:

    $this->assertNotEqual($node->getChangedTime(), $changed, 'Updating a node does not preserve "changed" timestamp.');

Also perhaps I am not understanding node_presave but I am unsure as to whether the code directly following the above does do what it is intended to do as we are already using $node and just changing the title:

    // Programmatically set the timestamps using hook_node_presave.
    $node->title = 'testing_node_presave';

    $node->save();
    $node = $this->drupalGetNodeByTitle('testing_node_presave', TRUE);
    $this->assertEqual($node->getCreatedTime(), 280299600, 'Saving a node uses "created" timestamp set in presave hook.');
    $this->assertEqual($node->getChangedTime(), 979534800, 'Saving a node uses "changed" timestamp set in presave hook.');
Anonymous’s picture

FileSize
761 bytes

Here's a patch for what I'm suggesting in #11

Status: Needs review » Needs work

The last submitted patch, 2064639-12.patch, failed testing.

Anonymous’s picture

OK I'm stuck on this as I can't figure out how to change something as my tests keep breaking locally - perhaps this is why the $change was not used as they had similar problems.

rahul.shinde’s picture

Assigned: akshaan » rahul.shinde
Issue summary: View changes
rahul.shinde’s picture

Patch re-rolled.

rahul.shinde’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: 2064639-16-unused_variables_renamed.patch, failed testing.

xjm’s picture

Title: Remove Unused local variable $changed from /core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php » Remove unused local variables from the node module

Please check that all other unused local variables are removed from the node module as well, and add those to this patch.

rahul.shinde’s picture

rahul.shinde’s picture

@xjm, I'll be looking into other unused variables. I missed out your comment before uploading patch.

rahul.shinde’s picture

Status: Needs work » Needs review

Adding patch for test queue.

xjm’s picture

Thanks @rahul.shinde!

rahul.shinde’s picture

Issue summary: View changes
rahul.shinde’s picture

Issue summary: View changes
Status: Needs review » Needs work
rahul.shinde’s picture

Status: Needs work » Needs review
FileSize
4.66 KB

@xjm, Adding patch to remove/update unused variables for node module.

Anonymous’s picture

I have just tested this patch and it applies cleanly to latest HEAD.

Before applying the patch node module has six unused variables, after it has zero so does the job.

parthipanramesh’s picture

Status: Needs review » Reviewed & tested by the community

Works. Thank you very much!

webchick’s picture

Thanks; this is close... however, it's not clear to me why 979534800 was chosen as the comparison value here:

+++ b/core/modules/node/lib/Drupal/node/Tests/NodeSaveTest.php
@@ -94,6 +94,7 @@ function testTimestamps() {
     $this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');
+    $this->assertNotEqual(979534800, $changed, 'Updating a node does not preserve "changed" timestamp.');

Does replacing 979534800 with $node->getUpdatedTime() per stevepurkiss in #11 not work?

webchick’s picture

Status: Reviewed & tested by the community » Needs work
superspring’s picture

Assigned: rahul.shinde » Unassigned
Status: Needs work » Needs review
FileSize
4.67 KB

I've changed the "979534800" line of code as it isn't actually testing anything.

Status: Needs review » Needs work

The last submitted patch, 31: 2064639-31-unused_variables_removed.patch, failed testing.

Anonymous’s picture

I have read this again and again but unsure what to do to resolve. Thought I'd post in case you thought I was ignoring - am not, just unsure how to progress.

InternetDevels’s picture

Problem is this check:

$this->assertEqual($node->getCreatedTime(), $created, 'Updating a node preserves "created" timestamp.');

this check can't be performed here, because preSave uses REQUEST_TIME for 'changed' property update and this constant does not change during request.

  public function preSave(EntityStorageControllerInterface $storage_controller) {
    parent::preSave($storage_controller);

    // Before saving the node, set changed and revision times.
    $this->changed->value = REQUEST_TIME;
  }

So we need to move this check to another test or change logic of the preSave() method and use time() instead.

In the attached patch I simply removed this check.

InternetDevels’s picture

Oops, I've attached wrong files.

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

as part of the toronto sprint we reviewed the patch in comment #35 - all identified unused local variables have been removed.

looks good.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

node-remove-unused-variables-2064639-35.patch no longer applies.

error: patch failed: core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php:44
error: core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php: patch does not apply

Salah Messaoud’s picture

Status: Needs work » Needs review
FileSize
3.32 KB

rerolled

alexpott’s picture

Status: Needs review » Needs work
+++ b/core/modules/node/lib/Drupal/node/Tests/NodeTokenReplaceTest.php
@@ -67,6 +67,7 @@ function testNodeTokenReplacement() {
     $node->save();
 
+
     // Generate and test sanitized tokens.

Not need for this empty line :)

Salah Messaoud’s picture

Status: Needs work » Needs review
FileSize
2.82 KB
311 bytes

Sorry I didn't notice the extra line, I fixed it and added interdiff

sandipmkhairnar’s picture

Thanks Salah. I have tested patch and its working fine for me.

vlad.n’s picture

Rerolled. This patch applies cleanly at commit 428d0f0.

Status: Needs review » Needs work

The last submitted patch, 42: node-remove-unused-variables-2064639-42.patch, failed testing.

longwave’s picture

Status: Needs review » Needs work

The last submitted patch, 44: 2064639-remove-unused-vars-node-44.patch, failed testing.

longwave’s picture

longwave’s picture

Status: Needs work » Needs review
andrei.dincu’s picture

I have removed unused variables from the following files:

File core/modules/node/lib/Drupal/node/NodeFormController.php
Line 325: Unused variable $account.

File core/modules/node/lib/Drupal/node/NodeFormController.php
Line 325: Unused variable $account.

File core/modules/node/lib/Drupal/node/Tests/NodeTypePersistenceTest.php
Line 32: Unused variable $forum_disable.

File core/modules/node/node.tokens.inc
Line 136: Unused variable $field_langcode.

martin107’s picture

FileSize
829 bytes

providing interdiff .. all looks ok maybe other committed patch has fix identical issues.

droplet’s picture

Status: Needs review » Needs work

one more ?
Location      file [drupal8x] - ...\core\modules\node\lib\Drupal\node\Controller\NodeController.php   Problem synopsis      Unused local variable $uri (at line 132)

jayeshanandani’s picture

Status: Needs work » Needs review
FileSize
3.46 KB
667 bytes

Removed Unused local variable $uri (at line 132) at core\modules\node\lib\Drupal\node\Controller\NodeController.php.

LinL’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Patch no longer applies, tagging for reroll.

martin107’s picture

Status: Needs work » Needs review
FileSize
2.81 KB

reroll ( Change: The removed variable in NodeController.php has already beeen removed in HEAD )

andrei.dincu’s picture

Patch applied cleanly.
Rerolled done.

martin107’s picture

Issue tags: -Needs reroll
connorwk’s picture

Re-rolled because of:
PSR-4
#2116363: Unified repository of field definitions (cache + API) removed one of the unused variables a previous patch removed in node.token.inc
#2238077: Rename EntityFormController to EntityForm renamed one of the files that had an unused variable (NodeFormController.php to NodeForm.php)

I'm unsure why but phpStorm doesn't say that $account is an unused variable although it clearly is and was removed with a previous patch. This line is in NodeForm.php line 312 if anyone else wants to check it out.
- if (!empty($form_state['values']['uid']) && !($account = user_load_by_name($form_state['values']['uid']))) {
+ if (!empty($form_state['values']['uid']) && !user_load_by_name($form_state['values']['uid'])) {

All the unused variables in the node module seem to be removed now via phpStorm's inspect code on the node module.

My first patch at the Austin sprint!

alimac’s picture

I ran Inspect Code in PhpStorm on latest of 8.x. It found 2 unused local variables: $forum_enable, $created.

I applied the patch from #56 and ran Inspect Code again. This time it did not find any unused local variables. So, it looks to me that the patch works and does what it's supposed to.

alimac’s picture

Status: Needs review » Reviewed & tested by the community

Marking as RTBC.

alimac’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 5417fcf and pushed to 8.x. Thanks!

  • Commit 5417fcf on 8.x by alexpott:
    Issue #2064639 by rahul.shinde, InternetDevels, martin107, longwave,...

Status: Fixed » Closed (fixed)

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