Repository navigation
fix(parser): 未限定 Name::class 编译期解析为 FQCN - #23
Merged
Merged
Conversation
未限定 ClassName::class 先前仅在 findFullClassNameByNamespace 返回 ok=true 时采用 FQCN,类未加载时会固化为短名。与 parseStaticCall 一致, 编译期始终采用返回的候选全名,符合 PHP 不要求类存在且不 autoload 的语义。 Co-authored-by: selden <ctfang@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
在命名空间内写
Bar::class时,若Foo\Bar尚未加载,Origami 会在解析期把常量固化为短名Bar;后续加载该类也不会改变该字符串。官方 PHP 则在编译期按当前命名空间与use展开为Foo\Bar,且不要求类存在、不触发 autoload。这会导致依赖容器
has(SomeClass::class)的代码(例如 LoomServiceScope)在类晚加载时永远匹配失败。原因
parser/ident_parser.go处理Ident::class时仅在findFullClassNameByNamespace返回ok=true时才采用展开后的类名;而同一文件中的parseStaticCall早已注明:ok=false时仍可能返回带命名空间前缀的候选 FQCN,必须采用。修复
Name::class与parseStaticCall对齐:findFullClassNameByNamespace返回非空字符串即作为编译期类名字面量。self::class、static::class、parent::class及$obj::class(仍走ClassConstant运行时节点)。回归测试
新增
tests/php/class_const_compile_time_test.php,覆盖:Baz\Qux::classuse ... as别名stdClass::class验证
go run ./zy.go tests/php/class_const_compile_time_test.php go run ./zy.go tests/php/this_class_const_test.php go test ./parser/... ./node/...