I think it's a bit of a game of luck, but they seem to engineer these phones to last ~3 years. If you are lucky, they will last longer, but if not, they'll die on you.
Same with the battery.
Also; there's a lot of confusion about how to keep the battery life up. Charge it often? Leave it drain, then charge? etc...
Anecdotally:
we are Samsung people so far; S2,S5,S7,S9 - of these, the S2's were proper workhorses, one is still around for visitors, the other we've finally chucked due to a completely demolished screen as well as a dead battery. But the battery does not last anymore in the 'good' one either. We've even dropped one into water (not supposed to be water resistant) and after drying it out it kept working.
My S5 was a bit of a flop, broke down on me, then later wife's S5 gave up. Twice. Mine was still within warranty, got fixed. Hers wasn't the 2nd time, so she got an S7 a couple of years back; that still lasts, altho the screen is battered (I don't know what's wrong with her, she brakes every screen, I don't
)
My replacement S5 lasted some 5 years I think when I've finally changed it, the battery life was crap on that as well. The phone originally went 2 days w/o a charge (I'm not using it much) but by the end, it barely got through a normal day.
Now the S9+ is great again, two days easy with my light usage, even when moving around, changing cells. Occasionally some fucker of an app takes a chunk out of it, but the battery seems pretty darn decent so far.