There is a bit of a misconception here.
Blocking Terrain does not result in an Occupied Square. Squares are occupied by creatures, not objects. PHB 283.
There is nothing in the Blocking Terrain portion of the DMG to suggest that blocking terrain uses the official "Occupied Square" terminology.
The occupied terminology is important for things such as picking up an item in an unoccupied square. If blocking terrain was "occupied", a creature couldn't pick up an item off of a shelf in that square according to RAW since creatures can only pick up items in an unoccupied square.
So yes, a phasing creature can end its move in a solid wall.
You're reading RAW, not RAI. Such an interpretation makes that situation doable, but such a situation is definitely unfun and unfair.
First of all, it says that creatures occupy squares, but it doesn't actually say that large objects can't occupy squares. You can't take that to mean objects don't, just as you can't assume that, for example, elves aren't smart because you know for a fact that all eladrin are smart. Just because all eladrin are doesn't mean that all elves can't be either; just because all creatures occupy squares doesn't mean that all objects can't either.
Second, if a wall takes up the entire square and has a shelf on it,
technically the shelf is in the square that the creature is in when next to the shelf. Small objects like that and dropped items do not occupy the square; they are too tiny. Medium objects and larger, I would say, occupy squares. Small creatures can occupy squares since they are moving around, but Small objects (like certain types of difficult terrain) are still and so a creature can enter that space. Tiny creatures don't even really occupy spaces until there's four of them in the same space; why would Tiny objects?
Third, if a phasing creature can end its move in a space with a wall (hypothetically assuming that blocking terrain does not count as occupied squares), how does it make sense that it can't end its move in a space with a creature, which takes up less room and is probably easier to go through? If it can phase through anything, even when staying still, why are creatures harder to phase through than walls? On top of that, since when does phasing grant you the ability to see through three feet of soldi stone? A creature who is in a square of blocking terrain has its eyes inside the square, yet the game mechanics allow it to see through that? I think the problem lies not in the line of sight, but as I said before, in allowing phasing creatures to end their move in a square of blocking terrain. I'm sure someone could come up with a flavor reason for all this, but I could just as easily come up with a flavor reason for the opposite to be true.
Finally, getting pwned by a ghost who should be a standard encounter but is impossible to beat is not fun. Fun should come before simulation and story, and to make that situation fun, you shouldn't allow the creature to end its movement in a wall. Maybe you will allow it when the PCs are spying on a dragon to reward their creative thinking, because that would be fun. At the very least, you could only have the monster do that once or twice. But the whole encounter? It becomes unfun, and so the rules, whatever they actually are, should be thrown out the window to make it fun. I can't stress that enough.
Sorry about the rambling. It's just I can't see how you could encounter that situation and not realize immediately that something's wrong. If the designers ever intended such a grind to happen, I would be rendered speechless for months. I think that they probably intended for phasing creatures to be unable to attack from inside a wall, except in key story-related instances at the DM and players' whim, but not as a part of fundamental rules.