my guess here is the formula would be something like:
or to be more precise, it seems to me that if |A| ≠ |B|, but |B| is a substructure of |A| there is some element a of |A| not in |B|. assuming |B| is non-empty (perhaps this requires a separate case) we have some element b in |B|, and we have the automorphism:
f(x) = x, if x ≠ a,b
f(a) = b
f(b) = a
under which φ is not invariant.