Just a thought - since you have added useful function level comments it would be good to turn them into a comment block before the function - ie. 

/**
 * This function is called from CRM_Contribute_Form_AdditionalPayment::postProcess, used for recording additional payments (partial payments as well as refunded payments).
* @param integer $contributionId is the invoice contribution id (got created after processing participant payment)

etc
 *