extracted piece of code into a method

In order to fix #10421 I need to enable merge to take an option
so that it could merge without making the last where condition to win.

That fix would forever reside in 4-0-stable branch and would not be
merged to master since using scope without lambda has been dreprected.

I thought extracing the code into a method makes the code look better.
Hence the request to merge it in both master and 4-0-stable.

If there is any concern then this code can be merged only in
4-0-stable and that would be fine too.