Skip to content

Commit

Permalink
Merge pull request #9002 from jack-worman/forbidden_functions_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Dec 24, 2022
2 parents 8b05f2e + 1bb9eb4 commit 58ae748
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dictionaries/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3697,7 +3697,7 @@
'get_declared_interfaces' => ['list<class-string>'],
'get_declared_traits' => ['list<class-string>|null'],
'get_defined_constants' => ['array<string,int|string|float|bool|null|array|resource>', 'categorize='=>'bool'],
'get_defined_functions' => ['array<string,list<callable-string>>', 'exclude_disabled='=>'bool'],
'get_defined_functions' => ['array{internal: list<callable-string>, user: list<callable-string>}', 'exclude_disabled='=>'bool'],
'get_defined_vars' => ['array'],
'get_extension_funcs' => ['list<callable-string>|false', 'extension'=>'string'],
'get_headers' => ['array|false', 'url'=>'string', 'associative='=>'int', 'context='=>'resource'],
Expand Down
2 changes: 1 addition & 1 deletion dictionaries/CallMap_historical.php
Original file line number Diff line number Diff line change
Expand Up @@ -11124,7 +11124,7 @@
'get_declared_interfaces' => ['list<class-string>'],
'get_declared_traits' => ['list<class-string>|null'],
'get_defined_constants' => ['array<string,int|string|float|bool|null|array|resource>', 'categorize='=>'bool'],
'get_defined_functions' => ['array<string,list<callable-string>>', 'exclude_disabled='=>'bool'],
'get_defined_functions' => ['array{internal: list<callable-string>, user: list<callable-string>}', 'exclude_disabled='=>'bool'],
'get_defined_vars' => ['array'],
'get_extension_funcs' => ['list<callable-string>|false', 'extension'=>'string'],
'get_headers' => ['array|false', 'url'=>'string', 'associative='=>'int'],
Expand Down
14 changes: 4 additions & 10 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2257,17 +2257,11 @@ public function getPredefinedFunctions(): array
public function collectPredefinedFunctions(): void
{
$defined_functions = get_defined_functions();

if (isset($defined_functions['user'])) {
foreach ($defined_functions['user'] as $function_name) {
$this->predefined_functions[$function_name] = true;
}
foreach ($defined_functions['user'] as $function_name) {
$this->predefined_functions[$function_name] = true;
}

if (isset($defined_functions['internal'])) {
foreach ($defined_functions['internal'] as $function_name) {
$this->predefined_functions[$function_name] = true;
}
foreach ($defined_functions['internal'] as $function_name) {
$this->predefined_functions[$function_name] = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

use function array_map;
use function extension_loaded;
use function implode;
use function in_array;
use function is_string;
use function strpos;
Expand All @@ -60,15 +59,15 @@
class NamedFunctionCallHandler
{
/**
* @param lowercase-string $function_id
* @param lowercase-string $function_id
*/
public static function handle(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
PhpParser\Node\Expr\FuncCall $stmt,
PhpParser\Node\Expr\FuncCall $real_stmt,
PhpParser\Node\Name $function_name,
?string $function_id,
string $function_id,
Context $context
): void {
if ($function_id === 'get_class'
Expand Down Expand Up @@ -298,17 +297,17 @@ public static function handle(
) {
IssueBuffer::maybeAdd(
new ForbiddenCode(
'Unsafe ' . implode('', $function_name->parts),
'Unsafe ' . $function_id,
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
}

if (isset($codebase->config->forbidden_functions[strtolower((string) $function_name)])) {
if (isset($codebase->config->forbidden_functions[$function_id])) {
IssueBuffer::maybeAdd(
new ForbiddenCode(
'You have forbidden the use of ' . $function_name,
'You have forbidden the use of ' . $function_id,
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
Expand Down
34 changes: 34 additions & 0 deletions tests/ForbiddenCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,40 @@ public function testForbiddenVarExportFunction(): void
$this->analyzeFile($file_path, new Context());
}

public function testNoExceptionWithMatchingNameButDifferentNamespace(): void
{
$this->project_analyzer = $this->getProjectAnalyzerWithConfig(
TestConfig::loadFromXML(
dirname(__DIR__, 2),
<<<'XML'
<?xml version="1.0"?>
<psalm>
<forbiddenFunctions>
<function name="strlen"/>
</forbiddenFunctions>
</psalm>
XML,
),
);
$file_path = getcwd() . '/src/somefile.php';
$this->addFile(
$file_path,
<<<'PHP'
<?php
namespace Foo {
function strlen(): int {
return 0;
}
}
namespace {
use function Foo\strlen;
strlen();
}
PHP,
);
$this->analyzeFile($file_path, new Context());
}

public function testForbiddenEmptyFunction(): void
{
$this->expectExceptionMessage('ForbiddenCode');
Expand Down

0 comments on commit 58ae748

Please sign in to comment.