Run totallyGuarded() once outside of loop.

The boolean returned by totallyGuarded() will never change during the loop. Running the method outside of the foreach avoids one unnecessary count() and two checks for each iteration.
Merge pull request #3100 from Youpie/patch-1

Run totallyGuarded() once outside of loop.