Thus, the two verses combine these two rulings: upholding justice and bearing witness to it—whether the case concerns friends or foes.
The Almighty then says: “If he is rich or poor, Allah is nearer to them both.” That is: should the one against whom testimony is required be wealthy, and you hope that some benefit of his wealth will come back to you, so you refrain from testifying against him, or should he be poor, from whom you neither expect profit nor feel any fear—still, Allah has a greater claim (1) over them both than you do. He is their Lord and Patron, and they are both His servants (2) just as you are His servants. Therefore, do not court the favor of a rich man because of his riches, nor set your hopes (3) on a poor man because of his poverty; for Allah is nearer to them both than you are.
A finer meaning (4) may also be intended: people might shy away from establishing justice and bearing witness against either the rich or the poor—against the rich out of concern for his property, and against the poor because he owns nothing and seems of little consequence, so souls relax their duty toward him. They are therefore told: Allah is nearer to both the wealthy and the destitute than you are; He knows them better and is more merciful to them. So never abandon the truth or withhold testimony on behalf of either a rich man or a poor one.
The Exalted then says: “So do not follow caprice, lest you swerve from justice.” He thus forbids them from yielding to desires that would lead them to forsake fairness.