<commit_before><?php

namespace PageExperience\ToolStack;

use AmpProject\Exception\NotImplemented;
use PageExperience\Analysis;
use PageExperience\ConfigurationProfile;
use PageExperience\Context;
use PageExperience\Tool;
use PageExperience\Tool\AnalysisTool;
use PageExperience\ToolStack;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;

/**
 * Stack of tools that execute asynchronously and in parallel.
 *
 * @package ampproject/px-toolbox
 */
final class ParallelToolStack implements ToolStack
{

    /**
     * Array of tools to execute in sequential order.
     *
     * @var array<Tool>
     */
    private $tools;

    /**
     * Instantiate a SequentialToolStack object.
     *
     * @param Tool ...$tools Tools to execute in sequential order.
     */
    public function __construct(Tool ...$tools)
    {
        $this->tools = $tools;
    }

    /**
     * Analyze a URL.
     *
     * @param Analysis             $analysis Current state of the analysis.
     * @param string               $url      URL to run an analysis for.
     * @param ConfigurationProfile $profile  Configuration profile to use for the analysis.
     * @param Context              $context  Current context of the analysis.
     * @return Analysis Adapted page experience analysis.
     */
    public function analyze(Analysis $analysis, $url, ConfigurationProfile $profile, Context $context)
    {
        $analysisMerger = new AnalysisMerger($analysis);
        $contextMerger  = new ContextMerger($context);

        foreach ($this->tools as $tool) {
            if (! $tool instanceof AnalysisTool) {
                continue;
            }

            $analysisClone = clone $analysis;
            $contextClone  = clone $context;

            Loop::futureTick(
                static function () use (
                    $tool,
                    $analysisClone,
                    $url,
                    $profile,
                    $contextClone,
                    $analysisMerger,
                    $contextMerger
                ) {
                    $newAnalysis = $tool->analyze($analysisClone, $url, $profile, $contextClone);
                    $analysisMerger->collectAnalysis($newAnalysis);
                    $contextMerger->collectContext($contextClone);
                }
            );
        }

        $context->replaceWith($contextMerger->mergeContext());

        return $analysisMerger->mergeAnalysis();
    }
<commit_msg>Add missing loop run<commit_after><?php

namespace PageExperience\ToolStack;

use AmpProject\Exception\NotImplemented;
use PageExperience\Analysis;
use PageExperience\ConfigurationProfile;
use PageExperience\Context;
use PageExperience\Tool;
use PageExperience\Tool\AnalysisTool;
use PageExperience\ToolStack;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;

/**
 * Stack of tools that execute asynchronously and in parallel.
 *
 * @package ampproject/px-toolbox
 */
final class ParallelToolStack implements ToolStack
{

    /**
     * Array of tools to execute in sequential order.
     *
     * @var array<Tool>
     */
    private $tools;

    /**
     * Instantiate a SequentialToolStack object.
     *
     * @param Tool ...$tools Tools to execute in sequential order.
     */
    public function __construct(Tool ...$tools)
    {
        $this->tools = $tools;
    }

    /**
     * Analyze a URL.
     *
     * @param Analysis             $analysis Current state of the analysis.
     * @param string               $url      URL to run an analysis for.
     * @param ConfigurationProfile $profile  Configuration profile to use for the analysis.
     * @param Context              $context  Current context of the analysis.
     * @return Analysis Adapted page experience analysis.
     */
    public function analyze(Analysis $analysis, $url, ConfigurationProfile $profile, Context $context)
    {
        $analysisMerger = new AnalysisMerger($analysis);
        $contextMerger  = new ContextMerger($context);

        foreach ($this->tools as $tool) {
            if (! $tool instanceof AnalysisTool) {
                continue;
            }

            $analysisClone = clone $analysis;
            $contextClone  = clone $context;

            Loop::futureTick(
                static function () use (
                    $tool,
                    $analysisClone,
                    $url,
                    $profile,
                    $contextClone,
                    $analysisMerger,
                    $contextMerger
                ) {
                    $newAnalysis = $tool->analyze($analysisClone, $url, $profile, $contextClone);
                    $analysisMerger->collectAnalysis($newAnalysis);
                    $contextMerger->collectContext($contextClone);
                }
            );
        }

        Loop::run();

        $context->replaceWith($contextMerger->mergeContext());

        return $analysisMerger->mergeAnalysis();
    }

{'fraction_non_alphanumeric': 0.0932527693856999, 'fraction_numerical': 0.0, 'mean_word_length': 1.9559523809523809, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6628300', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1225, 'n_words': 330}