<commit_before><?php

namespace AppBundle\Twig;

use AppBundle\Enum\Countries;
use Symfony\Component\Translation\TranslatorInterface;
use AppBundle\Entity\User;

/**
 * Class AppExtension
 * @package AppBundle\Twig
 */
class AppExtension extends \Twig_Extension
{
    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @param TranslatorInterface $translator
     */
    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * @return array
     */
    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('country_name', [$this, 'countryName']),
            new \Twig_SimpleFilter('pronoun', [$this, 'pronoun']),
        ];
    }

    /**
     * @return array
     */
    public function getFunctions()
    {
        return array(
            'want_to_learn_name'        => new \Twig_Function_Method($this, 'wantToLearnName'),
            'user_category_name_string' => new \Twig_Function_Method($this, 'userCategoryNameString'),
            'user_matched_categories'   => new \Twig_Function_Method($this, 'userMatchedCategories')
        );
    }

    /**
     * @param string $countryCode
     *
     * @return string
     */
    public function countryName($countryCode)
    {
        return Countries::getName($countryCode);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'app_extension';
    }

    /**
     * @param string $gender
     *
     * @return string
     */
    public function pronoun($gender)
    {
        if ('M' == $gender) {
            return 'han';
        } elseif ('F' == $gender) {
            return 'hon';
        }

        return 'hen';
    }

    /**
     * @param bool $wantToLearn
     * @return string
     */
    public function wantToLearnName($wantToLearn)
    {
        return $wantToLearn? $this->translator->trans('New'): $this->translator->trans('Established');
    }

    /**
     * @param User $user
     * @return string
     */
    public function userCategoryNameString(User $user)
    {
        $categoryNames  = array_values($user->getCategoryNames());

        if (count($categoryNames) > 1) {
            $lastCategory   = array_pop($categoryNames);
            $categories = implode(', ', $categoryNames) .' '.  $this->translator->trans('and') .' '. $lastCategory;
        } else {
            $categories = implode(', ', $categoryNames);
        }

        return $categories;
    }

    /**
     * @param User $matchUser
     * @param User $user
     * @return string
     */
    public function userMatchedCategories(User $matchUser, User $user)
    {
        $matches = [];

        foreach ($matchUser->getCategoryNames() as $id => $name) {
            foreach ($user->getCategoryNames() as $userCatId => $userCatName) {
                if ($id == $userCatId) {
                    $matches[] = $userCatName;
                    break;
                }
            }
        }

        if (count($matches) > 1) {
            $lastCategory   = array_pop($matches);
            $categories = implode(', ', $matches) .' '.  $this->translator->trans('and') .' '. $lastCategory;
        } else {
            $categories = implode(', ', $matches);
        }

        return $categories;
    }


}<commit_msg>Refactored to string categories<commit_after><?php

namespace AppBundle\Twig;

use AppBundle\Enum\Countries;
use Symfony\Component\Translation\TranslatorInterface;
use AppBundle\Entity\User;

/**
 * Class AppExtension
 * @package AppBundle\Twig
 */
class AppExtension extends \Twig_Extension
{
    /**
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @param TranslatorInterface $translator
     */
    public function __construct(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

    /**
     * @return array
     */
    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('country_name', [$this, 'countryName']),
            new \Twig_SimpleFilter('pronoun', [$this, 'pronoun']),
        ];
    }

    /**
     * @return array
     */
    public function getFunctions()
    {
        return array(
            'want_to_learn_name'        => new \Twig_Function_Method($this, 'wantToLearnName'),
            'user_category_name_string' => new \Twig_Function_Method($this, 'userCategoryNameString'),
            'user_matched_categories'   => new \Twig_Function_Method($this, 'userMatchedCategories')
        );
    }

    /**
     * @param string $countryCode
     *
     * @return string
     */
    public function countryName($countryCode)
    {
        return Countries::getName($countryCode);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'app_extension';
    }

    /**
     * @param string $gender
     *
     * @return string
     */
    public function pronoun($gender)
    {
        if ('M' == $gender) {
            return 'han';
        } elseif ('F' == $gender) {
            return 'hon';
        }

        return 'hen';
    }

    /**
     * @param bool $wantToLearn
     * @return string
     */
    public function wantToLearnName($wantToLearn)
    {
        return $wantToLearn? $this->translator->trans('New'): $this->translator->trans('Established');
    }

    /**
     * @param User $user
     * @return string
     */
    public function userCategoryNameString(User $user)
    {
        $categoryNames  = array_values($user->getCategoryNames());

        return $this->toStringCategories($categoryNames);
    }

    /**
     * @param User $matchUser
     * @param User $user
     * @return string
     */
    public function userMatchedCategories(User $matchUser, User $user)
    {
        $matches = [];

        foreach ($matchUser->getCategoryNames() as $id => $name) {
            foreach ($user->getCategoryNames() as $userCatId => $userCatName) {
                if ($id == $userCatId) {
                    $matches[] = $userCatName;
                    break;
                }
            }
        }

        return $this->toStringCategories($matches);
    }

    /**
     * @param array $categories
     * @return array|string
     */
    private function toStringCategories(array $categories)
    {
        if (count($categories) > 1) {
            $lastCategory   = array_pop($categories);
            $categories = implode(', ', $categories) .' '.  $this->translator->trans('and') .' '. $lastCategory;
        } else {
            $categories = implode(', ', $categories);
        }

        return $categories;
    }
}
{'fraction_non_alphanumeric': 0.1357153580543462, 'fraction_numerical': 0.00045038282540159135, 'mean_word_length': 1.9861048857014791, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, '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': '7302286', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1898, 'n_words': 372}