Skip to content
Permalink
Browse files

Add constant definitions

  • Loading branch information
muglug committed Jan 9, 2020
1 parent 55bf633 commit bd7f3424914807ad7f3c3355707b3b9bb4d7e76f
Showing with 54 additions and 7 deletions.
  1. +8 −0 src/Psalm/Codebase.php
  2. +46 −7 src/Psalm/Internal/Stubs/Generator/StubsGenerator.php
@@ -665,6 +665,14 @@ public function getStubbedConstantType($const_id)
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
*
@@ -14,13 +14,11 @@ public static function getAll(
\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 @@ public static function getAll(
);
}

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 @@ public static function getAll(
);
}

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 @@ public static function getAll(
$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);

0 comments on commit bd7f342

Please sign in to comment.
You can’t perform that action at this time.