The Error: "PhaseScriptExecution failed with a nonzero exit code" is a symptom not a root-cause.
Trying blind fixes like multiple clean and Xcode project file manipulation will not necessarily fix your problem. As long as you do not really know what the problem actually is.
In order to find the problem, in Xcode go to Report Navigator tab from left (last icon in Xcode 15) and select the Build that resulted in error. There you'll be able to see more about the actual error that is causing the PhaseScriptException.
In my case I was getting this error on a Kotlin Multi Platform project and it was cause due to java incompatibility version between os and Gradle.