<commit_before><?php

namespace Knp\FriendlyContexts\Doctrine;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Inflector\Inflector;
use Knp\FriendlyContexts\Dictionary\Containable;

class EntityResolver
{
    use Containable;

    public function resolve(ObjectManager $entityManager, $name, $namespaces)
    {
        $results = [];
        if (is_array($namespaces)) {
            foreach ($namespaces as $namespace) {
                $results = array_merge($results, $this->resolve($entityManager, $name, $namespace) ?: []);
            }
        } else {
            $namespace = $namespaces;
            $allMetadata = $entityManager->getMetadataFactory()->getAllMetadata();
            $allClass = $this->getObjectReflector()->getReflectionsFromMetadata($allMetadata);

            foreach ($this->entityNameProposal($name) as $name) {
                $class = array_filter(
                    $allClass,
                    function ($e) use ($namespace, $name) {
                        $nameValid = strtolower($e->getShortName()) === strtolower($name);
                        return '' === $namespace
                            ? $nameValid
                            : $namespace === substr($e->getNamespaceName(), 0, strlen($namespace)) && $nameValid
                            ;
                    }
                );
                $results = array_merge($results, $class);
            }
        }

        return (0 < count($results)) ? $results : null;
    }

    public function getMetadataFromProperty(ObjectManager $entityManager, $entity, $property)
    {
        $metadata     = $this->getMetadataFromObject($entityManager, $entity);

        if (null !== $map = $this->getMappingFromMetadata($metadata->fieldMappings, $property)) {

            return $map;
        }

        if (null !== $map = $this->getMappingFromMetadata($metadata->associationMappings, $property)) {

            return $map;
        }

        throw new \RuntimeException(
            sprintf(
                'Can\'t find property %s or %s in class %s',<commit_msg>Refactor EntityResolver<commit_after><?php

namespace Knp\FriendlyContexts\Doctrine;

use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Inflector\Inflector;
use Knp\FriendlyContexts\Dictionary\Containable;

class EntityResolver
{
    use Containable;

    public function resolve(ObjectManager $entityManager, $name, $namespaces)
    {
        $results = [];
        if (is_array($namespaces)) {
            foreach ($namespaces as $namespace) {
                $results = array_merge($results, $this->resolve($entityManager, $name, $namespace) ?: []);
            }
        } else {
            $namespace = $namespaces;
            $allMetadata = $entityManager->getMetadataFactory()->getAllMetadata();
            $allClass = $this->getObjectReflector()->getReflectionsFromMetadata($allMetadata);

            foreach ($this->entityNameProposal($name) as $name) {
                $class = array_filter(
                    $allClass,
                    function ($e) use ($namespace, $name) {
                        $nameValid = strtolower($e->getShortName()) === strtolower($name);
                        return '' === $namespace
                            ? $nameValid
                            : $namespace === substr($e->getNamespaceName(), 0, strlen($namespace)) && $nameValid
                        ;
                    }
                );
                $results = array_merge($results, $class);
            }
        }

        return (0 < count($results)) ? $results : null;
    }

    public function getMetadataFromProperty(ObjectManager $entityManager, $entity, $property)
    {
        $metadata     = $this->getMetadataFromObject($entityManager, $entity);

        if (null !== $map = $this->getMappingFromMetadata($metadata->fieldMappings, $property)) {

            return $map;
        }

        if (null !== $map = $this->getMappingFromMetadata($metadata->associationMappings, $property)) {

            return $map;
        }

        throw new \RuntimeException(
            sprintf(
                'Can\'t find property %s or %s in class %s',
{'fraction_non_alphanumeric': 0.12619502868068833, 'fraction_numerical': 0.0009560229445506692, 'mean_word_length': 2.0107913669064748, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '1785887', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1058, 'n_words': 210}