Skip to content

CompileNodeToValueTest: failing tests for unsupported default values (enum case, new)#1353

Closed
janedbal wants to merge 1 commit intoRoave:6.11.xfrom
janedbal:compile-node-to-value-error
Closed

CompileNodeToValueTest: failing tests for unsupported default values (enum case, new)#1353
janedbal wants to merge 1 commit intoRoave:6.11.xfrom
janedbal:compile-node-to-value-error

Conversation

@janedbal
Copy link
Copy Markdown
Contributor

I was trying to fix this issue, but it looks like it is not designed for non-scalar values: ReflectionParameter::getDefaultValue(): string|int|float|bool|array|null.


The actual issue we are facing is that we initialize custom attribute within custom phpstan rule. When a default value of such attribute is enum case, phpstan collapses.

@Ocramius
Copy link
Copy Markdown
Member

This is correct: getDefaultValue() cannot produce object, as it goes against the basic invariant of roave/better-reflection not loading any of the code being analyzed.

@kukulich
Copy link
Copy Markdown
Collaborator

@janedbal it can be “fixed” in https://github.com/ondrejmirtes/BetterReflection

@Ocramius
Copy link
Copy Markdown
Member

Certainly, but the basic invariant here is "don't load code", so that will certainly not happen here.

Also for the fork, loading runtime code could even be categorized as a security issue, from my point of view: instead of loading the code, load the AST for it instead.

@JanTvrdik
Copy link
Copy Markdown

If it cannot be fixed, which I understand, could it at least throw better exception in such cases?

@Ocramius
Copy link
Copy Markdown
Member

@JanTvrdik what's the current exception you get? We generally have quite specific exception types.

Is it this one, perhaps?

https://github.com/Roave/BetterReflection/blob/ce6a9a27c746fbd45d5a6603ba24e7d4a1f3b0f7/src/NodeCompiler/CompileNodeToValue.php#LL132C35-L132C35

Another alternative that comes to mind is having BetterReflection operating in "unsafe mode": something like new Reflector($yesIReallyWantToExposeMyselfToRCEAttacksWhileInspectingForeignCodebases = true)

@janedbal
Copy link
Copy Markdown
Contributor Author

Maybe worth updating Compatibility page with this issue? ReflectionAttribute is missing there completely.

@JanTvrdik
Copy link
Copy Markdown

@Ocramius It's actually https://github.com/Roave/BetterReflection/blob/6.11.x/src/NodeCompiler/CompileNodeToValue.php#L250, because the "enum case fetch" is represented as ClassConstFetch in AST. So instead of "constant not found" error I would expect "enum values are unsupported" error.

@Ocramius
Copy link
Copy Markdown
Member

@janedbal please do send a patch :-)

@JanTvrdik is it possible to have a test with a better message expectation, perhaps? That would certainly improve the DX a lot :)

janedbal added a commit to janedbal/BetterReflection that referenced this pull request Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants