When it becomes hardly usable, or after about five years since I bought it.
I've had three smartphones in my life so far: a Nokia X6 that lasted me little more than two years, a good phone which I chose because it had lots of storage for music, but after about two years the music app started randomly crashing and it also had other weird bugs and an unsupported system that made me change it. In late 2013 I bought an iPhone 5c that lasted me close to five years, not because it was unusable but battery life was degrading and I was looking for a larger screen when I found a massive discount on an iPhone 8 Plus in mid-2018, and that's the same phone I still use. Ideally it should last me until at least spring 2023, but I already know I'll not change the battery if it degrades too much as I'm moving away from closed-source ecosystems and I'll switch to another phone with a custom Android ROM, probably LineageOS or /e/. Or a Pinephone, but I doubt it'll be an option as a daily driver in the near future.