Skip to content

Commit 454d59a

Browse files
Add fix and tests
1 parent 49a82bc commit 454d59a

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

SymfonyCustom/Sniffs/PHP/ImportInternalFunctionSniff.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use function array_filter;
1212
use function array_map;
13+
use function get_defined_functions;
1314
use function in_array;
1415
use function mb_strtolower;
1516

@@ -93,22 +94,29 @@ private function processString(File $phpcsFile, int $stackPtr, array $functionUs
9394
}
9495

9596
if (isset($ignore[$tokens[$prevToken]['code']])) {
96-
// Not a call to a PHP function.
97+
// Not a call to a function.
9798
return;
9899
}
99100

100101
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
101102
if (isset($ignore[$tokens[$nextToken]['code']])) {
102-
// Not a call to a PHP function.
103+
// Not a call to a function.
103104
return;
104105
}
105106

106107
if (T_STRING === $tokens[$stackPtr]['code'] && T_OPEN_PARENTHESIS !== $tokens[$nextToken]['code']) {
107-
// Not a call to a PHP function.
108+
// Not a call to a function.
108109
return;
109110
}
110111

111112
$function = mb_strtolower($tokens[$stackPtr]['content']);
113+
114+
$internalFunctions = get_defined_functions()['internal'];
115+
if (!in_array($function, $internalFunctions)) {
116+
// Not a call to a PHP function.
117+
return;
118+
}
119+
112120
if (!in_array($function, $functionUses)) {
113121
$phpcsFile->addError(
114122
'PHP internal function "%s" must be imported',
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bar;
4+
5+
use function array_values;
6+
7+
function my_function(array $array) {}
8+
9+
my_function([]);
10+
array_values([]);
11+
array_unique([]);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SymfonyCustom\Tests\PHP;
6+
7+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
8+
9+
/**
10+
* Unit test class for the ImportInternalFunction sniff.
11+
*/
12+
class ImportInternalFunctionUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* @return array<int, int>
16+
*/
17+
protected function getErrorList(): array
18+
{
19+
return [
20+
11 => 1,
21+
];
22+
}
23+
24+
/**
25+
* @return array<int, int>
26+
*/
27+
protected function getWarningList(): array
28+
{
29+
return [];
30+
}
31+
}

0 commit comments

Comments
 (0)