Skip to content

fix(reflection): ReflectionAttribute 支持 std 注解 getArguments/newInstance - #21

Merged
ctfang merged 3 commits into
mainfrom
cursor/reflection-attribute-args-3a0d
Nov 12, 2026
Merged

ctfang merged 3 commits into
mainfrom
cursor/reflection-attribute-args-3a0d

Conversation

@ctfang

@ctfang ctfang commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

根因(Loom 复测反馈)

  1. Container newInstance():CreateInstanceFromClass 只传入用户实参,未注入 target;构造里 annotationTargetClass 在缺少下标 1 时把下标 0 的用户字符串当成 $target,触发「target 类型错误」。
  2. 参数属性 target:ReflectionAttribute 的 metadata.target 把 TARGET_PARAMETER 错设为 16(实为 TARGET_CLASS_CONSTANT),TARGET_PROPERTY|TARGET_PARAMETER 等标志与参数反射的 target 对不上,误报 cannot target function。

修复

  • 抽取 bindAnnotationConstructorArgs,新增 InstantiateAnnotationForReflection:按声明绑定用户实参 + decl.Target,不执行 InitAnnotation/construct 副作用。
  • newInstance() 对含 AnnotationTargetParameter 的注解类走该路径,并继续 bindUserAnnotationProperties。
  • 对齐 PHP Attribute::TARGET_* 常量(TARGET_PARAMETER = 32),按目标类型生成校验错误信息。

测试

go test ./node ./std/php/reflection -count=1
go run ./zy.go tests/php/reflection_attribute_args_test.php
go run ./zy.go tests/php/function_attribute_contract_test.php

覆盖:Singleton/Bind/Inject/Named newInstance、构造参数上的 InjectConfig(PROPERTY|PARAMETER)、仅 TARGET_FUNCTION 在参数上仍应失败。

Loom 复测

在 frame-context 之上拉取本分支后,复跑 loom_modules_test、loom_config_injection_test 与 ModuleDiscovery::attribute() / newInstance() 路径即可。

Open in Web Open in Cursor 

cursoragent and others added 3 commits November 12, 2026 13:43
在类/方法/属性/参数上与应用实例并行存储 Annotation 声明节点,
ReflectionAttribute 据此还原用户实参并在 newInstance 后绑定可读属性;
放宽对无 PHP #[Attribute] 元数据的 std 原生注解校验。

Co-authored-by: selden <ctfang@users.noreply.github.com>
恢复无 #[Attribute] 的 PHP 类报错,同时 std 原生注解类不经过该校验。

Co-authored-by: selden <ctfang@users.noreply.github.com>
带 target 形参的注解经 InstantiateAnnotationForReflection 绑定实参且不执行构造副作用;
修正 ReflectionAttribute 目标常量(PARAMETER=32);补充 Container/参数 target 回归测试。

Co-authored-by: selden <ctfang@users.noreply.github.com>
@ctfang
ctfang merged commit a26bab2 into main Nov 12, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants