protected function removeSymlinks(
        PackageInterface $package,
        $targetRoot,
        array $pathMap,
        $mode = self::INVALID_FAIL
    ) {
        if (empty($pathMap)) {
            return;
        }

        $packageRoot = $this->getInstallPath($package);
        $actions     = [];

        // Check the file map first and make sure we only remove our own symlinks.
        foreach ($pathMap as $sourcePath => $targetPath) {
            $source = $this->filesystem->normalizePath($packageRoot . ($sourcePath ? ('/'.$sourcePath) : ''));
            $target = $this->filesystem->normalizePath($targetRoot . '/' . $targetPath);

            if ($this->canRemoveSymlink($source, $target, $mode)) {
                $actions[] = $target;
            }
        }

        // Remove the symlinks if everything is ok.
        foreach ($actions as $target) {
            $this->logRemove($target);

            if (is_dir($target)) {
                $this->filesystem->removeDirectory($target);
            } else {
                $this->filesystem->unlink($target);
            }

            $this->removeEmptyDirectories(dirname($target), $targetRoot);
        }
    }