-
Notifications
You must be signed in to change notification settings - Fork 208
Add script to generate function map for static analysis tools #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b94591c
Add script to generate function map for static analysis tools
alcaeus aa72cef
Refactor function map generation script
alcaeus 8c1a23a
Remove workaround for unserialize methods
alcaeus 8031f45
Update list of phony make targets
alcaeus de4465d
Add section about generating function maps to contribution docs
alcaeus 4880238
Fix code style in function map generator
alcaeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
if (PHP_VERSION_ID < 80000) { | ||
echo 'This script requires PHP 8.0 or higher'; | ||
exit(1); | ||
} | ||
|
||
$filename = __DIR__ . '/functionmap.php'; | ||
(new FunctionMapGenerator)->createFunctionMap($filename); | ||
printf("Created call map in %s\n", $filename); | ||
|
||
class FunctionMapGenerator | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
public function createFunctionMap(string $filename): void | ||
{ | ||
$this->writeFunctionMap($filename, $this->getFunctionMap()); | ||
} | ||
|
||
private function getFunctionMap(): array | ||
{ | ||
$classes = array_filter(get_declared_classes(), $this->filterItems(...)); | ||
$interfaces = array_filter(get_declared_interfaces(), $this->filterItems(...)); | ||
$functions = array_filter(get_defined_functions()['internal'], $this->filterItems(...)); | ||
|
||
$functionMap = []; | ||
|
||
// Generate call map for functions | ||
foreach ($functions as $functionName) { | ||
$reflectionFunction = new ReflectionFunction($functionName); | ||
$functionMap[$reflectionFunction->getName()] = $this->getFunctionMapEntry($reflectionFunction); | ||
} | ||
|
||
// Generate call map for classes and interfaces | ||
$members = array_merge($classes, $interfaces); | ||
sort($members); | ||
|
||
$skippedMethods = ['__set_state', '__wakeup', '__serialize', '__unserialize']; | ||
|
||
foreach ($members as $member) { | ||
$reflectionClass = new ReflectionClass($member); | ||
|
||
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { | ||
if ($method->getDeclaringClass() != $reflectionClass && $method->getName() != '__toString') { | ||
continue; | ||
} | ||
|
||
if (in_array($method->getName(), $skippedMethods, true)) { | ||
continue; | ||
} | ||
|
||
$methodKey = $reflectionClass->getName() . '::' . $method->getName(); | ||
$functionMap[$methodKey] = $this->getFunctionMapEntry($method); | ||
} | ||
} | ||
|
||
return $functionMap; | ||
} | ||
|
||
private function writeFunctionMap(string $filename, array $functionMap): void | ||
{ | ||
$lines = []; | ||
foreach ($functionMap as $methodName => $typeInfo) { | ||
$generatedTypeInfo = implode( | ||
', ', | ||
array_map( | ||
function (string|int $key, string $value): string { | ||
if (is_int($key)) { | ||
return $this->removeDoubleBackslash(var_export($value, true)); | ||
} | ||
|
||
return sprintf('%s => %s', var_export($key, true), $this->removeDoubleBackslash(var_export($value, true))); | ||
}, | ||
array_keys($typeInfo), | ||
array_values($typeInfo) | ||
) | ||
); | ||
|
||
$lines[] = sprintf( | ||
' %s => [%s],', | ||
$this->removeDoubleBackslash(var_export($methodName, true)), | ||
$generatedTypeInfo | ||
); | ||
} | ||
|
||
$fileTemplate = <<<'PHP' | ||
<?php | ||
|
||
$mongoDBFunctionMap = [ | ||
%s | ||
]; | ||
|
||
PHP; | ||
|
||
file_put_contents($filename, sprintf($fileTemplate, implode("\n", $lines))); | ||
} | ||
|
||
private function filterItems(string $name): bool { | ||
$namespaces = ['MongoDB\BSON\\', 'MongoDB\Driver\\']; | ||
|
||
$name = strtolower($name); | ||
|
||
foreach ($namespaces as $namespace) { | ||
// Always compare lowercase names, as get_defined_functions lowercases function names by default | ||
if (str_starts_with($name, strtolower($namespace))) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function getFunctionMapEntry(ReflectionFunctionAbstract $function): array | ||
{ | ||
$returnType = match(true) { | ||
$function->hasReturnType() => (string) $function->getReturnType(), | ||
$function->hasTentativeReturnType() => (string) $function->getTentativeReturnType(), | ||
default => 'void', | ||
}; | ||
|
||
$functionMapEntry = [$returnType]; | ||
|
||
foreach ($function->getParameters() as $parameter) { | ||
$parameterKey = $parameter->getName(); | ||
if ($parameter->isOptional()) { | ||
$parameterKey .= '='; | ||
} | ||
|
||
$functionMapEntry[$parameterKey] = (string) $parameter->getType(); | ||
} | ||
|
||
return $functionMapEntry; | ||
} | ||
|
||
private function removeDoubleBackslash(string $string): string | ||
{ | ||
return str_replace('\\\\', '\\', $string); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.