From ae339717c92c322c353a8fa9004eb2600b267473 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:22:54 +0200 Subject: [PATCH 01/41] support deprecated magic __toString() in echo statement --- .../EchoDeprecatedToStringRule.php | 94 +++++++++++++++++++ .../EchoDeprecatedToStringRuleTest.php | 34 +++++++ .../echo-deprecated-magic-method-tostring.php | 17 ++++ 3 files changed, 145 insertions(+) create mode 100644 src/Rules/Deprecations/EchoDeprecatedToStringRule.php create mode 100644 tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php create mode 100644 tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php new file mode 100644 index 0000000..08f12e6 --- /dev/null +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -0,0 +1,94 @@ + + */ +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 + ); + } + } + + return $messages; + } + +} diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php new file mode 100644 index 0000000..9cd44f6 --- /dev/null +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -0,0 +1,34 @@ + + */ +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, + ] + ] + ); + } + +} diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php new file mode 100644 index 0000000..783c070 --- /dev/null +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -0,0 +1,17 @@ + Date: Sat, 1 May 2021 09:30:34 +0200 Subject: [PATCH 02/41] cleanup --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 08f12e6..a67db75 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -3,19 +3,12 @@ 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; +use PhpParser\Node\Stmt\Echo_; /** * @implements \PHPStan\Rules\Rule @@ -35,7 +28,7 @@ public function __construct(RuleLevelHelper $ruleLevelHelper) public function getNodeType(): string { - return Node\Stmt\Echo_::class; + return Echo_::class; } public function processNode(Node $node, Scope $scope): array From 0e7f1d1b3cb384c658ca15dc0316eb28d1d7e2ac Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:32:50 +0200 Subject: [PATCH 03/41] check against null --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index a67db75..bdd7d81 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -58,6 +58,11 @@ static function (Type $type): bool { } $classReflection = $type->getClassReflection(); + + if (null === $classReflection) { + continue; + } + $methodReflection = $classReflection->getNativeMethod('__toString', $scope); if (!$methodReflection->isDeprecated()->yes()) { From d5e32480555eb7c1b6afdc7d3925e4e7c1c87686 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:35:30 +0200 Subject: [PATCH 04/41] remove unnecessary $scope arg --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index bdd7d81..32ecc9b 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -63,7 +63,7 @@ static function (Type $type): bool { continue; } - $methodReflection = $classReflection->getNativeMethod('__toString', $scope); + $methodReflection = $classReflection->getNativeMethod('__toString'); if (!$methodReflection->isDeprecated()->yes()) { continue; From d555a07c4dc8944c50374896cf455e02b9610e74 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:41:14 +0200 Subject: [PATCH 05/41] wrap test into function() to prevent echoing the actual string --- .../data/echo-deprecated-magic-method-tostring.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 783c070..8eefafb 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -2,8 +2,11 @@ namespace CheckDeprecatedStaticMethodCall; -$bar = new MagicBar(); -echo $bar; +function () +{ + $bar = new MagicBar(); + echo $bar; +}; class MagicBar { From 8f530ffafb2c9bec75317b3d7da855d74f181618 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:56:02 +0200 Subject: [PATCH 06/41] Update EchoDeprecatedToStringRuleTest.php --- tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 9cd44f6..00f761e 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -25,7 +25,7 @@ public function testDeprecatedMagicMethodToStringCall(): void [ [ 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', - 6, + 8, ] ] ); From 8f3cb87d48b52ef5377bdae63b6b6596190de1b0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 09:57:29 +0200 Subject: [PATCH 07/41] fix cs --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 32ecc9b..c3fa1f5 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -3,12 +3,12 @@ namespace PHPStan\Rules\Deprecations; use PhpParser\Node; +use PhpParser\Node\Stmt\Echo_; use PHPStan\Analyser\Scope; -use PHPStan\Type\ObjectType; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\ErrorType; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; -use PhpParser\Node\Stmt\Echo_; /** * @implements \PHPStan\Rules\Rule @@ -16,9 +16,7 @@ class EchoDeprecatedToStringRule implements \PHPStan\Rules\Rule { - /** - * @var RuleLevelHelper - */ + /** @var RuleLevelHelper */ private $ruleLevelHelper; public function __construct(RuleLevelHelper $ruleLevelHelper) @@ -59,7 +57,7 @@ static function (Type $type): bool { $classReflection = $type->getClassReflection(); - if (null === $classReflection) { + if ($classReflection === null) { continue; } From 514e9043f4f51df942280cc4ccfeb87b28c36ecb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 10:01:06 +0200 Subject: [PATCH 08/41] fix cs --- tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 00f761e..0e335b0 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -26,7 +26,7 @@ public function testDeprecatedMagicMethodToStringCall(): void [ 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', 8, - ] + ], ] ); } From 2c04cf2788e7bcae4f8e0eec857775fb930d80b9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 10:15:56 +0200 Subject: [PATCH 09/41] support echo with string concat --- .../EchoDeprecatedToStringRule.php | 87 +++++++++++-------- .../EchoDeprecatedToStringRuleTest.php | 4 + .../echo-deprecated-magic-method-tostring.php | 1 + 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index c3fa1f5..fa63c3a 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -38,53 +38,70 @@ public function processNode(Node $node, Scope $scope): array $messages = []; foreach ($node->exprs as $key => $expr) { - if (!$expr instanceof Node\Expr\Variable) { - continue; - } + if ($expr instanceof Node\Expr\Variable) { + $message = $this->checkExpr($expr, $scope); - $type = $this->ruleLevelHelper->findTypeToCheck( - $scope, - $expr, - '', - static function (Type $type): bool { - return !$type->toString() instanceof ErrorType; + if ($message) { + $messages[] = $message; + } + } elseif ($expr instanceof Node\Expr\BinaryOp\Concat) { + $message = $this->checkExpr($expr->left, $scope); + if ($message) { + $messages[] = $message; } - )->getType(); - if (!$type instanceof ObjectType) { - continue; + $message = $this->checkExpr($expr->right, $scope); + if ($message) { + $messages[] = $message; + } } + } - $classReflection = $type->getClassReflection(); + return $messages; + } - if ($classReflection === null) { - continue; + private function checkExpr(Node $expr, Scope $scope): ?string + { + $type = $this->ruleLevelHelper->findTypeToCheck( + $scope, + $expr, + '', + static function (Type $type): bool { + return !$type->toString() instanceof ErrorType; } + )->getType(); + + if (!$type instanceof ObjectType) { + return null; + } - $methodReflection = $classReflection->getNativeMethod('__toString'); + $classReflection = $type->getClassReflection(); - if (!$methodReflection->isDeprecated()->yes()) { - continue; - } + if ($classReflection === null) { + return null; + } - $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 - ); - } + $methodReflection = $classReflection->getNativeMethod('__toString'); + + if (!$methodReflection->isDeprecated()->yes()) { + return null; } - return $messages; + $description = $methodReflection->getDeprecatedDescription(); + if ($description === null) { + return sprintf( + 'Call to deprecated method %s() of class %s.', + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName() + ); + } + + return sprintf( + "Call to deprecated method %s() of class %s:\n%s", + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName(), + $description + ); } } diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 0e335b0..a33f2ef 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -27,6 +27,10 @@ public function testDeprecatedMagicMethodToStringCall(): void 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', 8, ], + [ + 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', + 9, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 8eefafb..802a564 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -6,6 +6,7 @@ function () { $bar = new MagicBar(); echo $bar; + echo $bar . "hallo"; }; class MagicBar From 024edd4b5936c67172b35bf8adbb568920be32f8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 10:17:28 +0200 Subject: [PATCH 10/41] fix type error --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index fa63c3a..2a8636d 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -60,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array return $messages; } - private function checkExpr(Node $expr, Scope $scope): ?string + private function checkExpr(Node\Expr $expr, Scope $scope): ?string { $type = $this->ruleLevelHelper->findTypeToCheck( $scope, From fed9f4ff7fb5298a331fda74f93876e9d8194bd5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 11:33:22 +0200 Subject: [PATCH 11/41] Update tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php --- .../Deprecations/data/echo-deprecated-magic-method-tostring.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 802a564..40f1259 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -1,6 +1,6 @@ Date: Sat, 1 May 2021 11:35:29 +0200 Subject: [PATCH 12/41] Update EchoDeprecatedToStringRuleTest.php --- tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index a33f2ef..690203b 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -24,11 +24,11 @@ public function testDeprecatedMagicMethodToStringCall(): void [__DIR__ . '/data/echo-deprecated-magic-method-tostring.php'], [ [ - 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', 8, ], [ - 'Call to deprecated method __toString() of class CheckDeprecatedStaticMethodCall\MagicBar.', + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', 9, ], ] From 44179146959be883f82ecef5c3dd6de77a9ebd8d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 19:52:47 +0200 Subject: [PATCH 13/41] more testcoverage --- .../EchoDeprecatedToStringRuleTest.php | 8 ++++++++ .../echo-deprecated-magic-method-tostring.php | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 690203b..cf6758f 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -31,6 +31,14 @@ public function testDeprecatedMagicMethodToStringCall(): void 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', 9, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 12, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 13, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 40f1259..2bedc21 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -7,6 +7,10 @@ function () $bar = new MagicBar(); echo $bar; echo $bar . "hallo"; + + $barDesc = new MagicBarWithDesc(); + echo $barDesc; + echo $barDesc . "hallo"; }; class MagicBar @@ -19,3 +23,14 @@ public function __toString() return 'a string'; } } + +class MagicBarWithDesc +{ + /** + * @deprecated use XY instead. + */ + public function __toString() + { + return 'a string'; + } +} From f133d6ba979d21726d8c572701f46240806ae36b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 19:54:05 +0200 Subject: [PATCH 14/41] cover __toString() without deprecation --- .../data/echo-deprecated-magic-method-tostring.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 2bedc21..4b42e46 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -11,6 +11,10 @@ function () $barDesc = new MagicBarWithDesc(); echo $barDesc; echo $barDesc . "hallo"; + + $noDeps = new NoDeprecation(); + echo $noDeps; + echo $noDeps . "hallo"; }; class MagicBar @@ -34,3 +38,11 @@ public function __toString() return 'a string'; } } + +class NoDeprecation +{ + public function __toString() + { + return 'a string'; + } +} From 47c4a55373d4508b76342d98a6d89560d6f4cf03 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:06:34 +0200 Subject: [PATCH 15/41] added more testcoverage --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index cf6758f..bf5f301 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -32,13 +32,21 @@ public function testDeprecatedMagicMethodToStringCall(): void 9, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 12, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 10, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 13, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 14, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 15, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 4b42e46..fc7233a 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -7,14 +7,22 @@ function () $bar = new MagicBar(); echo $bar; echo $bar . "hallo"; + echo "la". ($bar."3") . "lu"; $barDesc = new MagicBarWithDesc(); echo $barDesc; echo $barDesc . "hallo"; + echo "la". ($barDesc."3") . "lu"; $noDeps = new NoDeprecation(); echo $noDeps; echo $noDeps . "hallo"; + echo "la". ($noDeps."3") . "lu"; + + echo "la". "le" . "lu"; + echo "la". 5 . "lu"; + echo "la". (5+3) . "lu"; + echo "la". (5*3) . "lu"; }; class MagicBar From f23f190cd966e8a7a906b1415006973bf2f03888 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:22:05 +0200 Subject: [PATCH 16/41] support nested expressions --- .../EchoDeprecatedToStringRule.php | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 2a8636d..8a5f653 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -36,30 +36,36 @@ public function processNode(Node $node, Scope $scope): array } $messages = []; - foreach ($node->exprs as $key => $expr) { - if ($expr instanceof Node\Expr\Variable) { - $message = $this->checkExpr($expr, $scope); - - if ($message) { - $messages[] = $message; - } - } elseif ($expr instanceof Node\Expr\BinaryOp\Concat) { - $message = $this->checkExpr($expr->left, $scope); - if ($message) { - $messages[] = $message; - } - - $message = $this->checkExpr($expr->right, $scope); - if ($message) { - $messages[] = $message; - } - } + $this->deepCheckExpr($expr, $scope, $messages); } return $messages; } + /** + * @param string[] $messages + * + * @return void + */ + private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages) + { + if ($expr instanceof Node\Expr\BinaryOp\Concat) { + $this->deepCheckExpr($expr->left, $scope, $messages); + $this->deepCheckExpr($expr->right, $scope, $messages); + } elseif ($expr instanceof Node\Expr) { + $message = $this->checkExpr($expr, $scope); + + if ($message) { + $messages[] = $message; + } + } else { + + + + } + } + private function checkExpr(Node\Expr $expr, Scope $scope): ?string { $type = $this->ruleLevelHelper->findTypeToCheck( From d720be68e04f4ff99ef5382bf308ad065d77594a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:23:27 +0200 Subject: [PATCH 17/41] fix cs --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 8a5f653..d3640fe 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -45,10 +45,8 @@ public function processNode(Node $node, Scope $scope): array /** * @param string[] $messages - * - * @return void */ - private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages) + private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): void { if ($expr instanceof Node\Expr\BinaryOp\Concat) { $this->deepCheckExpr($expr->left, $scope, $messages); @@ -61,8 +59,6 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages) } } else { - - } } From 2b38c72d9e4463971df96b3f3c1376a80879dc59 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:24:25 +0200 Subject: [PATCH 18/41] fix cs --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index d3640fe..9c771f9 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -44,6 +44,8 @@ public function processNode(Node $node, Scope $scope): array } /** + * @param Node\Expr $expr + * @param Scope $scope * @param string[] $messages */ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): void @@ -57,8 +59,6 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): if ($message) { $messages[] = $message; } - } else { - } } From 2ab82caf5ed4d630ac49308dd47b06e4f2c8616b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:32:40 +0200 Subject: [PATCH 19/41] cover explicit (string) cast --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index bf5f301..fb21d1d 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -36,8 +36,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 10, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 13, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 11, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -47,6 +47,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 15, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 16, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 17, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index fc7233a..e03339f 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -8,16 +8,19 @@ function () echo $bar; echo $bar . "hallo"; echo "la". ($bar."3") . "lu"; + echo "la". (string)($bar) . "lu"; $barDesc = new MagicBarWithDesc(); echo $barDesc; echo $barDesc . "hallo"; echo "la". ($barDesc."3") . "lu"; + echo "la". (string)($barDesc) . "lu"; $noDeps = new NoDeprecation(); echo $noDeps; echo $noDeps . "hallo"; echo "la". ($noDeps."3") . "lu"; + echo "la". (string)($noDeps) . "lu"; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From 1b43bf109f9cdcb776939a22f359439d1ba40125 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:37:56 +0200 Subject: [PATCH 20/41] impl string casts --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 9c771f9..1aa91b4 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -53,6 +53,8 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): if ($expr instanceof Node\Expr\BinaryOp\Concat) { $this->deepCheckExpr($expr->left, $scope, $messages); $this->deepCheckExpr($expr->right, $scope, $messages); + } elseif ($expr instanceof Node\Expr\Cast\String_) { + $this->deepCheckExpr($expr->expr, $scope, $messages); } elseif ($expr instanceof Node\Expr) { $message = $this->checkExpr($expr, $scope); From ea2038c14ed82c8950b12197ff1f7770f1b9bf61 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:45:55 +0200 Subject: [PATCH 21/41] cover assignment in expression --- .../Deprecations/EchoDeprecatedToStringRule.php | 2 +- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 1aa91b4..02ab367 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -53,7 +53,7 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): if ($expr instanceof Node\Expr\BinaryOp\Concat) { $this->deepCheckExpr($expr->left, $scope, $messages); $this->deepCheckExpr($expr->right, $scope, $messages); - } elseif ($expr instanceof Node\Expr\Cast\String_) { + } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign) { $this->deepCheckExpr($expr->expr, $scope, $messages); } elseif ($expr instanceof Node\Expr) { $message = $this->checkExpr($expr, $scope); diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index fb21d1d..5c87836 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -40,8 +40,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 11, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 14, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 12, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -55,6 +55,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 17, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 18, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 19, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index e03339f..3212f1d 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -9,18 +9,21 @@ function () echo $bar . "hallo"; echo "la". ($bar."3") . "lu"; echo "la". (string)($bar) . "lu"; + echo "la". ($x=$bar) . "lu"; $barDesc = new MagicBarWithDesc(); echo $barDesc; echo $barDesc . "hallo"; echo "la". ($barDesc."3") . "lu"; echo "la". (string)($barDesc) . "lu"; + echo "la". ($x=$barDesc) . "lu"; $noDeps = new NoDeprecation(); echo $noDeps; echo $noDeps . "hallo"; echo "la". ($noDeps."3") . "lu"; echo "la". (string)($noDeps) . "lu"; + echo "la". ($x=$noDeps) . "lu"; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From a111c5f6c4c8cd378dee9bc7fc47c63970b86c92 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 20:56:15 +0200 Subject: [PATCH 22/41] cover Node\Expr\AssignOp --- .../Deprecations/EchoDeprecatedToStringRule.php | 2 +- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 02ab367..e5f29ab 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -53,7 +53,7 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): if ($expr instanceof Node\Expr\BinaryOp\Concat) { $this->deepCheckExpr($expr->left, $scope, $messages); $this->deepCheckExpr($expr->right, $scope, $messages); - } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign) { + } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignOp) { $this->deepCheckExpr($expr->expr, $scope, $messages); } elseif ($expr instanceof Node\Expr) { $message = $this->checkExpr($expr, $scope); diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 5c87836..b82b68d 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -44,8 +44,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 12, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 15, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 13, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -63,6 +63,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 19, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 20, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 21, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 3212f1d..d5b7f5a 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -10,6 +10,7 @@ function () echo "la". ($bar."3") . "lu"; echo "la". (string)($bar) . "lu"; echo "la". ($x=$bar) . "lu"; + echo "la". ($x.=$bar) . "lu"; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -17,6 +18,7 @@ function () echo "la". ($barDesc."3") . "lu"; echo "la". (string)($barDesc) . "lu"; echo "la". ($x=$barDesc) . "lu"; + echo "la". ($x.=$barDesc) . "lu"; $noDeps = new NoDeprecation(); echo $noDeps; @@ -24,6 +26,7 @@ function () echo "la". ($noDeps."3") . "lu"; echo "la". (string)($noDeps) . "lu"; echo "la". ($x=$noDeps) . "lu"; + echo "la". ($x.=$noDeps) . "lu"; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From 689ff9cb3a51b695ae827e4135734d82b3516efc Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:00:47 +0200 Subject: [PATCH 23/41] cover Expr\AssignOp\Coalesce --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index b82b68d..d3fd3dc 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -48,8 +48,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 13, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 16, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 14, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -71,6 +71,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 21, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 22, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 23, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index d5b7f5a..aa51491 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -11,6 +11,7 @@ function () echo "la". (string)($bar) . "lu"; echo "la". ($x=$bar) . "lu"; echo "la". ($x.=$bar) . "lu"; + echo "" ?: $bar; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -19,6 +20,7 @@ function () echo "la". (string)($barDesc) . "lu"; echo "la". ($x=$barDesc) . "lu"; echo "la". ($x.=$barDesc) . "lu"; + echo "" ?: $barDesc; $noDeps = new NoDeprecation(); echo $noDeps; @@ -27,6 +29,7 @@ function () echo "la". (string)($noDeps) . "lu"; echo "la". ($x=$noDeps) . "lu"; echo "la". ($x.=$noDeps) . "lu"; + echo "" ?: $noDeps; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From ca8c02bf3b17878703f7b6484bbd39616fe0ecd5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:05:52 +0200 Subject: [PATCH 24/41] cover Expr\BinaryOp\Equal --- .../Deprecations/EchoDeprecatedToStringRule.php | 2 +- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index e5f29ab..c9294bf 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array */ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): void { - if ($expr instanceof Node\Expr\BinaryOp\Concat) { + if ($expr instanceof Node\Expr\BinaryOp) { $this->deepCheckExpr($expr->left, $scope, $messages); $this->deepCheckExpr($expr->right, $scope, $messages); } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignOp) { diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index d3fd3dc..a564b9e 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -52,8 +52,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 14, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 17, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 15, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -79,6 +79,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 23, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 24, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 25, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index aa51491..7d48fa4 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -12,6 +12,7 @@ function () echo "la". ($x=$bar) . "lu"; echo "la". ($x.=$bar) . "lu"; echo "" ?: $bar; + echo "" == $bar; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -21,6 +22,7 @@ function () echo "la". ($x=$barDesc) . "lu"; echo "la". ($x.=$barDesc) . "lu"; echo "" ?: $barDesc; + echo "" == $barDesc; $noDeps = new NoDeprecation(); echo $noDeps; @@ -30,6 +32,7 @@ function () echo "la". ($x=$noDeps) . "lu"; echo "la". ($x.=$noDeps) . "lu"; echo "" ?: $noDeps; + echo "" == $noDeps; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From b6016a7a6baf8ae0ada572064e0ae59ad1649149 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:07:11 +0200 Subject: [PATCH 25/41] cover Expr\BinaryOp\Identical --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index a564b9e..b49ca4a 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -56,8 +56,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 15, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 18, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 16, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -87,6 +87,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 25, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 26, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 27, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 7d48fa4..fd92e11 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -13,6 +13,7 @@ function () echo "la". ($x.=$bar) . "lu"; echo "" ?: $bar; echo "" == $bar; + echo "" === $bar; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -23,6 +24,7 @@ function () echo "la". ($x.=$barDesc) . "lu"; echo "" ?: $barDesc; echo "" == $barDesc; + echo "" === $barDesc; $noDeps = new NoDeprecation(); echo $noDeps; @@ -33,6 +35,7 @@ function () echo "la". ($x.=$noDeps) . "lu"; echo "" ?: $noDeps; echo "" == $noDeps; + echo "" === $noDeps; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From 470ccd22ec9d6910ff1bdf841b27107a67e7402f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:09:20 +0200 Subject: [PATCH 26/41] cover Expr\BinaryOp\NotEqual --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index b49ca4a..52110d8 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -60,8 +60,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 16, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 19, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 17, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -95,6 +95,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 27, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 28, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 29, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index fd92e11..e56e49c 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -13,6 +13,7 @@ function () echo "la". ($x.=$bar) . "lu"; echo "" ?: $bar; echo "" == $bar; + echo "" != $bar; echo "" === $bar; $barDesc = new MagicBarWithDesc(); @@ -24,6 +25,7 @@ function () echo "la". ($x.=$barDesc) . "lu"; echo "" ?: $barDesc; echo "" == $barDesc; + echo "" != $barDesc; echo "" === $barDesc; $noDeps = new NoDeprecation(); @@ -35,6 +37,7 @@ function () echo "la". ($x.=$noDeps) . "lu"; echo "" ?: $noDeps; echo "" == $noDeps; + echo "" != $noDeps; echo "" === $noDeps; echo "la". "le" . "lu"; From 239be6ae5b3934c13b3889586d284bf6018a5251 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:10:59 +0200 Subject: [PATCH 27/41] cover Expr\BinaryOp\NotIdentical --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 52110d8..1542a93 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -64,8 +64,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 17, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 20, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 18, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -103,6 +103,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 29, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 30, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 31, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index e56e49c..523fbb0 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -15,6 +15,7 @@ function () echo "" == $bar; echo "" != $bar; echo "" === $bar; + echo "" !== $bar; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -27,6 +28,7 @@ function () echo "" == $barDesc; echo "" != $barDesc; echo "" === $barDesc; + echo "" !== $barDesc; $noDeps = new NoDeprecation(); echo $noDeps; @@ -39,6 +41,7 @@ function () echo "" == $noDeps; echo "" != $noDeps; echo "" === $noDeps; + echo "" !== $noDeps; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From eed7f6049d61d45cbd37529b15eb5a307e8ec970 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:12:31 +0200 Subject: [PATCH 28/41] cover Expr\BinaryOp\Spaceship --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 1542a93..f5ed399 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -68,8 +68,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 18, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 21, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 19, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -111,6 +111,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 31, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 32, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 33, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 523fbb0..f894c7f 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -16,6 +16,7 @@ function () echo "" != $bar; echo "" === $bar; echo "" !== $bar; + echo "" <=> $bar; $barDesc = new MagicBarWithDesc(); echo $barDesc; @@ -29,6 +30,7 @@ function () echo "" != $barDesc; echo "" === $barDesc; echo "" !== $barDesc; + echo "" <=> $barDesc; $noDeps = new NoDeprecation(); echo $noDeps; @@ -42,6 +44,7 @@ function () echo "" != $noDeps; echo "" === $noDeps; echo "" !== $noDeps; + echo "" <=> $noDeps; echo "la". "le" . "lu"; echo "la". 5 . "lu"; From 879df3802cdea301e8c3cb5a44402bf64d442f41 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:15:45 +0200 Subject: [PATCH 29/41] simplify --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index c9294bf..870cc12 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -55,7 +55,7 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): $this->deepCheckExpr($expr->right, $scope, $messages); } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignOp) { $this->deepCheckExpr($expr->expr, $scope, $messages); - } elseif ($expr instanceof Node\Expr) { + } else { $message = $this->checkExpr($expr, $scope); if ($message) { From 266bf341c514e1132a4008c0f1ab371a272edf6e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:19:52 +0200 Subject: [PATCH 30/41] cover Node\Scalar\Encapsed --- .../Deprecations/EchoDeprecatedToStringRule.php | 4 ++++ .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 870cc12..3898699 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -55,6 +55,10 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): $this->deepCheckExpr($expr->right, $scope, $messages); } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignOp) { $this->deepCheckExpr($expr->expr, $scope, $messages); + } elseif ($expr instanceof Node\Scalar\Encapsed) { + foreach($expr->parts as $part) { + $this->deepCheckExpr($part, $scope, $messages); + } } else { $message = $this->checkExpr($expr, $scope); diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index f5ed399..302534b 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -72,8 +72,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 19, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 22, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 20, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -119,6 +119,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 33, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 34, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 35, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index f894c7f..7e3627c 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -7,6 +7,7 @@ function () $bar = new MagicBar(); echo $bar; echo $bar . "hallo"; + echo "{$bar}"; echo "la". ($bar."3") . "lu"; echo "la". (string)($bar) . "lu"; echo "la". ($x=$bar) . "lu"; @@ -21,6 +22,7 @@ function () $barDesc = new MagicBarWithDesc(); echo $barDesc; echo $barDesc . "hallo"; + echo "{$barDesc}"; echo "la". ($barDesc."3") . "lu"; echo "la". (string)($barDesc) . "lu"; echo "la". ($x=$barDesc) . "lu"; @@ -35,6 +37,7 @@ function () $noDeps = new NoDeprecation(); echo $noDeps; echo $noDeps . "hallo"; + echo "{$noDeps}"; echo "la". ($noDeps."3") . "lu"; echo "la". (string)($noDeps) . "lu"; echo "la". ($x=$noDeps) . "lu"; From 4ae64bb2868d2c8a317db9ea79923eb5f23f8cc6 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:28:47 +0200 Subject: [PATCH 31/41] cover coalesce operator --- .../Deprecations/EchoDeprecatedToStringRuleTest.php | 12 ++++++++++-- .../data/echo-deprecated-magic-method-tostring.php | 3 +++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php index 302534b..f13e90a 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedToStringRuleTest.php @@ -76,8 +76,8 @@ public function testDeprecatedMagicMethodToStringCall(): void 20, ], [ - "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", - 23, + 'Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBar.', + 21, ], [ "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", @@ -127,6 +127,14 @@ public function testDeprecatedMagicMethodToStringCall(): void "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", 35, ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 36, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedToStringRule\MagicBarWithDesc:\nuse XY instead.", + 37, + ], ] ); } diff --git a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php index 7e3627c..ae9a442 100644 --- a/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php +++ b/tests/Rules/Deprecations/data/echo-deprecated-magic-method-tostring.php @@ -13,6 +13,7 @@ function () echo "la". ($x=$bar) . "lu"; echo "la". ($x.=$bar) . "lu"; echo "" ?: $bar; + echo $_GET['doesNotExist'] ?? $bar; echo "" == $bar; echo "" != $bar; echo "" === $bar; @@ -28,6 +29,7 @@ function () echo "la". ($x=$barDesc) . "lu"; echo "la". ($x.=$barDesc) . "lu"; echo "" ?: $barDesc; + echo $_GET['doesNotExist'] ?? $barDesc; echo "" == $barDesc; echo "" != $barDesc; echo "" === $barDesc; @@ -43,6 +45,7 @@ function () echo "la". ($x=$noDeps) . "lu"; echo "la". ($x.=$noDeps) . "lu"; echo "" ?: $noDeps; + echo $_GET['doesNotExist'] ?? $noDeps; echo "" == $noDeps; echo "" != $noDeps; echo "" === $noDeps; From 61c9b8f51ad632157c1163ec51bbda5ed63c23ee Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 1 May 2021 21:29:58 +0200 Subject: [PATCH 32/41] fix cs --- src/Rules/Deprecations/EchoDeprecatedToStringRule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php index 3898699..8eb0e9b 100644 --- a/src/Rules/Deprecations/EchoDeprecatedToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedToStringRule.php @@ -56,7 +56,7 @@ private function deepCheckExpr(Node\Expr $expr, Scope $scope, array &$messages): } elseif ($expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\Assign || $expr instanceof Node\Expr\AssignOp) { $this->deepCheckExpr($expr->expr, $scope, $messages); } elseif ($expr instanceof Node\Scalar\Encapsed) { - foreach($expr->parts as $part) { + foreach ($expr->parts as $part) { $this->deepCheckExpr($part, $scope, $messages); } } else { From c11446335ca2e8e492dc0a264fb4542bc28cbaed Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:09:40 +0200 Subject: [PATCH 33/41] extracted EchoDeprecatedBinaryOpToStringRule --- .../EchoDeprecatedBinaryOpToStringRule.php | 96 +++++++++++++++++++ ...EchoDeprecatedBinaryOpToStringRuleTest.php | 70 ++++++++++++++ ...recated-binaryop-magic-method-tostring.php | 57 +++++++++++ 3 files changed, 223 insertions(+) create mode 100644 src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php create mode 100644 tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php create mode 100644 tests/Rules/Deprecations/data/echo-deprecated-binaryop-magic-method-tostring.php diff --git a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php new file mode 100644 index 0000000..510e6f2 --- /dev/null +++ b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php @@ -0,0 +1,96 @@ + + */ +class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule +{ + + /** @var RuleLevelHelper */ + private $ruleLevelHelper; + + public function __construct(RuleLevelHelper $ruleLevelHelper) + { + $this->ruleLevelHelper = $ruleLevelHelper; + } + + public function getNodeType(): string + { + return Node\Expr\BinaryOp::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if (DeprecatedScopeHelper::isScopeDeprecated($scope)) { + return []; + } + + $messages = []; + $message = $this->checkExpr($node->left, $scope); + if ($message) { + $messages[] = $message; + } + $message = $this->checkExpr($node->right, $scope); + if ($message) { + $messages[] = $message; + } + + return $messages; + } + + private function checkExpr(Node\Expr $expr, Scope $scope): ?string + { + $type = $this->ruleLevelHelper->findTypeToCheck( + $scope, + $expr, + '', + static function (Type $type): bool { + return !$type->toString() instanceof ErrorType; + } + )->getType(); + + if (!$type instanceof ObjectType) { + return null; + } + + $classReflection = $type->getClassReflection(); + + if ($classReflection === null) { + return null; + } + + $methodReflection = $classReflection->getNativeMethod('__toString'); + + if (!$methodReflection->isDeprecated()->yes()) { + return null; + } + + $description = $methodReflection->getDeprecatedDescription(); + if ($description === null) { + return sprintf( + 'Call to deprecated method %s() of class %s.', + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName() + ); + } + + return sprintf( + "Call to deprecated method %s() of class %s:\n%s", + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName(), + $description + ); + } + +} diff --git a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php new file mode 100644 index 0000000..7fa8cbf --- /dev/null +++ b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php @@ -0,0 +1,70 @@ + + */ +class EchoDeprecatedBinaryOpToStringRuleTest extends \PHPStan\Testing\RuleTestCase +{ + + protected function getRule(): \PHPStan\Rules\Rule + { + $ruleLevelHelper = new RuleLevelHelper($this->createBroker(), true, false, true); + + return new EchoDeprecatedBinaryOpToStringRule($ruleLevelHelper); + } + + public function testDeprecatedMagicMethodToStringCall(): void + { + require_once __DIR__ . '/data/echo-deprecated-binaryop-magic-method-tostring.php'; + $this->analyse( + [__DIR__ . '/data/echo-deprecated-binaryop-magic-method-tostring.php'], + [ + [ + 'Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBar.', + 8, + ], + [ + 'Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBar.', + 9, + ], + [ + 'Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBar.', + 10, + ], + [ + 'Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBar.', + 11, + ], + [ + 'Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBar.', + 12, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBarWithDesc:\nuse XY instead.", + 15, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBarWithDesc:\nuse XY instead.", + 16, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBarWithDesc:\nuse XY instead.", + 17, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBarWithDesc:\nuse XY instead.", + 18, + ], + [ + "Call to deprecated method __toString() of class EchoDeprecatedBinaryOpToStringRule\MagicBarWithDesc:\nuse XY instead.", + 19, + ], + ] + ); + } + +} diff --git a/tests/Rules/Deprecations/data/echo-deprecated-binaryop-magic-method-tostring.php b/tests/Rules/Deprecations/data/echo-deprecated-binaryop-magic-method-tostring.php new file mode 100644 index 0000000..133611a --- /dev/null +++ b/tests/Rules/Deprecations/data/echo-deprecated-binaryop-magic-method-tostring.php @@ -0,0 +1,57 @@ + $bar; + + $barDesc = new MagicBarWithDesc(); + echo "" == $barDesc; + echo "" != $barDesc; + echo "" === $barDesc; + echo "" !== $barDesc; + echo "" <=> $barDesc; + + $noDeps = new NoDeprecation(); + echo "" == $noDeps; + echo "" != $noDeps; + echo "" === $noDeps; + echo "" !== $noDeps; + echo "" <=> $noDeps; +}; + +class MagicBar +{ + /** + * @deprecated + */ + public function __toString() + { + return 'a string'; + } +} + +class MagicBarWithDesc +{ + /** + * @deprecated use XY instead. + */ + public function __toString() + { + return 'a string'; + } +} + +class NoDeprecation +{ + public function __toString() + { + return 'a string'; + } +} From 3adc2830e445739d20b22aa825b34d093ed19895 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:15:26 +0200 Subject: [PATCH 34/41] register PHPStan\Rules\Deprecations\EchoDeprecatedBinaryOpToStringRule with bleeding edge switch --- rules.neon | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rules.neon b/rules.neon index a8f89b8..f9d3773 100644 --- a/rules.neon +++ b/rules.neon @@ -1,6 +1,8 @@ services: - class: PHPStan\Rules\Deprecations\DeprecatedClassHelper + - + class: PHPStan\Rules\Deprecations\EchoDeprecatedBinaryOpToStringRule rules: - PHPStan\Rules\Deprecations\AccessDeprecatedPropertyRule @@ -19,3 +21,7 @@ rules: - PHPStan\Rules\Deprecations\TypeHintDeprecatedInFunctionSignatureRule - PHPStan\Rules\Deprecations\UsageOfDeprecatedCastRule - PHPStan\Rules\Deprecations\UsageOfDeprecatedTraitRule + +conditionalTags: + PHPStan\Rules\Deprecations\EchoDeprecatedBinaryOpToStringRule: + phpstan.rules.rule: %featureToggles.bleedingEdge% From a4138a7306df8bd3232fa743ed31030f5dc88bc5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:23:33 +0200 Subject: [PATCH 35/41] take core-logic from CallToDeprecatedMethodRule --- .../Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php index 7fa8cbf..091739a 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php @@ -14,7 +14,7 @@ protected function getRule(): \PHPStan\Rules\Rule { $ruleLevelHelper = new RuleLevelHelper($this->createBroker(), true, false, true); - return new EchoDeprecatedBinaryOpToStringRule($ruleLevelHelper); + return new EchoDeprecatedBinaryOpToStringRule($this->createBroker(), $ruleLevelHelper); } public function testDeprecatedMagicMethodToStringCall(): void From c09043bf310cd1d8b13fcd51f63afd8e5f4d05c9 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:23:42 +0200 Subject: [PATCH 36/41] Update EchoDeprecatedBinaryOpToStringRule.php --- .../EchoDeprecatedBinaryOpToStringRule.php | 84 +++++++++---------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php index 510e6f2..cb63af0 100644 --- a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php @@ -3,25 +3,25 @@ namespace PHPStan\Rules\Deprecations; use PhpParser\Node; -use PhpParser\Node\Stmt\Echo_; use PHPStan\Analyser\Scope; +use PHPStan\Broker\Broker; use PHPStan\Rules\RuleLevelHelper; -use PHPStan\Type\ErrorType; -use PHPStan\Type\ObjectType; -use PHPStan\Type\Type; -use function PHPStan\dumpType; +use PHPStan\Type\TypeUtils; /** * @implements \PHPStan\Rules\Rule */ class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule { + /** @var Broker */ + private $broker; /** @var RuleLevelHelper */ private $ruleLevelHelper; - public function __construct(RuleLevelHelper $ruleLevelHelper) + public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper) { + $this->broker = $broker; $this->ruleLevelHelper = $ruleLevelHelper; } @@ -49,48 +49,42 @@ public function processNode(Node $node, Scope $scope): array return $messages; } - private function checkExpr(Node\Expr $expr, Scope $scope): ?string + private function checkExpr(Node\Expr $node, Scope $scope): ?string { - $type = $this->ruleLevelHelper->findTypeToCheck( - $scope, - $expr, - '', - static function (Type $type): bool { - return !$type->toString() instanceof ErrorType; + $methodCalledOnType = $scope->getType($node); + $referencedClasses = TypeUtils::getDirectClassNames($methodCalledOnType); + + foreach ($referencedClasses as $referencedClass) { + try { + $classReflection = $this->broker->getClass($referencedClass); + $methodReflection = $classReflection->getNativeMethod('__toString'); + + if (!$methodReflection->isDeprecated()->yes()) { + return null; + } + + $description = $methodReflection->getDeprecatedDescription(); + if ($description === null) { + return sprintf( + 'Call to deprecated method %s() of class %s.', + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName() + ); + } + + return sprintf( + "Call to deprecated method %s() of class %s:\n%s", + $methodReflection->getName(), + $methodReflection->getDeclaringClass()->getName(), + $description + ); + } catch (\PHPStan\Broker\ClassNotFoundException $e) { + // Other rules will notify if the class is not found + } catch (\PHPStan\Reflection\MissingMethodFromReflectionException $e) { + // Other rules will notify if the the method is not found } - )->getType(); - - if (!$type instanceof ObjectType) { - return null; - } - - $classReflection = $type->getClassReflection(); - - if ($classReflection === null) { - return null; - } - - $methodReflection = $classReflection->getNativeMethod('__toString'); - - if (!$methodReflection->isDeprecated()->yes()) { - return null; } - $description = $methodReflection->getDeprecatedDescription(); - if ($description === null) { - return sprintf( - 'Call to deprecated method %s() of class %s.', - $methodReflection->getName(), - $methodReflection->getDeclaringClass()->getName() - ); - } - - return sprintf( - "Call to deprecated method %s() of class %s:\n%s", - $methodReflection->getName(), - $methodReflection->getDeclaringClass()->getName(), - $description - ); + return null; } - } From 2ca2d511f113eb3bd2cc2ba83e6632c27b136412 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:25:40 +0200 Subject: [PATCH 37/41] fix cs --- src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php index cb63af0..20bb3b1 100644 --- a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php @@ -13,6 +13,7 @@ */ class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule { + /** @var Broker */ private $broker; @@ -87,4 +88,5 @@ private function checkExpr(Node\Expr $node, Scope $scope): ?string return null; } + } From 830f6d526bae10f2b8002375cf458d8736f8d07a Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:26:48 +0200 Subject: [PATCH 38/41] removed copy/paste leftovers --- .../Deprecations/EchoDeprecatedBinaryOpToStringRule.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php index 20bb3b1..9756da0 100644 --- a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php @@ -9,7 +9,7 @@ use PHPStan\Type\TypeUtils; /** - * @implements \PHPStan\Rules\Rule + * @implements \PHPStan\Rules\Rule */ class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule { @@ -17,13 +17,9 @@ class EchoDeprecatedBinaryOpToStringRule implements \PHPStan\Rules\Rule /** @var Broker */ private $broker; - /** @var RuleLevelHelper */ - private $ruleLevelHelper; - - public function __construct(Broker $broker, RuleLevelHelper $ruleLevelHelper) + public function __construct(Broker $broker) { $this->broker = $broker; - $this->ruleLevelHelper = $ruleLevelHelper; } public function getNodeType(): string From fef4fe7395840c5a90c62353ca24d07e74fae7ef Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:27:23 +0200 Subject: [PATCH 39/41] fix test generic --- .../Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php index 091739a..3be9017 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php @@ -5,7 +5,7 @@ use PHPStan\Rules\RuleLevelHelper; /** - * @extends \PHPStan\Testing\RuleTestCase + * @extends \PHPStan\Testing\RuleTestCase */ class EchoDeprecatedBinaryOpToStringRuleTest extends \PHPStan\Testing\RuleTestCase { From 583279a30fc001e3cdffae147c603bdc27e29138 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:27:54 +0200 Subject: [PATCH 40/41] removed no longer needed RuleLevelHeper from tests --- .../Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php index 3be9017..de3860c 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php @@ -12,9 +12,7 @@ class EchoDeprecatedBinaryOpToStringRuleTest extends \PHPStan\Testing\RuleTestCa protected function getRule(): \PHPStan\Rules\Rule { - $ruleLevelHelper = new RuleLevelHelper($this->createBroker(), true, false, true); - - return new EchoDeprecatedBinaryOpToStringRule($this->createBroker(), $ruleLevelHelper); + return new EchoDeprecatedBinaryOpToStringRule($this->createBroker()); } public function testDeprecatedMagicMethodToStringCall(): void From 867e2a568b96b288e5784d7582e6745c3af19c9f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 9 May 2021 08:29:42 +0200 Subject: [PATCH 41/41] fix analysis errors and cs --- src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php | 4 ++-- .../Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php index 9756da0..4b39be9 100644 --- a/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php +++ b/src/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRule.php @@ -3,9 +3,9 @@ namespace PHPStan\Rules\Deprecations; use PhpParser\Node; +use PhpParser\Node\Expr\BinaryOp; use PHPStan\Analyser\Scope; use PHPStan\Broker\Broker; -use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\TypeUtils; /** @@ -24,7 +24,7 @@ public function __construct(Broker $broker) public function getNodeType(): string { - return Node\Expr\BinaryOp::class; + return BinaryOp::class; } public function processNode(Node $node, Scope $scope): array diff --git a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php index de3860c..9a5e9e7 100644 --- a/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php +++ b/tests/Rules/Deprecations/EchoDeprecatedBinaryOpToStringRuleTest.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Deprecations; -use PHPStan\Rules\RuleLevelHelper; - /** * @extends \PHPStan\Testing\RuleTestCase */