1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Add constant definitions

This commit is contained in:
Matthew Brown 2020-01-08 23:48:42 -05:00
parent 55bf6333af
commit bd7f342491
2 changed files with 54 additions and 7 deletions

View File

@ -665,6 +665,14 @@ class Codebase
return isset(self::$stubbed_constants[$const_id]) ? self::$stubbed_constants[$const_id] : null;
}
/**
* @return array<string, Type\Union>
*/
public function getAllStubbedConstants()
{
return self::$stubbed_constants;
}
/**
* @param string $file_path
*

View File

@ -14,13 +14,11 @@ class StubsGenerator
\Psalm\Internal\Provider\ClassLikeStorageProvider $class_provider,
\Psalm\Internal\Provider\FileStorageProvider $file_provider
) : string {
$all_class_storage = $class_provider->getAll();
$namespaced_nodes = [];
$psalm_base = dirname(__DIR__, 5);
foreach ($all_class_storage as $storage) {
foreach ($class_provider->getAll() as $storage) {
if (\strpos($storage->name, 'Psalm\\') === 0) {
continue;
}
@ -51,12 +49,9 @@ class StubsGenerator
);
}
unset($all_class_storage);
$all_stubbed_functions = $codebase->functions->getAllStubbedFunctions();
$all_function_names = [];
foreach ($all_stubbed_functions as $function_storage) {
foreach ($codebase->functions->getAllStubbedFunctions() as $function_storage) {
if ($function_storage->location
&& \strpos($function_storage->location->file_path, $psalm_base) === 0
) {
@ -83,6 +78,26 @@ class StubsGenerator
);
}
foreach ($codebase->getAllStubbedConstants() as $fq_name => $type) {
if ($type->isMixed()) {
continue;
}
$name_parts = explode('\\', $fq_name);
$constant_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = new PhpParser\Node\Stmt\Const_(
[
new PhpParser\Node\Const_(
$constant_name,
self::getExpressionFromType($type)
)
]
);
}
foreach ($file_provider->getAll() as $file_storage) {
if (\strpos($file_storage->file_path, $psalm_base) === 0) {
continue;
@ -112,6 +127,30 @@ class StubsGenerator
$namespace_name
);
}
foreach ($file_storage->constants as $fq_name => $type) {
if ($type->isMixed()) {
continue;
}
if ($type->isMixed()) {
continue;
}
$name_parts = explode('\\', $fq_name);
$constant_name = array_pop($name_parts);
$namespace_name = implode('\\', $name_parts);
$namespaced_nodes[$namespace_name][$fq_name] = new PhpParser\Node\Stmt\Const_(
[
new PhpParser\Node\Const_(
$constant_name,
self::getExpressionFromType($type)
)
]
);
}
}
ksort($namespaced_nodes);