<commit_before><?php

declare(strict_types=1);

/*
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Endroid\PropertyAccess;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor as BasePropertyAccessor;

class PropertyAccessor
{
    /**
     * @var BasePropertyAccessor
     */
    protected $accessor;

    /**
     * @var ExpressionLanguage
     */
    protected $language;

    /**
     * Creates a new instance.
     */
    public function __construct()
    {
        $this->accessor = PropertyAccess::createPropertyAccessor();
        $this->language = new ExpressionLanguage();
    }

    /**
     * Returns the value at the given path.
     *
     * @param mixed  $object
     * @param string $path
     *
     * @return mixed
     */
    public function getValue($object, $path)
    {
        $paths = preg_split('#(\[((?>[^\[\]]+)|(?R))*\])#i', $path, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

        for ($i = 0; $i < count($paths); ++$i) {
            $path = trim($paths[$i], '.');
            if ('[' == substr($path, 0, 1)) {
                ++$i;
            }
            if (preg_match('#[^a-z0-9\.\]\[]+#i', $path)) {
                $expression = trim($path, '[]');
                $object = $this->filter($object, $expression);
            } else {
                $object = $this->accessor->getValue($object, $path);
            }

            if (null === $object) {
                break;
            }
        }

        return $object;
    }

    /**
     * Sets the value at the given path.
     *
     * @param mixed  $object
     * @param string $path
     * @param mixed  $value
     */
    public function setValue($object, $path, $value)
    {
        $this->accessor->setValue($object, $path, $value);
    }

    /**
     * Returns the objects filtered by the given path value.
     *
     * @param array  $objects
     * @param string $expression
     *
     * @return array
     */
    public function filter(array $objects, $expression)
    {
        $filteredObjects = [];

        foreach ($objects as $key => $object) {
            try {
                if (@$this->language->evaluate('object.'.$expression, ['object' => $object])) {
                    $filteredObjects[] = $object;
                }
            } catch (\Exception $exception) {
                // Property does not exist: ignore this item
            }
        }

        return $filteredObjects;
    }
}
<commit_msg>Fix strong typing<commit_after><?php

declare(strict_types=1);

/*
 * (c) <NAME> <<EMAIL>>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Endroid\PropertyAccess;

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor as BasePropertyAccessor;

class PropertyAccessor
{
    /**
     * @var BasePropertyAccessor
     */
    protected $accessor;

    /**
     * @var ExpressionLanguage
     */
    protected $language;

    /**
     * Creates a new instance.
     */
    public function __construct()
    {
        $this->accessor = PropertyAccess::createPropertyAccessor();
        $this->language = new ExpressionLanguage();
    }

    /**
     * Returns the value at the given path.
     *
     * @param mixed  $object
     * @param string $path
     *
     * @return mixed
     */
    public function getValue($object, $path)
    {
        $paths = preg_split('#(\[((?>[^\[\]]+)|(?R))*\])#i', $path, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

        for ($i = 0; $i < count($paths); ++$i) {
            $path = trim($paths[$i], '.');
            if ('[' == substr($path, 0, 1)) {
                ++$i;
            }
            if (preg_match('#[^a-z0-9\.\]\[]+#i', $path)) {
                $expression = trim($path, '[]');
                $object = $this->filter($object, $expression);
            } else {
                $object = $this->accessor->getValue($object, $path);
            }

            if (null === $object) {
                break;
            }
        }

        return $object;
    }

    /**
     * Sets the value at the given path.
     *
     * @param mixed  $object
     * @param string $path
     * @param mixed  $value
     */
    public function setValue($object, $path, $value)
    {
        $this->accessor->setValue($object, $path, $value);
    }

    /**
     * Returns the objects filtered by the given path value.
     *
     * @param array  $objects
     * @param string $expression
     *
     * @return array
     */
    public function filter(array $objects, $expression)
    {
        $filteredObjects = [];

        foreach ($objects as $key => $object) {
            try {
                if (@$this->language->evaluate('object.'.$expression, ['object' => $object])) {
                    $filteredObjects[] = $object;
                }
            } catch (\Exception $exception) {
                // Property does not exist: ignore this item
            }
        }

        return $filteredObjects;
    }
}

{'fraction_non_alphanumeric': 0.14674934235249906, 'fraction_numerical': 0.0024426907177752723, 'mean_word_length': 1.864908503767492, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '1326210', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1503, 'n_words': 370}