What is the ruling on completing a prayer if one joins an ongoing congregation mid-prayer when intending to perform a repeat prayer?

Chapter on the Times When Prayer is Prohibited

Al-Mughni

Book of Prayer

Book 3 · Issue 3 · Bab 9

Open in Qurani

Primary text

If one intends to repeat a prayer and only catches two rak'ahs with the congregation, it is permissible to conclude the prayer with them (i.e., perform Tasleem immediately) because the second prayer is considered Nafl. However, it is recommended to complete it to four rak'ahs because the intention was for the full four rak'ahs. This is based on the general principle, "What you miss, complete it."