Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

support deprecated magic __toString() in echo statement #37

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

Closed
staabm wants to merge 41 commits into phpstan:1.1.x from staabm:to-string

Conversation

@staabm
Copy link
Contributor

@staabm staabm commented May 1, 2021
edited
Loading

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

voku reacted with eyes emoji
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

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.

Copy link
Contributor Author

staabm commented May 1, 2021
edited
Loading

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

Copy link
Contributor Author

staabm commented May 1, 2021
edited
Loading

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

Copy link
Contributor Author

staabm commented May 9, 2021
edited
Loading

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?

Copy link
Contributor Author

staabm commented May 14, 2025

@staabm staabm deleted the to-string branch May 14, 2025 09:54
Copy link
Member

Oh, not really this case 😊 But it could all work together with the new virtual nodes you proposed.

Copy link
Contributor Author

staabm commented May 14, 2025

ok cool, so maybe we should re-open phpstan/phpstan#4899 so its not forgotten?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes requested changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /