Closed zmitic closed 11 months ago
This isn't really pretty code, right?
/** @param non-empty-string $name */ fn(string $name, Product $p) ...
Not even PHPStan currently supports reading PHPDocs above closures and arrow functions, see: https://github.com/phpstan/phpstan/issues/3770
So if you decide to implement this you should first make sure that your users would even be willing to write code like this. To me it feels like a lot of magic.
But you're right, if we disregard PHPStan and consider that you're just using phpstan/phpdoc-parser to achieve whatever features you want, you need to implement this part yourself.
phpstan/phpdoc-parser just provides an AST representation. A lot of other useful things are done by PHPStan itself, and unfortunately it's not really separable from the rest of the tool.
These things are:
Thanks for quick response, I will keep my eye on these. The example I put doesn't have to be param before the closure, this is totally fine too as long as I document it:
fn(/** @param non-empty-string $name */ string $name, Product $p)
or variation of it. Sure, it might look like magic but I assure you, it is very useful in projects with big reusable forms. I guess TypeNodeResolver is what I am looking for, thanks again.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I am sorry if this is the wrong repo but I checked others, all the issues and tests, and couldn't find an answer. Tests like this are all using strings, and better reflection doesn't read docblocks.
Here is a real use case. With my bundle, user can do this in symfony/forms:
In the background, my code does a reflection of
update_value
. If the first param is non-nullable (like in the example), it silently addsNotNull
validation if users didn't do that themselves. It has to be on form level because entity can (and will) fail for empty_data, which means entity-level validation will not be executed. It is 100% legit behavior, I am not mixing wrong things, it is just hard to explain on simple example.The problem is if I wanted this:
Neither psalm nor phpstan would complain here, but without user manually adding validation, empty string could still end as
$name
. Forgetting that validation is very easy, happens to me all the time so I simply don't use it. But if I could read this@non-empty-string
somehow during runtime, from reflection, then it would be trivial to add such validation automatically. Not just this one, but many others likepositive-int
, orint<1, 42>
.I hope the above makes sense, I tried to put least complex example but the real use-cases go above this. Is this possible to do? Performance doesn't matter here.