override fun applyFix()

in src/main/kotlin/com/pestphp/pest/inspections/ChangeMultipleExpectCallsToChainableQuickFix.kt [17:56]


    override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
        var statement = descriptor.psiElement as? Statement ?: return
        var expectCall = statement.firstPsiChild as? MethodReference ?: return
        var replaceExpectCall = expectCall

        if (!expectCall.isExpectation()) {
            return
        }

        // Find the first expect call in the group
        while ((statement.prevPsiSibling as? Statement)?.isExpectation() == true) {
            statement = statement.prevPsiSibling as Statement
            expectCall = statement.firstPsiChild as MethodReference
            replaceExpectCall = expectCall
        }

        // Loop over all the next statement and merge together to one expect cal..
        var nextSibling = statement.nextPsiSibling as? Statement
        while (nextSibling != null) {
            val siblingMethodReference = nextSibling.firstPsiChild as? MethodReference ?: break

            if (!siblingMethodReference.isExpectation()) {
                break
            }

            // Replace expect with and on the next call.
            replaceExpectCall = PhpPsiElementFactory.createMethodReference(
                project,
                replaceExpectCall.text
                        + "\n->"
                        + siblingMethodReference.text.replaceFirst("expect", "and")
            )

            val oldSibling = nextSibling
            nextSibling = nextSibling.nextPsiSibling as? Statement
            oldSibling.delete()
        }

        expectCall.replace(replaceExpectCall)
    }