Aggiornato Composer
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace PhpParser;
|
||||
|
||||
class JsonDecoder
|
||||
{
|
||||
/** @var \ReflectionClass[] Node type to reflection class map */
|
||||
private $reflectionClassCache;
|
||||
class JsonDecoder {
|
||||
/** @var \ReflectionClass<Node>[] Node type to reflection class map */
|
||||
private array $reflectionClassCache;
|
||||
|
||||
/** @return mixed */
|
||||
public function decode(string $json) {
|
||||
$value = json_decode($json, true);
|
||||
if (json_last_error()) {
|
||||
@@ -16,6 +16,10 @@ class JsonDecoder
|
||||
return $this->decodeRecursive($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
private function decodeRecursive($value) {
|
||||
if (\is_array($value)) {
|
||||
if (isset($value['nodeType'])) {
|
||||
@@ -29,7 +33,7 @@ class JsonDecoder
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function decodeArray(array $array) : array {
|
||||
private function decodeArray(array $array): array {
|
||||
$decodedArray = [];
|
||||
foreach ($array as $key => $value) {
|
||||
$decodedArray[$key] = $this->decodeRecursive($value);
|
||||
@@ -37,14 +41,13 @@ class JsonDecoder
|
||||
return $decodedArray;
|
||||
}
|
||||
|
||||
private function decodeNode(array $value) : Node {
|
||||
private function decodeNode(array $value): Node {
|
||||
$nodeType = $value['nodeType'];
|
||||
if (!\is_string($nodeType)) {
|
||||
throw new \RuntimeException('Node type must be a string');
|
||||
}
|
||||
|
||||
$reflectionClass = $this->reflectionClassFromNodeType($nodeType);
|
||||
/** @var Node $node */
|
||||
$node = $reflectionClass->newInstanceWithoutConstructor();
|
||||
|
||||
if (isset($value['attributes'])) {
|
||||
@@ -66,7 +69,7 @@ class JsonDecoder
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function decodeComment(array $value) : Comment {
|
||||
private function decodeComment(array $value): Comment {
|
||||
$className = $value['nodeType'] === 'Comment' ? Comment::class : Comment\Doc::class;
|
||||
if (!isset($value['text'])) {
|
||||
throw new \RuntimeException('Comment must have text');
|
||||
@@ -79,7 +82,8 @@ class JsonDecoder
|
||||
);
|
||||
}
|
||||
|
||||
private function reflectionClassFromNodeType(string $nodeType) : \ReflectionClass {
|
||||
/** @return \ReflectionClass<Node> */
|
||||
private function reflectionClassFromNodeType(string $nodeType): \ReflectionClass {
|
||||
if (!isset($this->reflectionClassCache[$nodeType])) {
|
||||
$className = $this->classNameFromNodeType($nodeType);
|
||||
$this->reflectionClassCache[$nodeType] = new \ReflectionClass($className);
|
||||
@@ -87,7 +91,8 @@ class JsonDecoder
|
||||
return $this->reflectionClassCache[$nodeType];
|
||||
}
|
||||
|
||||
private function classNameFromNodeType(string $nodeType) : string {
|
||||
/** @return class-string<Node> */
|
||||
private function classNameFromNodeType(string $nodeType): string {
|
||||
$className = 'PhpParser\\Node\\' . strtr($nodeType, '_', '\\');
|
||||
if (class_exists($className)) {
|
||||
return $className;
|
||||
|
||||
Reference in New Issue
Block a user