Using the Drupal standard I am getting a false positive

$getMapOnlyLatestRevision = function ($tableName, $baseField, $idField, $preValidIds = []) use ($db): ?array {
  if (empty($tableName) || empty($baseField) || empty($idField)) {
    return NULL;
  }
  $ids = [];
  $map = [];
  if (empty($preValidIds)) {
    $ids = $db->query("SELECT MAX($idField) FROM $tableName GROUP BY $baseField")->fetchCol();
  }
  else {
    $ids = $db->query("SELECT MAX($idField) FROM $tableName WHERE $idField IN (:ids[]) GROUP BY $baseField", [
      ':ids[]' => $preValidIds,
    ])->fetchCol();
  }
  if (empty($ids)) {
    return NULL;
  }
  $map = $db->query("SELECT * FROM $tableName WHERE $idField IN (:ids[])", [
    ':ids[]' => $ids,
  ])->fetchAllAssoc($idField);
  return $map;
};

This results in a false positive error of: Non-namespaced classes/interfaces/traits should not be referenced with use statements

CommentFileSizeAuthor
#7 coder-3217297-fix.patch9.82 KBapolitsin

Issue fork coder-3217297

Command icon 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:

Comments

chriscalip created an issue. See original summary.

chriscalip’s picture

Issue summary: View changes
apolitsin’s picture

Got the same problem on any `use` code

namespace Drupal\my\Controller;
use Drupal\Core\Controller\ControllerBase;

/**
 * Controller Page.
 */
class MyPage extends ControllerBase {
}
-----------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------
 1 | ERROR | [x] Non-namespaced classes/interfaces/traits should not be referenced with use statements
-----------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------
apolitsin’s picture

apolitsin’s picture

QuickFix:

    git clone https://git.drupalcode.org/project/coder.git && \
    rm ./coder/coder_sniffer/Drupal/Sniffs/Classes/UseGlobalClassSniff.php && \
    rm ./coder/tests/Drupal/Classes/UseGlobalClassUnitTest.inc && \
    rm ./coder/tests/Drupal/Classes/UseGlobalClassUnitTest.inc.fixed && \
    rm ./coder/tests/Drupal/Classes/UseGlobalClassUnitTest.php 

apolitsin’s picture

StatusFileSize
new9.82 KB

  • klausi authored e4e05ca7 on 8.3.x
    fix(UseGlobalClass): Fix false positive with closure use statements (#...
klausi’s picture

Version: 8.x-3.x-dev » 8.3.x-dev
Status: Active » Fixed

@chriscalip thanks for reporting, I pushed a fix.

@APolitsin I could not reproduce any problem with your example code. Can you tray again with most recent Coder and PHPCS releases?

Status: Fixed » Closed (fixed)

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