<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\Utils\Mapping;

use Darvin\Utils\Doctrine\ObjectManagerProviderInterface;
use Darvin\Utils\Mapping\AnnotationDriver\AnnotationDriverInterface;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Util\ClassUtils;

/**
 * Metadata factory
 */
class MetadataFactory implements MetadataFactoryInterface
{
    /**
     * @var \Darvin\Utils\Doctrine\ObjectManagerProviderInterface
     */
    private $objectManagerProvider;

    /**
     * @var \Darvin\Utils\Mapping\AnnotationDriver\AnnotationDriverInterface[]
     */
    private $annotationDrivers;

    /**
     * @var \Doctrine\Common\Persistence\ObjectManager
     */
    private $om;

    /**
     * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata[]
     */
    private $doctrineMeta;

    /**
     * @var array[]
     */
    private $extendedMeta;

    /**
     * @var string[]
     */
    private $identifiers;

    /**
     * @param \Darvin\Utils\Doctrine\ObjectManagerProviderInterface $objectManagerProvider Object manager provider
     */
    public function __construct(ObjectManagerProviderInterface $objectManagerProvider)
    {
        $this->objectManagerProvider = $objectManagerProvider;

        $this->om = null;
        $this->annotationDrivers = $this->doctrineMeta = $this->extendedMeta = $this->identifiers = [];
    }

    /**
     * {@inheritdoc}
     */
    public function addAnnotationDriver(AnnotationDriverInterface $annotationDriver)
    {
        $this->annotationDrivers[] = $annotationDriver;
    }

    /**
     * {@inheritdoc}
     */
    public function getIdentifier($objectOrClass)
    {
        $doctrineMeta = $this->getDoctrineMetadata($objectOrClass);

        if (!isset($this->identifiers[$doctrineMeta->getName()])) {
            $identifiers = $doctrineMeta->getIdentifier();
            $count = count($identifiers);

            if ($count > 1) {
                $message = sprintf(
                    'Only objects with single identifier are supported, class "%s" has %d identifiers.',
                    $doctrineMeta->getName(),
                    $count
                );

                throw new MappingException($message);
            }

            $this->identifiers[$doctrineMeta->getName()] = array_shift($identifiers);
        }

        return $this->identifiers[$doctrineMeta->getName()];
    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedMetadata($objectOrClass)
    {
        $doctrineMeta = $this->getDoctrineMetadata($objectOrClass);

        if (!isset($this->extendedMeta[$doctrineMeta->getName()])) {
            $meta = [];
            $om = $this->getObjectManager();

            foreach (array_reverse(class_parents($doctrineMeta->getName())) as $parent) {
                if ($om->getMetadataFactory()->hasMetadataFor($parent)) {<commit_msg>Replace "array_shift()" with "reset()" in extended metadata factory.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\Utils\Mapping;

use Darvin\Utils\Doctrine\ObjectManagerProviderInterface;
use Darvin\Utils\Mapping\AnnotationDriver\AnnotationDriverInterface;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Util\ClassUtils;

/**
 * Metadata factory
 */
class MetadataFactory implements MetadataFactoryInterface
{
    /**
     * @var \Darvin\Utils\Doctrine\ObjectManagerProviderInterface
     */
    private $objectManagerProvider;

    /**
     * @var \Darvin\Utils\Mapping\AnnotationDriver\AnnotationDriverInterface[]
     */
    private $annotationDrivers;

    /**
     * @var \Doctrine\Common\Persistence\ObjectManager
     */
    private $om;

    /**
     * @var \Doctrine\Common\Persistence\Mapping\ClassMetadata[]
     */
    private $doctrineMeta;

    /**
     * @var array[]
     */
    private $extendedMeta;

    /**
     * @var string[]
     */
    private $identifiers;

    /**
     * @param \Darvin\Utils\Doctrine\ObjectManagerProviderInterface $objectManagerProvider Object manager provider
     */
    public function __construct(ObjectManagerProviderInterface $objectManagerProvider)
    {
        $this->objectManagerProvider = $objectManagerProvider;

        $this->om = null;
        $this->annotationDrivers = $this->doctrineMeta = $this->extendedMeta = $this->identifiers = [];
    }

    /**
     * {@inheritdoc}
     */
    public function addAnnotationDriver(AnnotationDriverInterface $annotationDriver)
    {
        $this->annotationDrivers[] = $annotationDriver;
    }

    /**
     * {@inheritdoc}
     */
    public function getIdentifier($objectOrClass)
    {
        $doctrineMeta = $this->getDoctrineMetadata($objectOrClass);

        if (!isset($this->identifiers[$doctrineMeta->getName()])) {
            $identifiers = $doctrineMeta->getIdentifier();
            $count = count($identifiers);

            if ($count > 1) {
                $message = sprintf(
                    'Only objects with single identifier are supported, class "%s" has %d identifiers.',
                    $doctrineMeta->getName(),
                    $count
                );

                throw new MappingException($message);
            }

            $this->identifiers[$doctrineMeta->getName()] = reset($identifiers);
        }

        return $this->identifiers[$doctrineMeta->getName()];
    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedMetadata($objectOrClass)
    {
        $doctrineMeta = $this->getDoctrineMetadata($objectOrClass);

        if (!isset($this->extendedMeta[$doctrineMeta->getName()])) {
            $meta = [];
            $om = $this->getObjectManager();

            foreach (array_reverse(class_parents($doctrineMeta->getName())) as $parent) {
                if ($om->getMetadataFactory()->hasMetadataFor($parent)) {
{'fraction_non_alphanumeric': 0.12879148801016357, 'fraction_numerical': 0.001588057805304113, 'mean_word_length': 2.6341604154645126, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3714465', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1645, 'n_words': 309}