-
Notifications
You must be signed in to change notification settings - Fork 48
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
Feature/deprecations #18
Conversation
FYI you can run the build locally and you don't have to wait for Travis 😊 I'd like if you squashed those "fix" commits into the places where the error got created in the first places, for a nicer Git history. Thanks! |
Haha yeah I usually do that but the phpcs configuration changed since I've made these commits, I'll squash everything once it's working properly. |
@ondrejmirtes Any idea why some of the tested files are actually run? I've got some To explain why, I'm testing deprecated property accesses in some tests and the easiest way to use them was just an |
b88079b
to
7bf2992
Compare
@ondrejmirtes Ok, I think this is ready. Let me know if there's anything I need to adjust. 🙂 As we discussed before, this PR does currently not support warnings when using deprecated classes or interfaces in type hints. |
Any news? |
Don't worry, I'll get to it soon, it will be part of 0.10 😊 |
Nice 👍 Sorry, patience is not one of my virtues 😄 |
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.
Also, please add a one-line bullet point to the README 😊 Thanks!
} | ||
|
||
$methodCalledOnType = $scope->getType($node->var); | ||
$referencedClasses = $methodCalledOnType->getReferencedClasses(); |
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.
This should rather use TypeUtils::getDirectClassNames
.
} | ||
|
||
$propertyAccessedOnType = $scope->getType($node->var); | ||
$referencedClasses = $propertyAccessedOnType->getReferencedClasses(); |
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.
This should rather use TypeUtils::getDirectClassNames()
.
if ($node->class instanceof Name) { | ||
$referredClasses[] = (string) $node->class; | ||
} else { | ||
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck( |
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.
There's a missing:
if ($classTypeResult->getType() instanceof ErrorType) {
return [];
}
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.
Also, the same method could be used for static methods (not skipping $node->name
that is not Name).
$classTypeResult = $this->ruleLevelHelper->findTypeToCheck( | ||
$scope, | ||
$node->class, | ||
'%s' // We don't care about the error message |
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.
Empty string please.
|
||
if ($property instanceof DeprecatableReflection && $property->isDeprecated()) { | ||
return [sprintf( | ||
'Access to deprecated static property %s of class %s.', |
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.
property $%s
'%s' // We don't care about the error message | ||
); | ||
|
||
$referredClasses = $classTypeResult->getReferencedClasses(); |
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.
$referencedClasses
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->class instanceof Name) { |
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.
Employ RuleLevelHelper::findTypeToCheck
instead of skipping
} | ||
|
||
return [sprintf( | ||
'Fetching class constant of deprecated class %s.', |
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.
Which class constant?
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.
There's only one class constant 🙂
E.g. stdClass::class
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.
IMHO this rule works for all accessed class constants...
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.
Haha yeah sorry. Apparently I'm confused. But that made me notice that we're totally missing deprecations of constants! I'll fix that too.
public static function isScopeDeprecated(Scope $scope): bool | ||
{ | ||
$class = $scope->getClassReflection(); | ||
if ($class !== null && $class->isDeprecated()) { |
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.
We might also be inside a deprecated trait.
@@ -1,6 +1,6 @@ | |||
<?php declare(strict_types = 1); | |||
|
|||
namespace PHPStan\Rules\Deprecations; | |||
namespace PHPStan\Analyzer; |
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.
Noo, leave it in the original place 😊
I found a few things during the review. Hopefully you will be able to go through them this week, I plan to release 0.10 the next week 😊 Thanks! |
if ($interface->isDeprecated()) { | ||
if (!$class->getNativeReflection()->isAnonymous()) { | ||
$errors[] = sprintf( | ||
'Implementation of deprecated interface %s in class %s.', |
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.
What about Class %s implements deprecated interface %s.
?
if ($parentClass->isDeprecated()) { | ||
if (!$class->getNativeReflection()->isAnonymous()) { | ||
$errors[] = sprintf( | ||
'Inheritance of deprecated class %s in class %s.', |
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.
What about Class %s extends deprecated class %s.
?
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.
Also, what about interfaces extending other deprecated interfaces? 😊
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.
Also, what about interfaces extending other deprecated interfaces? 😊
That should be handled in the ImplementationOfDeprecatedInterfaceRule
. But tests are missing! Haven't thought about that.
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 don't think so, you need a rule that processes Interface_::class
for that.
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.
You're right 🙂
I'll make sure it works!
@ondrejmirtes Sure! I'll fix the issues by this weekend then! |
Cool, looking forward to that 😊 |
Is there a way in PHPStan to resolve a class name in a certain scope? Something that supports
|
@iluuu1994 Try |
@ondrejmirtes Knew it was somewhere 😆 Thanks! |
920f7f4
to
7a8f54b
Compare
@ondrejmirtes That's it! I'm still unsure if the implementation of the Other than that, it's ready for another review. |
Thinking more and more about this, I feel it doesn't really belong to strict-rules. |
Yeah I'm also thinking that this might be better as a separate package, it's also huge amount of code 😊 I really wouldn't want to all of this to go to waste, @iluuu1994 what would you think if I created something like |
@Majkl578 I don't know, deprecating code does signalize that you should get rid of the dependency sooner or later. Also, both Phan and Psalm have deprecation warnings out of the box. @ondrejmirtes It probably wouldn't get used quite as much if it requires you to add it manually. But hey, it's your project 🤷♂️ Whatever you see fit is fine. |
On the other hand, you can always ignore it using |
I mean, again, whatever you think is best is fine. If someone is consciously looking for the extension they are gonna find it. We could also mention it on the main repo so that it's easier to discover. |
As said earlier, not all deprecations are fixable or avoidable.
This is kinda selfish, heh. It would be used by people who are interested, not forcibly be people who are not. (And trust me, there is a LOT of such consumers, like Symfony bundles that are supporting SF 2.x + 3.x + 4.x simultaneously).
This is the correct assumption. |
That's hardly what I meant. I was my assuming the new deprecation rules add value to the majority of users of this repository (which might be wrong, we can argue about that). By your logic every new rule has to be opt-in because you didn't specifically enable it. |
Well, even then, I can only repeat myself:
Absolutely not, and please don't question my logic, I never said anthing like that. |
@iluuu1994 This is really awesome body of work! 😊 I'm gonna create an extra package for it and feature it in phpstan/phpstan release notes, README and also in the article, so don't worry, people will know about it 😊 |
@iluuu1994 BTW one more thing that should be changed but we can do that in the new repo: When returning error messages, you're using the original class/function/method names as mentioned in the code. You should use
|
@ondrejmirtes Sure! Let me know when you created the repository so I can migrate the code. |
@iluuu1994 I've sent you an invite to the repo https://github.com/phpstan/phpstan-deprecation-rules 😊 There's a basic skeleton ready. Let me know once the code is there so I can set up Travis CI. |
@ondrejmirtes The code is ready, you can set up Travis CI 😊 In the meantime I'll adjust the rules to use the reflection name for reporting errors. |
I fixed the problem with |
Awesome! The rule now checks for a deprecated scope as well. Didn't think of anonymous classes when writing that rule. |
OK, closing the PR here, we can continue the discussion over at the new repo 😊 BTW I'm gonna tag 0.10 there some time tonight, so don't be surprised 😊 |
Sure, thanks man! |
Already added this to Doctrine Migrations and it looks great! 🎉 |
@Majkl578 Nice! Glad to hear! |
No description provided.