2021-01-12 14:41:47 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-01-12 14:41:47 +01:00
|
|
|
namespace Rector\Privatization\Reflection;
|
|
|
|
|
2021-02-28 08:47:48 +01:00
|
|
|
use PHPStan\Reflection\ReflectionProvider;
|
2021-01-12 14:41:47 +01:00
|
|
|
final class ClassConstantsResolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, array<string, string>>
|
|
|
|
*/
|
|
|
|
private $cachedConstantNamesToValues = [];
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \PHPStan\Reflection\ReflectionProvider
|
2021-02-28 08:47:48 +01:00
|
|
|
*/
|
|
|
|
private $reflectionProvider;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\PHPStan\Reflection\ReflectionProvider $reflectionProvider)
|
2021-02-28 08:47:48 +01:00
|
|
|
{
|
|
|
|
$this->reflectionProvider = $reflectionProvider;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
2021-01-12 14:41:47 +01:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getClassConstantNamesToValues(string $classWithConstants) : array
|
2021-01-12 14:41:47 +01:00
|
|
|
{
|
|
|
|
if (isset($this->cachedConstantNamesToValues[$classWithConstants])) {
|
|
|
|
return $this->cachedConstantNamesToValues[$classWithConstants];
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->reflectionProvider->hasClass($classWithConstants)) {
|
2021-02-28 08:47:48 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$classReflection = $this->reflectionProvider->getClass($classWithConstants);
|
|
|
|
$reflectionClass = $classReflection->getNativeReflection();
|
|
|
|
$constantNamesToValues = $reflectionClass->getConstants();
|
2021-01-12 14:41:47 +01:00
|
|
|
$this->cachedConstantNamesToValues = $constantNamesToValues;
|
|
|
|
return $constantNamesToValues;
|
|
|
|
}
|
|
|
|
}
|