Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add php-parser Node to reflection data and cleanup in the end #78

Open
wants to merge 1 commit into
base: 0.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Reflection/Internal/CompleteReflection/RemoveNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Typhoon\Reflection\Internal\CompleteReflection;

use Typhoon\DeclarationId\AnonymousClassId;
use Typhoon\DeclarationId\AnonymousFunctionId;
use Typhoon\DeclarationId\ConstantId;
use Typhoon\DeclarationId\NamedClassId;
use Typhoon\DeclarationId\NamedFunctionId;
use Typhoon\Reflection\Internal\Data;
use Typhoon\Reflection\Internal\Hook\ClassHook;
use Typhoon\Reflection\Internal\Hook\ConstantHook;
use Typhoon\Reflection\Internal\Hook\FunctionHook;
use Typhoon\Reflection\Internal\Hook\HookPriorities;
use Typhoon\Reflection\TyphoonReflector;
use Typhoon\TypedMap\TypedMap;

/**
* @internal
* @psalm-internal Typhoon\Reflection
*/
enum RemoveNode implements ConstantHook, FunctionHook, ClassHook
{
case Instance;

public function priority(): int
{
return HookPriorities::CLEAN_UP;
}

public function processConstant(ConstantId $id, TypedMap $data, TyphoonReflector $reflector): TypedMap
{
return $data->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);
}
}
1 change: 1 addition & 0 deletions src/Reflection/Internal/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 27 additions & 0 deletions src/Reflection/Internal/Data/NodeKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Typhoon\Reflection\Internal\Data;

use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Function_;
use Typhoon\TypedMap\OptionalKey;
use Typhoon\TypedMap\TypedMap;

/**
* @internal
* @psalm-internal Typhoon\Reflection\Internal
* @implements OptionalKey<null|ClassLike|Function_|FuncCall|Const_>
*/
enum NodeKey implements OptionalKey
{
case Key;

public function default(TypedMap $map): mixed
{
return null;
}
}
4 changes: 4 additions & 0 deletions src/Reflection/Internal/PhpParser/NodeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/TyphoonReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,6 +103,7 @@ public static function build(
SetTemplateIndex::Instance,
ResolveClassInheritance::Instance,
RemoveContext::Instance,
RemoveNode::Instance,
RemoveCode::Instance,
CleanUpInternallyDefined::Instance,
]),
Expand Down