2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use const PHP_VERSION;
|
2019-03-01 14:57:10 +01:00
|
|
|
use PhpParser;
|
|
|
|
use Psalm\CodeLocation;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Context;
|
2019-03-01 14:57:10 +01:00
|
|
|
use Psalm\Plugin\Hook\PropertyVisibilityProviderInterface;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\StatementsSource;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function version_compare;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class PropertyVisibilityProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<
|
|
|
|
* string,
|
|
|
|
* array<\Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* bool,
|
|
|
|
* ?Context=,
|
|
|
|
* ?CodeLocation=
|
|
|
|
* ) : ?bool>
|
|
|
|
* >
|
|
|
|
*/
|
|
|
|
private static $handlers = [];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
self::$handlers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<PropertyVisibilityProviderInterface> $class
|
|
|
|
* @psalm-suppress PossiblyUnusedParam
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerClass(string $class)
|
|
|
|
{
|
|
|
|
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
|
|
|
|
/**
|
|
|
|
* @psalm-suppress UndefinedMethod
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
* @var \Closure
|
|
|
|
*/
|
|
|
|
$callable = \Closure::fromCallable([$class, 'isPropertyVisible']);
|
|
|
|
} else {
|
|
|
|
$callable = (new \ReflectionClass($class))->getMethod('isPropertyVisible')->getClosure(new $class);
|
|
|
|
|
|
|
|
if (!$callable) {
|
|
|
|
throw new \UnexpectedValueException('Callable must not be null');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
|
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
|
|
|
$this->registerClosure($fq_classlike_name, $callable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /**
|
|
|
|
* @param \Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* bool,
|
|
|
|
* ?Context=,
|
|
|
|
* ?CodeLocation=
|
|
|
|
* ) : ?bool $c
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerClosure(string $fq_classlike_name, \Closure $c)
|
|
|
|
{
|
|
|
|
self::$handlers[strtolower($fq_classlike_name)][] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has(string $fq_classlike_name) : bool
|
|
|
|
{
|
|
|
|
return isset(self::$handlers[strtolower($fq_classlike_name)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
* @return ?bool
|
|
|
|
*/
|
|
|
|
public function isPropertyVisible(
|
|
|
|
StatementsSource $source,
|
|
|
|
string $fq_classlike_name,
|
|
|
|
string $property_name,
|
|
|
|
bool $read_mode,
|
|
|
|
Context $context = null,
|
|
|
|
CodeLocation $code_location = null
|
|
|
|
) {
|
|
|
|
foreach (self::$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|