Skip to content

Commit e4e05ca

Browse files
authored
fix(UseGlobalClass): Fix false positive with closure use statements (#3217297)
1 parent 0f9e181 commit e4e05ca

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

coder_sniffer/Drupal/Sniffs/Classes/UseGlobalClassSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
/**
1617
* Checks non-namespaced classes are referenced by FQN, not imported.
@@ -54,6 +55,12 @@ public function process(File $phpcsFile, $stackPtr)
5455

5556
$tokens = $phpcsFile->getTokens();
5657

58+
// Make sure this is not a closure USE group.
59+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
60+
if ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
61+
return;
62+
}
63+
5764
// Find the first declaration, marking the end of the use statements.
5865
$bodyStart = $phpcsFile->findNext([T_CLASS, T_INTERFACE, T_TRAIT, T_FUNCTION], 0);
5966

tests/Drupal/Classes/UseGlobalClassUnitTest.inc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,26 @@ class Example extends Test implements Test, Alias, MultiLineAlias {
8585
abstract public function test9(): ?Test;
8686

8787
}
88+
89+
$getMapOnlyLatestRevision = function ($tableName, $baseField, $idField, $preValidIds = []) use ($db): ?array {
90+
if (empty($tableName) || empty($baseField) || empty($idField)) {
91+
return NULL;
92+
}
93+
$ids = [];
94+
$map = [];
95+
if (empty($preValidIds)) {
96+
$ids = $db->query("SELECT MAX($idField) FROM $tableName GROUP BY $baseField")->fetchCol();
97+
}
98+
else {
99+
$ids = $db->query("SELECT MAX($idField) FROM $tableName WHERE $idField IN (:ids[]) GROUP BY $baseField", [
100+
':ids[]' => $preValidIds,
101+
])->fetchCol();
102+
}
103+
if (empty($ids)) {
104+
return NULL;
105+
}
106+
$map = $db->query("SELECT * FROM $tableName WHERE $idField IN (:ids[])", [
107+
':ids[]' => $ids,
108+
])->fetchAllAssoc($idField);
109+
return $map;
110+
};

tests/Drupal/Classes/UseGlobalClassUnitTest.inc.fixed

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,26 @@ class Example extends \Test implements \Test, \Test1, \MultiLine {
8080
abstract public function test9(): ?\Test;
8181

8282
}
83+
84+
$getMapOnlyLatestRevision = function ($tableName, $baseField, $idField, $preValidIds = []) use ($db): ?array {
85+
if (empty($tableName) || empty($baseField) || empty($idField)) {
86+
return NULL;
87+
}
88+
$ids = [];
89+
$map = [];
90+
if (empty($preValidIds)) {
91+
$ids = $db->query("SELECT MAX($idField) FROM $tableName GROUP BY $baseField")->fetchCol();
92+
}
93+
else {
94+
$ids = $db->query("SELECT MAX($idField) FROM $tableName WHERE $idField IN (:ids[]) GROUP BY $baseField", [
95+
':ids[]' => $preValidIds,
96+
])->fetchCol();
97+
}
98+
if (empty($ids)) {
99+
return NULL;
100+
}
101+
$map = $db->query("SELECT * FROM $tableName WHERE $idField IN (:ids[])", [
102+
':ids[]' => $ids,
103+
])->fetchAllAssoc($idField);
104+
return $map;
105+
};

0 commit comments

Comments
 (0)