From 7a5120c4f7dee23cf9cc826bc80c29439e6e4575 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 24 Jun 2022 12:37:33 +0200 Subject: [PATCH] Run method call prohibition analyzer when cloning --- .../Statements/Expression/CloneAnalyzer.php | 18 ++++++++++---- tests/DeprecatedAnnotationTest.php | 24 +++++++++++++++++++ tests/InternalAnnotationTest.php | 22 +++++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/CloneAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/CloneAnalyzer.php index efd9fc3dbd1..0774533b4e6 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/CloneAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/CloneAnalyzer.php @@ -6,6 +6,7 @@ use Psalm\CodeLocation; use Psalm\Context; use Psalm\Internal\Analyzer\MethodAnalyzer; +use Psalm\Internal\Analyzer\Statements\Expression\Call\Method\MethodCallProhibitionAnalyzer; use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer; use Psalm\Internal\Analyzer\StatementsAnalyzer; use Psalm\Internal\MethodIdentifier; @@ -39,6 +40,7 @@ public static function analyze( return false; } + $location = new CodeLocation($statements_analyzer->getSource(), $stmt); $stmt_expr_type = $statements_analyzer->node_data->getType($stmt->expr); if ($stmt_expr_type) { @@ -71,7 +73,7 @@ public static function analyze( $does_method_exist = $codebase_methods->methodExists( $clone_method_id, $context->calling_method_id, - new CodeLocation($statements_analyzer->getSource(), $stmt) + $location ); $is_method_visible = MethodAnalyzer::isMethodVisible( $clone_method_id, @@ -81,6 +83,14 @@ public static function analyze( if ($does_method_exist && !$is_method_visible) { $invalid_clones[] = $clone_type_part->getId(); } else { + MethodCallProhibitionAnalyzer::analyze( + $codebase, + $context, + $clone_method_id, + $statements_analyzer->getNamespace(), + $location, + $statements_analyzer->getSuppressedIssues() + ); $possibly_valid = true; $immutable_cloned = true; } @@ -108,7 +118,7 @@ public static function analyze( IssueBuffer::maybeAdd( new MixedClone( 'Cannot clone mixed', - new CodeLocation($statements_analyzer->getSource(), $stmt) + $location ), $statements_analyzer->getSuppressedIssues() ); @@ -119,7 +129,7 @@ public static function analyze( IssueBuffer::maybeAdd( new PossiblyInvalidClone( 'Cannot clone ' . $invalid_clones[0], - new CodeLocation($statements_analyzer->getSource(), $stmt) + $location ), $statements_analyzer->getSuppressedIssues() ); @@ -127,7 +137,7 @@ public static function analyze( IssueBuffer::maybeAdd( new InvalidClone( 'Cannot clone ' . $invalid_clones[0], - new CodeLocation($statements_analyzer->getSource(), $stmt) + $location ), $statements_analyzer->getSuppressedIssues() ); diff --git a/tests/DeprecatedAnnotationTest.php b/tests/DeprecatedAnnotationTest.php index edcdf81c5eb..02f21cc9914 100644 --- a/tests/DeprecatedAnnotationTest.php +++ b/tests/DeprecatedAnnotationTest.php @@ -26,6 +26,16 @@ public static function barBar(): void { } }', ], + 'deprecatedCloneMethod' => [ + 'code' => ' [ 'code' => ' 'DeprecatedMethod', ], + 'deprecatedCloneMethodWithCall' => [ + 'code' => ' 'DeprecatedMethod', + ], 'deprecatedClassWithStaticCall' => [ 'code' => ' 'The method A\Foo::barBar is internal to A but called from B\Bat', ], + 'internalCloneMethodWithCall' => [ + 'code' => ' 'The method A\Foo::__clone is internal to A but called from B\Bat', + ], 'internalMethodWithCallFromRootNamespace' => [ 'code' => '