<commit_before><?php

/**
 * This file is part of the XSLT2Processor package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Jdomenechb\XSLT2Processor\XPath;

use Jdomenechb\XSLT2Processor\XML\DOMNodeList;
use Jdomenechb\XSLT2Processor\XPath\FunctionImplementation\Fn\Current;
use Jdomenechb\XSLT2Processor\XSLT\Context\GlobalContext;
use Jdomenechb\XSLT2Processor\XSLT\Context\TemplateContext;

class XPathSelector extends AbstractXPath
{
    /**
     * @var ExpressionInterface
     */
    protected $selector;

    /**
     * @var ExpressionInterface
     */
    protected $expression;

    public static function parseXPath($string)
    {
        if (substr($string, -1) !== ']') {
            return false;
        }

        $eph = new Expression\ExpressionParserHelper();
        $parts = $eph->parseFirstLevelSubExpressions($string, '[', ']');

        if (count($parts) <= 1) {
            return false;
        }

        // Remove empty part at the end
        array_pop($parts);
        $selectorString = array_pop($parts);

        $factory = new Factory();
        $obj = new self();

        $obj->setSelector($factory->create($selectorString));

        // Strip the selector from the original string
        $string = substr($string, 0, -strlen($selectorString) - 2);

        $obj->setExpression($factory->create($string));

        return $obj;
    }

    public function toString()
    {
        return $this->getExpression()->toString() . '[' . $this->getSelector()->toString() . ']';
    }

    public function evaluate($context)
    {
        $resultArray = $this->getExpression()->evaluate($context)->toArray();
        $newResult = [];

        if ($resultArray && $this->getSelector() instanceof XPathNumber) {
            $newResult[] = $resultArray[$this->getSelector()->toString() - 1];
        } else {
            $stack = Current::getStack();

            foreach ($resultArray as $resultElement) {
                $stack->push($resultElement);

                $evaluation = $this->getSelector()->evaluate($resultElement);

                if (
                    (!$evaluation instanceof DOMNodeList && $evaluation)
                    || ($evaluation instanceof DOMNodeList && $evaluation->count())
                ) {
                    $newResult[] = $resultElement;
                }<commit_msg>Ignore out of bonds selector<commit_after><?php

/**
 * This file is part of the XSLT2Processor package.
 *
 * (c) <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Jdomenechb\XSLT2Processor\XPath;

use Jdomenechb\XSLT2Processor\XML\DOMNodeList;
use Jdomenechb\XSLT2Processor\XPath\FunctionImplementation\Fn\Current;
use Jdomenechb\XSLT2Processor\XSLT\Context\GlobalContext;
use Jdomenechb\XSLT2Processor\XSLT\Context\TemplateContext;

class XPathSelector extends AbstractXPath
{
    /**
     * @var ExpressionInterface
     */
    protected $selector;

    /**
     * @var ExpressionInterface
     */
    protected $expression;

    public static function parseXPath($string)
    {
        if (substr($string, -1) !== ']') {
            return false;
        }

        $eph = new Expression\ExpressionParserHelper();
        $parts = $eph->parseFirstLevelSubExpressions($string, '[', ']');

        if (count($parts) <= 1) {
            return false;
        }

        // Remove empty part at the end
        array_pop($parts);
        $selectorString = array_pop($parts);

        $factory = new Factory();
        $obj = new self();

        $obj->setSelector($factory->create($selectorString));

        // Strip the selector from the original string
        $string = substr($string, 0, -strlen($selectorString) - 2);

        $obj->setExpression($factory->create($string));

        return $obj;
    }

    public function toString()
    {
        return $this->getExpression()->toString() . '[' . $this->getSelector()->toString() . ']';
    }

    /**
     * @inheritdoc
     */
    public function evaluate($context)
    {
        $resultArray = $this->getExpression()->evaluate($context)->toArray();
        $newResult = [];

        if (
            $resultArray
            && $this->getSelector() instanceof XPathNumber
            && isset($resultArray[($index = $this->getSelector()->toString() - 1)])
        ) {
            $newResult[] = $resultArray[$index];
        } else {
            $stack = Current::getStack();

            foreach ($resultArray as $resultElement) {
                $stack->push($resultElement);

                $evaluation = $this->getSelector()->evaluate($resultElement);

                if (
                    (!$evaluation instanceof DOMNodeList && $evaluation)
                    || ($evaluation instanceof DOMNodeList && $evaluation->count())
                ) {
                    $newResult[] = $resultElement;
                }
{'fraction_non_alphanumeric': 0.13556967050416832, 'fraction_numerical': 0.004366812227074236, 'mean_word_length': 2.3042622950819673, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, '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': '2983999', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1358, 'n_words': 293}