From 3ee969110c3636f623a6549eb5257d926f46d561 Mon Sep 17 00:00:00 2001 From: Henk Poley Date: Wed, 9 Jul 2025 04:07:32 +0200 Subject: [PATCH] Fix cast detection for method calls --- .../toStringCall/ToStringCallInspection.kt | 10 ++++++ .../toStringCall/ToStringCallIssue212Test.kt | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/test/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallIssue212Test.kt diff --git a/src/main/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallInspection.kt b/src/main/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallInspection.kt index 41ebc82..11ac18c 100644 --- a/src/main/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallInspection.kt +++ b/src/main/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallInspection.kt @@ -25,6 +25,13 @@ class ToStringCallInspection : PhpCleanInspection() { return object : PhpElementVisitor() { override fun visitPhpVariable(variable: Variable) { if (context.match(variable.parent)) { + if (variable.parent is UnaryExpression) { + val unary = variable.parent as UnaryExpression + val parent = unary.parent + if (parent is MethodReference && parent.classReference == unary) { + return + } + } if (IsSingleClassType().match(variable)) { holder.registerProblem( variable, @@ -51,6 +58,9 @@ class ToStringCallInspection : PhpCleanInspection() { } override fun visitPhpMethodReference(reference: MethodReference) { + if (reference.parent is UnaryExpression && reference.classReference is Variable) { + return + } if (context.match(reference.parent)) { val resolve = reference.resolve() if (resolve is Function && resolve.name != "__toString") { diff --git a/src/test/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallIssue212Test.kt b/src/test/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallIssue212Test.kt new file mode 100644 index 0000000..30cbe86 --- /dev/null +++ b/src/test/kotlin/com/funivan/idea/phpClean/inspections/toStringCall/ToStringCallIssue212Test.kt @@ -0,0 +1,31 @@ +package com.funivan.idea.phpClean.inspections.toStringCall + +import com.funivan.idea.phpClean.BaseInspectionTest +import kotlin.test.Test + +class ToStringCallIssue212Test : BaseInspectionTest() { + @Test + fun testCastFunctionCallFromObject() { + assert( + ToStringCallInspection(), + """ + returnsBlaFoo(); + """ + ) + } +}