1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 14:38:37 +01:00
psalm/src/Psalm/Internal/Provider/PropertyVisibilityProvider.php

138 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\Provider;
2021-12-03 21:40:18 +01:00
use Closure;
use Psalm\CodeLocation;
2019-07-05 22:24:00 +02:00
use Psalm\Context;
use Psalm\Plugin\EventHandler\Event\PropertyVisibilityProviderEvent;
use Psalm\Plugin\EventHandler\PropertyVisibilityProviderInterface;
use Psalm\Plugin\Hook\PropertyVisibilityProviderInterface as LegacyPropertyVisibilityProviderInterface;
2019-07-05 22:24:00 +02:00
use Psalm\StatementsSource;
2021-06-08 04:55:21 +02:00
use function is_subclass_of;
2021-06-08 04:55:21 +02:00
use function strtolower;
class PropertyVisibilityProvider
{
/**
* @var array<
* lowercase-string,
2021-12-04 03:37:19 +01:00
* array<Closure(PropertyVisibilityProviderEvent) : ?bool>
* >
*/
private static $handlers = [];
/**
* @var array<
* lowercase-string,
2021-12-04 03:37:19 +01:00
* array<Closure(
* StatementsSource,
* string,
* string,
* bool,
2020-08-31 22:40:16 +02:00
* Context,
* CodeLocation
* ) : ?bool>
* >
*/
private static $legacy_handlers = [];
public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
}
/**
* @param class-string<LegacyPropertyVisibilityProviderInterface>
* |class-string<PropertyVisibilityProviderInterface> $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyPropertyVisibilityProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'isPropertyVisible']);
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
$this->registerLegacyClosure($fq_classlike_name, $callable);
}
} elseif (is_subclass_of($class, PropertyVisibilityProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'isPropertyVisible']);
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
$this->registerClosure($fq_classlike_name, $callable);
}
}
}
/**
2021-12-04 03:37:19 +01:00
* @param Closure(PropertyVisibilityProviderEvent): ?bool $c
*/
2021-12-03 21:40:18 +01:00
public function registerClosure(string $fq_classlike_name, Closure $c): void
{
self::$handlers[strtolower($fq_classlike_name)][] = $c;
}
/**
2021-12-04 03:37:19 +01:00
* @param Closure(
* StatementsSource,
* string,
* string,
* bool,
2020-08-31 22:40:16 +02:00
* Context,
* CodeLocation
2021-12-04 03:37:19 +01:00
* ): ?bool $c
*/
2021-12-03 21:40:18 +01:00
public function registerLegacyClosure(string $fq_classlike_name, Closure $c): void
{
self::$legacy_handlers[strtolower($fq_classlike_name)][] = $c;
}
public function has(string $fq_classlike_name) : bool
{
return isset(self::$handlers[strtolower($fq_classlike_name)]) ||
isset(self::$legacy_handlers[strtolower($fq_classlike_name)]);
}
public function isPropertyVisible(
StatementsSource $source,
string $fq_classlike_name,
string $property_name,
bool $read_mode,
2020-08-31 22:40:16 +02:00
Context $context,
CodeLocation $code_location
): ?bool {
foreach (self::$legacy_handlers[strtolower($fq_classlike_name)] ?? [] as $property_handler) {
$property_visible = $property_handler(
$source,
$fq_classlike_name,
$property_name,
$read_mode,
$context,
$code_location
);
if ($property_visible !== null) {
return $property_visible;
}
}
foreach (self::$handlers[strtolower($fq_classlike_name)] ?? [] as $property_handler) {
$event = new PropertyVisibilityProviderEvent(
$source,
$fq_classlike_name,
$property_name,
$read_mode,
$context,
$code_location
);
$property_visible = $property_handler($event);
if ($property_visible !== null) {
return $property_visible;
}
}
return null;
}
}