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

File /core/modules/system/lib/Drupal/system/Tests/Database/SelectTest.php

Line 28: Unused local variable $name_field
Line 29: Unused local variable $age_field
Line 33: Unused local variable $record
Line 45: Unused local variable $name_field
Line 46: Unused local variable $age_field
Line 50: Unused local variable $record
Line 66: Unused local variable $name_field
Line 67: Unused local variable $age_field
Line 71: Unused local variable $record

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chertzog’s picture

Closing the following as duplicates:

#2080113: Remove Unused local variable $plugin_id from /core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorLanguageTest.php
#2080115: Remove Unused local variable $cached from /core/modules/system/lib/Drupal/system/Tests/Cache/GenericCacheBackendUnitTestBase.php
#2080117: Add assertion to Drupal/system/Tests/Database/DeleteTruncateTest.php
#2080119: Remove Unused local variable $result from /core/modules/system/lib/Drupal/system/Tests/Database/InsertDefaultsTest.php
#2080121: Remove Unused local variable $ringo from /core/modules/system/lib/Drupal/system/Tests/Database/DatabaseTestBase.php
#2080317: Remove Unused local variable $task_field from /core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php
#2080289: Remove Unused local variable $name_field from /core/modules/system/lib/Drupal/system/Tests/Database/SelectOrderedTest.php
#2080291: Remove Unused local variable $john from /core/modules/system/lib/Drupal/system/Tests/Database/CaseSensitivityTest.php
#2080295: Remove Unused local variable $ret from /core/modules/system/lib/Drupal/system/Tests/Database/SchemaTest.php
#2080299: Remove Unused local variable $record from /core/tests/Drupal/Tests/Core/Database/EmptyStatementTest.php
#2080303: Remove Unused local variable $log from /core/modules/system/lib/Drupal/system/Tests/Database/LoggingTest.php
#2080305: Remove Unused local variable $records from /core/modules/system/lib/Drupal/system/Tests/Database/FetchTest.php
#2080309: Remove Unused local variable $record from /core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php

chertzog’s picture

Status: Active » Needs review
FileSize
15.53 KB

Status: Needs review » Needs work

The last submitted patch, 2080311-remove-unused-variables.patch, failed testing.

chertzog’s picture

Status: Needs work » Needs review
rhm5000’s picture

Noticed some code that is using a while or for loop:
Example One

diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php
index a5d08e8..70ac5bc 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/AlterTest.php
@@ -34,7 +34,7 @@ function testSimpleAlter() {
     $result = $query->execute();
 
     $num_records = 0;
-    foreach ($result as $record) {
+    while ($result) {
       $num_records++;
     }

Example two

diff --git a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php
index a997a34..e9ba813 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Database/SelectComplexTest.php
@@ -154,13 +150,11 @@ function testGroupByAndHaving() {
    */
   function testRange() {
     $query = db_select('test');
-    $name_field = $query->addField('test', 'name');
-    $age_field = $query->addField('test', 'age', 'age');
     $query->range(0, 2);
     $result = $query->execute();
 
     $num_records = 0;
-    foreach ($result as $record) {
+    for ($i=0; $i < count($result); $i++) {
       $num_records++;
     }

to perform what the count function does. Updated patch with count function.

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

The last submitted patch, 2080311-remove-unused-local-variables-5.patch, failed testing.

rhm5000’s picture

Status: Needs work » Needs review

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

The last submitted patch, 2080311-remove-unused-local-variables-5.patch, failed testing.

pfrenssen’s picture

Why did you lump all these separate issues together like this? This greatly complicates the review process.

angel.h’s picture

Although sometimes joining issues is effective, the issues closed as duplicates shouldn't have been closed for the following reasons:
0. It is easier for new people to start contributing to core with simple tasks which include changing one file and a few lines.
1. It's much easier to review them.
2. Sometimes small changes can lead to bigger complications, for example the code might now need test (see #2072597: Remove Unused local variables from tests in the Views module).

This has been discussed with YesCT at the core sprint at DrupalCon Prague.

For these reasons I'll return the issues to their previous status.
This issue should be done only regarding the file it concerns.

rhm5000’s picture

Status: Needs work » Needs review
FileSize
1.53 KB

Addressing #10. Patch addressing issue #2080311 separately.

pfrenssen’s picture

The unused $record variables were not yet removed. Removed them in this patch.

The last submitted patch, 2080311-remove-unused-local-12.patch, failed testing.

rhm5000’s picture

Status: Needs work » Needs review
areke’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

The patch from #12 looks good; it keeps the tests while removing the unused variables and effectively gets rid of useless while loops. RTBC.

chx’s picture

This, however, removes the actual counting of records and relies on rowCount working properly. Do we test that?

YesCT’s picture

maybe we should use an approach like in #5: count($result) ?

chx’s picture

Status: Reviewed & tested by the community » Needs work

Sounds a plan to me. You could do even do both the rowCount and the count() to test rowCount() while at it.

areke’s picture

Status: Needs work » Needs review
FileSize
2.75 KB

How's this?

Status: Needs review » Needs work

The last submitted patch, 19: 2080311-19.patch, failed testing.

xjm’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2080343: Remove Unused local variables from system module
xjm’s picture

Title: Remove Unused local variable $name_field from /core/modules/system/lib/Drupal/system/Tests/Database/SelectTest.php » Clean up Drupal\system\Tests\Database\SelectTest.php
Component: other » database system
Status: Closed (duplicate) » Needs work

Actually this looks like another one that could stand to have its own separate review. Thanks!

Rajesh Ashok’s picture

Status: Needs work » Fixed

Tested via Netbeans. Currently there are no unused variables at Drupal\system\Tests\Database\SelectTest.php So closing this issue.

Rajesh Ashok’s picture

Assigned: Unassigned » Rajesh Ashok
Status: Fixed » Needs work

Sorry, made a mistake there and so reopening the issue.

Rajesh Ashok’s picture

Assigned: Rajesh Ashok » Unassigned
Status: Needs work » Needs review
FileSize
1.53 KB
1.53 KB

As per #10, I have fixed only unused variables in Drupal\system\Tests\Database\SelectTest.php
As per #16, I did not replace the code with rowCount function to get number of records returned by the query.

ie. Did not replace the following code with rowCount function

$num_records = 0;
foreach ($result as $record) {
  $num_records++;
}

The last submitted patch, 25: removed-unused-variables-2080311-25.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 25: removed-unused-variables-2080311-25.patch, failed testing.

InternetDevels’s picture

I've changed "while" cycles to the count and added "fetchAll". First I've planned to use rowCount() but according to this issue: #2146733: Select queries should not use rowCount() to calculate number of rows it shouldn't be used for "Select" queries.

amitgoyal’s picture

Status: Needs review » Needs work

The patch no longer applies as the file core/modules/system/lib/Drupal/system/Tests/Database/SelectTest.php does not exist there.

natemow’s picture

Re-rolled patch per new drupal/core/modules/system/src/Tests/Database/SelectTest.php path.

natemow’s picture

Status: Needs work » Needs review
amitgoyal’s picture

Status: Needs review » Reviewed & tested by the community

#30 looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/system/src/Tests/Database/SelectTest.php
@@ -43,19 +38,16 @@ function testSimpleSelect() {
+    $num_records = $result->fetchAll();

@@ -64,19 +56,16 @@ function testSimpleComment() {
+    $num_records = $result->fetchAll();

$num_records is now an weird name $records would be better.

amitgoyal’s picture

Status: Needs work » Needs review
FileSize
2.8 KB
1.45 KB

Please review updated patch with fixes in #33.

cedricpriestley’s picture

This patch still applies.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Looks nice!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7fe5f2a and pushed to 8.x. Thanks!

  • alexpott committed 7fe5f2a on 8.x
    Issue #2080311 by rhm50, amitgoyal, InternetDevels, Rajesh Ashok,...

Status: Fixed » Closed (fixed)

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