From cca91cf5587825b005a741f90baaa5f29e669213 Mon Sep 17 00:00:00 2001 From: Valentin Udaltsov Date: Wed, 7 Aug 2024 04:24:25 +0300 Subject: [PATCH] Add php-parser Node to reflection data and cleanup in the end --- .../CompleteReflection/RemoveNode.php | 47 +++++++++++++++++++ src/Reflection/Internal/Data.php | 1 + src/Reflection/Internal/Data/NodeKey.php | 27 +++++++++++ .../Internal/PhpParser/NodeReflector.php | 4 ++ src/Reflection/TyphoonReflector.php | 2 + 5 files changed, 81 insertions(+) create mode 100644 src/Reflection/Internal/CompleteReflection/RemoveNode.php create mode 100644 src/Reflection/Internal/Data/NodeKey.php diff --git a/src/Reflection/Internal/CompleteReflection/RemoveNode.php b/src/Reflection/Internal/CompleteReflection/RemoveNode.php new file mode 100644 index 00000000..410fb855 --- /dev/null +++ b/src/Reflection/Internal/CompleteReflection/RemoveNode.php @@ -0,0 +1,47 @@ +without(Data::Node); + } + + public function processFunction(NamedFunctionId|AnonymousFunctionId $id, TypedMap $data, TyphoonReflector $reflector): TypedMap + { + return $data->without(Data::Node); + } + + public function processClass(NamedClassId|AnonymousClassId $id, TypedMap $data, TyphoonReflector $reflector): TypedMap + { + return $data->without(Data::Node); + } +} diff --git a/src/Reflection/Internal/Data.php b/src/Reflection/Internal/Data.php index 54b8fd25..e956f4de 100644 --- a/src/Reflection/Internal/Data.php +++ b/src/Reflection/Internal/Data.php @@ -66,4 +66,5 @@ enum Data public const Deprecation = Data\DeprecationKey::Key; public const Optional = Data\BoolKeys::Optional; public const Cloneable = Data\BoolKeys::Cloneable; + public const Node = Data\NodeKey::Key; } diff --git a/src/Reflection/Internal/Data/NodeKey.php b/src/Reflection/Internal/Data/NodeKey.php new file mode 100644 index 00000000..f57d180e --- /dev/null +++ b/src/Reflection/Internal/Data/NodeKey.php @@ -0,0 +1,27 @@ + + */ +enum NodeKey implements OptionalKey +{ + case Key; + + public function default(TypedMap $map): mixed + { + return null; + } +} diff --git a/src/Reflection/Internal/PhpParser/NodeReflector.php b/src/Reflection/Internal/PhpParser/NodeReflector.php index c809df83..306f75d0 100644 --- a/src/Reflection/Internal/PhpParser/NodeReflector.php +++ b/src/Reflection/Internal/PhpParser/NodeReflector.php @@ -67,6 +67,7 @@ public function reflectConstant(Const_ $node, int|string $key, Context $context) $valueTypeReflector = new ConstantExpressionTypeReflector($context); return (new TypedMap()) + ->with(Data::Node, $node) ->with(Data::Context, $context) ->with(Data::PhpDoc, $node->getDocComment()) ->with(Data::Location, $this->reflectLocation($context, $node)) @@ -84,6 +85,7 @@ public function reflectDefine(FuncCall $node, Context $context): TypedMap $valueTypeReflector = new ConstantExpressionTypeReflector($context); return (new TypedMap()) + ->with(Data::Node, $node) ->with(Data::Location, $this->reflectLocation($context, $node)) ->with(Data::Namespace, $context->namespace()) ->with(Data::ValueExpression, $compiler->compile($valueArg->value)) @@ -94,12 +96,14 @@ public function reflectFunction(Function_ $node, Context $context): TypedMap { return $this ->reflectFunctionLike($node, $context) + ->with(Data::Node, $node) ->with(Data::Namespace, $context->namespace()); } public function reflectClassLike(ClassLike $node, Context $context): TypedMap { $data = (new TypedMap()) + ->with(Data::Node, $node) ->with(Data::PhpDoc, $node->getDocComment()) ->with(Data::Location, $this->reflectLocation($context, $node)) ->with(Data::Context, $context) diff --git a/src/Reflection/TyphoonReflector.php b/src/Reflection/TyphoonReflector.php index 79d8936e..95caaf24 100644 --- a/src/Reflection/TyphoonReflector.php +++ b/src/Reflection/TyphoonReflector.php @@ -30,6 +30,7 @@ use Typhoon\Reflection\Internal\CompleteReflection\CopyPromotedParameterToProperty; use Typhoon\Reflection\Internal\CompleteReflection\RemoveCode; use Typhoon\Reflection\Internal\CompleteReflection\RemoveContext; +use Typhoon\Reflection\Internal\CompleteReflection\RemoveNode; use Typhoon\Reflection\Internal\CompleteReflection\SetAttributeRepeated; use Typhoon\Reflection\Internal\CompleteReflection\SetClassCloneable; use Typhoon\Reflection\Internal\CompleteReflection\SetInterfaceMethodAbstract; @@ -102,6 +103,7 @@ public static function build( SetTemplateIndex::Instance, ResolveClassInheritance::Instance, RemoveContext::Instance, + RemoveNode::Instance, RemoveCode::Instance, CleanUpInternallyDefined::Instance, ]),