-
Notifications
You must be signed in to change notification settings - Fork 542
NodeCallbackInvoker #4429
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
NodeCallbackInvoker #4429
Conversation
0a70f9d
to
c3e50ec
Compare
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
|
||
final class DirectInternalScopeFactoryFactory implements InternalScopeFactoryFactory |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am getting java vibes.. where is the FactoryFactoryFactory :-)
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a testing-only change which we need to be cleaned up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we're calling another rule, which might need it. If you revert it, you get PHPStan error.
return Node\Stmt\Echo_::class; | ||
} | ||
|
||
public function processNode(Node $node, NodeCallbackInvoker&Scope $scope): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function processNode(Node $node, NodeCallbackInvoker&Scope $scope): array | |
public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed the downgrader to also support the intersection type when notated in different order:
ondrejmirtes/simple-downgrader#17
/** | ||
* The interface NodeCallbackInvoker can be typehinted in 2nd parameter of Rule::processNode(): | ||
* | ||
* ```php | ||
* public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array | ||
* ``` | ||
* | ||
* It can be used to invoke rules for virtual made-up nodes. | ||
* | ||
* For example: You're writing a rule for a method with declaration like: | ||
* | ||
* ```php | ||
* public static create(string $class, mixed ...$args) | ||
* ``` | ||
* | ||
* And you'd like to check `Factory::create(Foo::class, 1, 2, 3)` as if it were | ||
* `new Foo(1, 2, 3)`. | ||
* | ||
* You can call `$scope->invokeNodeCallback(new New_(new Name($className), $args))` | ||
* | ||
* And PHPStan will call all the registered rules for New_, checking as if the instantiation | ||
* is actually in the code. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh wow, this is awesome! 🚀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I vaguely remember you're using FunctionCallParametersCheck somewhere, which is not part of BC promise. This would allow you to write simple and clean code instead 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note that this PHPDoc is lying a little bit. You don't need to change your processNode signature to take advantage of this. phpstan-src uses native intersection type which is downgraded to PHPDoc because of PHP 7.4+ support. Which means that unless you have your own PHPDoc in your custom rule, PHPStan will implicitly inherit the Rule interface PHPDoc and you can call $scope->invokeNodeCallback
without changing the signature :)
c3e50ec
to
51f9c39
Compare
No description provided.