Skip to content
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

support deprecated magic __toString() in echo statement #37

Open
wants to merge 41 commits into
base: 1.1.x
Choose a base branch
from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented May 1, 2021

with this change phpstan is able to detect cases, where object values are used in echo statements and implicitly call a magic and deprecated __toString method.

In case this new rule gets accepted, I would be willing to implement the same for the print statement.

refs phpstan/phpstan#4899

Comment on lines 70 to 88
if (!$methodReflection->isDeprecated()->yes()) {
continue;
}

$description = $methodReflection->getDeprecatedDescription();
if ($description === null) {
$messages[] = sprintf(
'Call to deprecated method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
} else {
$messages[] = sprintf(
"Call to deprecated method %s() of class %s:\n%s",
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName(),
$description
);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most of this code is inspired by CallToDeprecatedMethodRule

@ondrejmirtes
Copy link
Member

Hi, this is rather incomplete and does not have rules for other situations.

What I recommend in your situation:

  1. Delete the deprecated __toString() method from your code.
  2. Run PHPStan to see all the usages.
  3. Remove the usages.

@staabm
Copy link
Contributor Author

staabm commented May 1, 2021

Thx for your feedback.

I am not sure how I should take it.

I am aware there are a lot of more things to take care of, but just want to get in contact early, since the very basic case which is important to me is already covered.

Was your suggestion about how I should proceed to get this rule into shape so you might consider merging it, or was your intention to give me a hint of how I should handle a possible deprecated __toString() method in my real world project (and you think this PR is not worth finishing)?

Thanks again

@staabm
Copy link
Contributor Author

staabm commented May 1, 2021

I want ahead and added more coverage for more complex expressions.
Just realized that this needs a lot more then I initially expected 😅

In case someone knows a expression I am missing feel free to sent me an example.

* @param Scope $scope
* @param string[] $messages
*/
private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a followup step, I would extract this logic into a helper and add a separate rule for Print.
Additionally a Rule could be added for the string-concat case

@staabm
Copy link
Contributor Author

staabm commented May 9, 2021

hey ondrey,

thx again for your feedback. I tried to incorporate what I have understood so far.

I have extracted a new EchoDeprecatedBinaryOpToStringRule rule from the already existing one - I kept the initial implementation of EchoDeprecatedToStringRule untouched, but would extract more rules out of it, in case the newly added Rule looks like what you expect from it.

does this look good to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants