private function updateFileCleanly(JsonFile $jsonFile, array $new, $requireKey, $removeKey, $sortPackages, $isPostInstall)
    {
        $composerJson = $jsonFile->read();
        $manipulator = new JsonManipulator($composerJson);

        foreach ($new as $package => $constraint) {
            $constraint = $isPostInstall ? $this->findBestVersionForPackage($package, $constraint) : $constraint;
            if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) {
                return false;
            }
            if (!$manipulator->removeSubNode($removeKey, $package)) {
                return false;
            }
        }
        $jsonFile->put($manipulator->getContents());

        return true;
    }