2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
|
|
|
|
use Psalm\CodeLocation;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Context;
|
2021-01-06 15:05:53 +01:00
|
|
|
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;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2021-01-06 15:05:53 +01:00
|
|
|
use function is_subclass_of;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class PropertyVisibilityProvider
|
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
/**
|
|
|
|
* @var array<
|
|
|
|
* lowercase-string,
|
|
|
|
* array<\Closure(PropertyVisibilityProviderEvent) : ?bool>
|
|
|
|
* >
|
|
|
|
*/
|
|
|
|
private static $handlers = [];
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
/**
|
|
|
|
* @var array<
|
2020-12-29 12:42:12 +01:00
|
|
|
* lowercase-string,
|
2019-03-01 14:57:10 +01:00
|
|
|
* array<\Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* bool,
|
2020-08-31 22:40:16 +02:00
|
|
|
* Context,
|
|
|
|
* CodeLocation
|
2019-03-01 14:57:10 +01:00
|
|
|
* ) : ?bool>
|
|
|
|
* >
|
|
|
|
*/
|
2021-01-06 15:05:53 +01:00
|
|
|
private static $legacy_handlers = [];
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
self::$handlers = [];
|
2021-01-06 15:05:53 +01:00
|
|
|
self::$legacy_handlers = [];
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-06 15:05:53 +01:00
|
|
|
* @param class-string<LegacyPropertyVisibilityProviderInterface>
|
|
|
|
* |class-string<PropertyVisibilityProviderInterface> $class
|
2019-03-01 14:57:10 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerClass(string $class): void
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
if (is_subclass_of($class, LegacyPropertyVisibilityProviderInterface::class, true)) {
|
|
|
|
$callable = \Closure::fromCallable([$class, 'isPropertyVisible']);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
|
|
|
|
$this->registerLegacyClosure($fq_classlike_name, $callable);
|
|
|
|
}
|
|
|
|
} elseif (is_subclass_of($class, PropertyVisibilityProviderInterface::class, true)) {
|
|
|
|
$callable = \Closure::fromCallable([$class, 'isPropertyVisible']);
|
|
|
|
|
|
|
|
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
|
|
|
|
$this->registerClosure($fq_classlike_name, $callable);
|
|
|
|
}
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-06 15:05:53 +01:00
|
|
|
* @param \Closure(PropertyVisibilityProviderEvent) : ?bool $c
|
|
|
|
*/
|
|
|
|
public function registerClosure(string $fq_classlike_name, \Closure $c): void
|
|
|
|
{
|
|
|
|
self::$handlers[strtolower($fq_classlike_name)][] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-01 14:57:10 +01:00
|
|
|
* @param \Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* bool,
|
2020-08-31 22:40:16 +02:00
|
|
|
* Context,
|
|
|
|
* CodeLocation
|
2019-03-01 14:57:10 +01:00
|
|
|
* ) : ?bool $c
|
|
|
|
*/
|
2021-01-06 15:05:53 +01:00
|
|
|
public function registerLegacyClosure(string $fq_classlike_name, \Closure $c): void
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
self::$legacy_handlers[strtolower($fq_classlike_name)][] = $c;
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function has(string $fq_classlike_name) : bool
|
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
return isset(self::$handlers[strtolower($fq_classlike_name)]) ||
|
|
|
|
isset(self::$legacy_handlers[strtolower($fq_classlike_name)]);
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?bool {
|
2021-05-01 22:56:25 +02:00
|
|
|
foreach (self::$legacy_handlers[strtolower($fq_classlike_name)] ?? [] as $property_handler) {
|
|
|
|
$property_visible = $property_handler(
|
2021-01-06 15:05:53 +01:00
|
|
|
$source,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$property_name,
|
|
|
|
$read_mode,
|
|
|
|
$context,
|
|
|
|
$code_location
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($property_visible !== null) {
|
|
|
|
return $property_visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 22:56:25 +02:00
|
|
|
foreach (self::$handlers[strtolower($fq_classlike_name)] ?? [] as $property_handler) {
|
|
|
|
$event = new PropertyVisibilityProviderEvent(
|
2019-03-01 14:57:10 +01:00
|
|
|
$source,
|
|
|
|
$fq_classlike_name,
|
|
|
|
$property_name,
|
|
|
|
$read_mode,
|
|
|
|
$context,
|
|
|
|
$code_location
|
|
|
|
);
|
2021-05-01 22:56:25 +02:00
|
|
|
$property_visible = $property_handler($event);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
if ($property_visible !== null) {
|
|
|
|
return $property_visible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|