Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(),
"""
<?php
class BlaFoo {
public function __toString(): string {
return 'BlaFoo';
}
}
class Another {
public function returnsBlaFoo(): BlaFoo {
return new BlaFoo();
}
public function __toString(): string {
return '';
}
}
${'$'}another = new Another();
(string)${'$'}another->returnsBlaFoo();
"""
)
}
}