Add new method signature rule#1292
Conversation
91943c0 to
201a3cb
Compare
|
Hi, it's not gonna be that easy because I still want it to work on Rule classes (see the failed build). I know it isn't exactly right but it's a common pattern. Basically I want to specify the type of the parameter in phpDoc because >I know< what's going to be passed there. |
201a3cb to
3162279
Compare
|
@ondrejmirtes Don't worry, we can ignore all errors from the I have some issues with traits at the moment (for some reason the phpDoc types are ignored in traits). I'll see if I can fix that right now. |
3162279 to
b410394
Compare
b410394 to
cbaeb24
Compare
cb54430 to
5308c8c
Compare
|
Great, now it suddenly works and I've changed absolutely nothing 😬 |
|
You cannot solve #532 without introducing generics first. The |
|
@JanTvrdik #532 is not about generics but about covariance and contravariance. We don't need generics for the support that. Sure, because of the introduction of this rule I had to ignore this error: - '#^Declaration of PHPStan\\Rules\\.*::processNode\(.*\): .* should be compatible with PHPStan\\Rules\\Rule::processNode\(.*\): .*$#' |
|
Also, I don't think people will really use generics until it's a language feature or something their IDE supports. For example, at our company we're not using the |
Yes, but without generics you're missing the key information that is required to decide whether the parameter contravariance is valid. You simply do not have enough data. |
Well, this is gonna break some code. Even if we introduce generics people will have to go and annotate their code with generic parameters. Generics aren't gonna change that. There's other ways to achieve similar results. You could: class SomeRule implements Rule
{
public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof Function_) {
throw new \InvalidArgumentException();
}
// ...
}
}Obviously this does not protect you from the calling side as the signature is still the same. But that's really not the point here. The method signature rule is all about keeping the promise of the method signature. Note that PHP itself has simple forms of covariance and contravariance while lacking generic support. |
5308c8c to
5756e52
Compare
|
Can you give me feedback on this PR? One issue I've had is that the Also, how do you feel about the whole generics issue? |
6c80055 to
775c46a
Compare
|
I added support for the Does this defuse the situation in your opinion? |
|
I'm still a little bit torn about this - people sometimes use phpDocs to override badly documented types of parent classes, therefore breaking LSP on purpose. |
That's definitely a valid argument. However, I don't think we should treat the exception as the rule. You can always ignore such cases or ignore the rule completely if that's happens all over your codebase. Also, only invariant types are reported all the way up to level 6. |
5904f1c to
99bb3a9
Compare
775c46a to
59b191b
Compare
ac6bbca to
6fc59ee
Compare
|
After fixing all phpDoc issues this finally works with no dirty workarounds 🎉 |
6fc59ee to
df2e2f9
Compare
ondrejmirtes
left a comment
There was a problem hiding this comment.
I like this a lot! I was rewriting something at my job yesterday and this rule is something I'd really appreciate :)
I have some suggestions:
- You probably missed my comment about better error messages: #1292 (comment)
- Method
checkParameterCompatibilityincludescheckReturnTypeCompatibilityso it should be renamed, probably tocheckMethodCompatibility. You might have to refactor it too because of 1).
|
Sorry, I had some personal issues these days. Give me a few days or feel free to take over. |
|
No problem, I'll wait 👌 Take care. |
df2e2f9 to
db14605
Compare
db14605 to
83132ab
Compare
|
@ondrejmirtes This is ready. 🙂 |
|
Perfect! :) Thank you very much. |
|
@ondrejmirtes Awesome, thank you! Do we wanna turn on |
|
Yeah, please do :) I also added a commit here so that the build doesn't break (because of Rule interface implementations): 978f829 |
Closes #532
Closes #729