protected function filterPackageMap(array $packageMap, PackageInterface $mainPackage)
    {
        $packages = array();
        $include = array();

        foreach ($packageMap as $item) {
            $package = $item[0];
            $name = $package->getName();
            $packages[$name] = $package;
        }

        $add = function (PackageInterface $package) use (&$add, $packages, &$include) {
            foreach ($package->getRequires() as $link) {
                $target = $link->getTarget();
                if (!isset($include[$target])) {
                    $include[$target] = true;
                    if (isset($packages[$target])) {
                        $add($packages[$target]);
                    }
                }
            }
        };
        $add($mainPackage);

        return array_filter(
            $packageMap,
            function ($item) use ($include) {
                $package = $item[0];
                foreach ($package->getNames() as $name) {
                    if (isset($include[$name])) {
                        return true;
                    }
                }

                return false;
            }
        );
    }