<commit_before><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license.
 */

namespace Roave\DeveloperTools\Renderer\Detail;

use Roave\DeveloperTools\Inspection\AggregateInspection;
use Roave\DeveloperTools\Inspection\EventInspection;
use Roave\DeveloperTools\Inspection\InspectionInterface;
use Roave\DeveloperTools\Renderer\BaseInspectionRenderer;
use Roave\DeveloperTools\Renderer\Util\HierarchyBuilder;
use Roave\DeveloperTools\Renderer\Util\TraceComparator;

/**
 * Renders a group of given events
 */
class DetailEventsRenderer extends BaseInspectionRenderer
{
    const PARAM_EVENTS_HIERARCHY = 'eventsHierarchy';

    /**
     * {@inheritDoc}
     */
    protected $supportedInspection = AggregateInspection::class;

    /**
     * {@inheritDoc}
     */
    protected $templateName = 'roave-developer-tools/detail/events';

    /**
     * {@inheritDoc}
     */
    public function canRender(InspectionInterface $inspection)
    {
        // all values in the AggregateInspection are EventInspection, and the aggregate inspection is not empty
        return parent::canRender($inspection)
            && array_filter(array_map(
                function (InspectionInterface $inspection) {
                    return $inspection instanceof EventInspection;
                },
                $inspection->getInspectionData()
            ));
    }

    /**
     * {@inheritDoc}
     */
    public function render(InspectionInterface $inspection)
    {
        $viewModel = parent::render($inspection);

        /* @var $inspection InspectionInterface */
        $inspections = $inspection->getInspectionData();
        $hierarchy   = $this->computeEventsHierarchy($inspections);

        return $viewModel->setVariable(static::PARAM_EVENTS_HIERARCHY, $hierarchy);
    }

    /**
     * @param EventInspection[] $inspections
     *
     * @return array
     */
    private function computeEventsHierarchy(array $inspections)
    {
        usort($inspections, [$this, 'compareEventTime']);

        $indexed = [];

        foreach ($inspections as $inspection) {
            $indexed[$inspection->getInspectionData()[EventInspection::PARAM_EVENT_ID]] = $inspection;
        }

        $callerMap       = [];
        $possibleParents = [];

        /* @var $indexed EventInspection[] */
        foreach ($indexed as $eventId => $inspection) {<commit_msg>Setting inspections (indexed by event identifier) in the view model<commit_after><?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license.
 */

namespace Roave\DeveloperTools\Renderer\Detail;

use Roave\DeveloperTools\Inspection\AggregateInspection;
use Roave\DeveloperTools\Inspection\EventInspection;
use Roave\DeveloperTools\Inspection\InspectionInterface;
use Roave\DeveloperTools\Renderer\BaseInspectionRenderer;
use Roave\DeveloperTools\Renderer\Util\HierarchyBuilder;
use Roave\DeveloperTools\Renderer\Util\TraceComparator;

/**
 * Renders a group of given events
 */
class DetailEventsRenderer extends BaseInspectionRenderer
{
    const PARAM_EVENTS_HIERARCHY    = 'eventsHierarchy';
    const PARAM_INDEXED_INSPECTIONS = 'indexedInspections';

    /**
     * {@inheritDoc}
     */
    protected $supportedInspection = AggregateInspection::class;

    /**
     * {@inheritDoc}
     */
    protected $templateName = 'roave-developer-tools/detail/events';

    /**
     * {@inheritDoc}
     */
    public function canRender(InspectionInterface $inspection)
    {
        // all values in the AggregateInspection are EventInspection, and the aggregate inspection is not empty
        return parent::canRender($inspection)
            && array_filter(array_map(
                function (InspectionInterface $inspection) {
                    return $inspection instanceof EventInspection;
                },
                $inspection->getInspectionData()
            ));
    }

    /**
     * {@inheritDoc}
     */
    public function render(InspectionInterface $inspection)
    {
        $viewModel = parent::render($inspection);

        /* @var $inspections EventInspection[] */
        $inspections = $inspection->getInspectionData();
        $hierarchy   = $this->computeEventsHierarchy($inspections);
        $indexed     = [];

        foreach ($inspections as $inspection) {
            $indexed[$inspection->getInspectionData()[EventInspection::PARAM_EVENT_ID]] = $inspection;
        }

        return $viewModel
            ->setVariable(static::PARAM_EVENTS_HIERARCHY, $hierarchy)
            ->setVariable(static::PARAM_INDEXED_INSPECTIONS, $indexed);
    }

    /**
     * @param EventInspection[] $inspections
     *
     * @return array
     */
    private function computeEventsHierarchy(array $inspections)
    {
        usort($inspections, [$this, 'compareEventTime']);

        $indexed = [];

        foreach ($inspections as $inspection) {
            $indexed[$inspection->getInspectionData()[EventInspection::PARAM_EVENT_ID]] = $inspection;
        }

        $callerMap       = [];
        $possibleParents = [];

        /* @var $indexed EventInspection[] */
        foreach ($indexed as $eventId => $inspection) {
{'fraction_non_alphanumeric': 0.09401083613999121, 'fraction_numerical': 0.0, 'mean_word_length': 3.239602731222843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6540981', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1812, 'n_words': 512}