2021-11-15 18:27:19 +01:00
|
|
|
<?php
|
2021-12-15 04:42:37 +01:00
|
|
|
|
2021-11-15 18:27:19 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psalm\Internal\Codebase;
|
|
|
|
|
|
|
|
use Psalm\Codebase;
|
2021-12-13 16:28:14 +01:00
|
|
|
use Psalm\Type\Atomic;
|
2021-11-15 18:27:19 +01:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
|
|
|
|
use function array_merge;
|
|
|
|
use function array_values;
|
|
|
|
use function preg_match;
|
|
|
|
use function sprintf;
|
|
|
|
use function str_replace;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
final class ClassConstantByWildcardResolver
|
|
|
|
{
|
2022-12-11 23:26:05 +01:00
|
|
|
private Codebase $codebase;
|
2021-11-15 18:27:19 +01:00
|
|
|
|
|
|
|
public function __construct(Codebase $codebase)
|
|
|
|
{
|
|
|
|
$this->codebase = $codebase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-13 16:28:14 +01:00
|
|
|
* @return list<Atomic>|null
|
2021-11-15 18:27:19 +01:00
|
|
|
*/
|
|
|
|
public function resolve(string $class_name, string $constant_pattern): ?array
|
|
|
|
{
|
|
|
|
if (!$this->codebase->classlike_storage_provider->has($class_name)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$constant_regex_pattern = sprintf('#^%s$#', str_replace('*', '.*', $constant_pattern));
|
|
|
|
|
|
|
|
$class_like_storage = $this->codebase->classlike_storage_provider->get($class_name);
|
|
|
|
$matched_class_constant_types = [];
|
|
|
|
|
|
|
|
foreach ($class_like_storage->constants as $constant => $class_constant_storage) {
|
|
|
|
if (preg_match($constant_regex_pattern, $constant) === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $class_constant_storage->type) {
|
|
|
|
$matched_class_constant_types[] = [new TMixed()];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$matched_class_constant_types[] = $class_constant_storage->type->getAtomicTypes();
|
|
|
|
}
|
|
|
|
|
2021-11-15 21:10:20 +01:00
|
|
|
return array_values(array_merge([], ...$matched_class_constant_types));
|
2021-11-15 18:27:19 +01:00
|
|
|
}
|
|
|
|
}
|