Skip to content

Commit 572bb1d

Browse files
authored
Fix file global function detection (#34)
* Add test case for calling a defined function * Always look in the global scope * Only look for defined functions for function calls
1 parent ca82714 commit 572bb1d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

ImportDetection/Sniffs/Imports/RequireImportsSniff.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private function isSymbolDefined(File $phpcsFile, Symbol $symbol): bool {
157157
return $this->isNamespaceImported($phpcsFile, $namespace);
158158
}
159159
// If the symbol has no namespace and is itself is imported or defined, ignore it
160-
return $this->isNamespaceImportedOrDefined($phpcsFile, $symbol->getName(), $symbol->getSymbolConditions());
160+
return $this->isNamespaceImportedOrDefined($phpcsFile, $symbol);
161161
}
162162

163163
private function isNamespaceImported(File $phpcsFile, string $namespace): bool {
@@ -168,12 +168,23 @@ private function isNamespaceImported(File $phpcsFile, string $namespace): bool {
168168
);
169169
}
170170

171-
private function isNamespaceImportedOrDefined(File $phpcsFile, string $namespace, array $conditions): bool {
171+
private function isSymbolAFunctionCall(File $phpcsFile, Symbol $symbol): bool {
172+
$tokens = $phpcsFile->getTokens();
173+
$stackPtr = $symbol->getSymbolPosition();
174+
if (isset($tokens[$stackPtr + 1]) && $tokens[$stackPtr + 1]['type'] === 'T_OPEN_PARENTHESIS') {
175+
return true;
176+
}
177+
return false;
178+
}
179+
180+
private function isNamespaceImportedOrDefined(File $phpcsFile, Symbol $symbol): bool {
181+
$namespace = $symbol->getName();
182+
$conditions = $symbol->getSymbolConditions();
172183
return (
173184
$this->isClassImported($phpcsFile, $namespace)
174185
|| $this->isClassDefined($phpcsFile, $namespace)
175186
|| $this->isFunctionImported($phpcsFile, $namespace)
176-
|| $this->isFunctionDefined($phpcsFile, $namespace, $conditions)
187+
|| $this->isFunctionDefined($phpcsFile, $symbol, $namespace, $conditions)
177188
|| $this->isConstImported($phpcsFile, $namespace)
178189
|| $this->isConstDefined($phpcsFile, $namespace)
179190
);
@@ -250,17 +261,20 @@ private function isClassDefined(File $phpcsFile, string $className): bool {
250261
return false;
251262
}
252263

253-
private function isFunctionDefined(File $phpcsFile, string $functionName, array $conditions): bool {
264+
private function isFunctionDefined(File $phpcsFile, Symbol $symbol, string $functionName, array $conditions): bool {
254265
$tokens = $phpcsFile->getTokens();
266+
267+
if (! $this->isSymbolAFunctionCall($phpcsFile, $symbol)) {
268+
return false;
269+
}
270+
255271
$scopesToEnter = array_filter(array_keys($conditions), function ($conditionPtr) use ($conditions) {
256272
return $conditions[$conditionPtr] === T_FUNCTION;
257273
});
258274
$this->debug("looking for definition for function {$functionName}");
259275
$this->debug("my conditions are " . json_encode($conditions));
260276
$this->debug("scopes to enter " . implode(',', $scopesToEnter));
261-
if (empty($scopesToEnter)) {
262-
return false;
263-
}
277+
264278
// Only look at the inner-most scope and global scope
265279
$scopesToEnter = [end($scopesToEnter), 0];
266280

tests/Sniffs/Imports/WordPressFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ public function test_additional_constants() {
7878

7979
function foo() {}
8080
add_action('init', foo);
81+
foo();

0 commit comments

Comments
 (0)