-
Notifications
You must be signed in to change notification settings - Fork 20
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
Closed
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
ae33971
support deprecated magic __toString() in echo statement
staabm 86ad5d0
cleanup
staabm 0e7f1d1
check against null
staabm d5e3248
remove unnecessary $scope arg
staabm d555a07
wrap test into function() to prevent echoing the actual string
staabm 8f530ff
Update EchoDeprecatedToStringRuleTest.php
staabm 8f3cb87
fix cs
staabm 514e904
fix cs
staabm 2c04cf2
support echo with string concat
staabm 024edd4
fix type error
staabm fed9f4f
Update tests/Rules/Deprecations/data/echo-deprecated-magic-method-tos…
staabm b410c0d
Update EchoDeprecatedToStringRuleTest.php
staabm 4417914
more testcoverage
staabm f133d6b
cover __toString() without deprecation
staabm 47c4a55
added more testcoverage
staabm f23f190
support nested expressions
staabm d720be6
fix cs
staabm 2b38c72
fix cs
staabm 2ab82ca
cover explicit (string) cast
staabm 1b43bf1
impl string casts
staabm ea2038c
cover assignment in expression
staabm a111c5f
cover Node\Expr\AssignOp
staabm 689ff9c
cover Expr\AssignOp\Coalesce
staabm ca8c02b
cover Expr\BinaryOp\Equal
staabm b6016a7
cover Expr\BinaryOp\Identical
staabm 470ccd2
cover Expr\BinaryOp\NotEqual
staabm 239be6a
cover Expr\BinaryOp\NotIdentical
staabm eed7f60
cover Expr\BinaryOp\Spaceship
staabm 879df38
simplify
staabm 266bf34
cover Node\Scalar\Encapsed
staabm 4ae64bb
cover coalesce operator
staabm 61c9b8f
fix cs
staabm c114463
extracted EchoDeprecatedBinaryOpToStringRule
staabm 3adc283
register PHPStan\Rules\Deprecations\EchoDeprecatedBinaryOpToStringRul…
staabm a4138a7
take core-logic from CallToDeprecatedMethodRule
staabm c09043b
Update EchoDeprecatedBinaryOpToStringRule.php
staabm 2ca2d51
fix cs
staabm 830f6d5
removed copy/paste leftovers
staabm fef4fe7
fix test generic
staabm 583279a
removed no longer needed RuleLevelHeper from tests
staabm 867e2a5
fix analysis errors and cs
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\VariableTypeHolder; | ||
use PHPStan\Broker\Broker; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\TypeUtils; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<Echo_> | ||
*/ | ||
class EchoDeprecatedToStringRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
/** | ||
* @var RuleLevelHelper | ||
*/ | ||
private $ruleLevelHelper; | ||
|
||
public function __construct(RuleLevelHelper $ruleLevelHelper) | ||
{ | ||
$this->ruleLevelHelper = $ruleLevelHelper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Echo_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { | ||
return []; | ||
} | ||
|
||
$messages = []; | ||
|
||
foreach ($node->exprs as $key => $expr) { | ||
if (!$expr instanceof Node\Expr\Variable) { | ||
continue; | ||
} | ||
|
||
$type = $this->ruleLevelHelper->findTypeToCheck( | ||
$scope, | ||
$expr, | ||
'', | ||
static function (Type $type): bool { | ||
return !$type->toString() instanceof ErrorType; | ||
} | ||
)->getType(); | ||
|
||
if (!$type instanceof ObjectType) { | ||
continue; | ||
} | ||
|
||
$classReflection = $type->getClassReflection(); | ||
$methodReflection = $classReflection->getNativeMethod('__toString', $scope); | ||
|
||
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 | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most of this code is inspired by |
||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Deprecations; | ||
|
||
use PHPStan\Rules\RuleLevelHelper; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<EchoDeprecatedToStringRule> | ||
*/ | ||
class EchoDeprecatedToStringRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
$ruleLevelHelper = new RuleLevelHelper($this->createBroker(), true, false, true); | ||
|
||
return new EchoDeprecatedToStringRule($ruleLevelHelper); | ||
} | ||
|
||
public function testDeprecatedMagicMethodToStringCall(): void | ||
{ | ||
require_once __DIR__ . '/data/echo-deprecated-magic-method-tostring.php'; | ||
$this->analyse( | ||
[__DIR__ . '/data/echo-deprecated-magic-method-tostring.php'], | ||
[ | ||
[ | ||
'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', | ||
6, | ||
] | ||
] | ||
); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace CheckDeprecatedStaticMethodCall; | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$bar = new MagicBar(); | ||
echo $bar; | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class MagicBar | ||
{ | ||
/** | ||
* @deprecated | ||
*/ | ||
public function __toString() | ||
{ | ||
return 'a string'; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.