<commit_before><?php

namespace MediaMonks\RestApiBundle\Exception;

use MediaMonks\RestApiBundle\Response\Error;
use MediaMonks\RestApiBundle\Util\StringUtil;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;

class FormValidationException extends \Exception
{
    const ERROR_TYPE_GENERAL = 'general';
    const ERROR_TYPE_CSRF = 'csrf';

    /**
     * @var FormInterface
     */
    protected $form;

    /**
     * FormValidationException constructor.
     * @param FormInterface $form
     * @param int|string $message
     * @param \Exception|string $code
     */
    public function __construct(
        FormInterface $form,
        $message = Error::MESSAGE_FORM_VALIDATION,
        $code = Error::CODE_FORM_VALIDATION
    ) {
        $this->form    = $form;
        $this->message = $message;
        $this->code    = $code;
    }

    /**
     * @return array
     */
    public function getFieldErrors()
    {
        return $this->getErrorMessages($this->form);
    }

    /**
     * @param FormInterface $form
     * @return array
     */
    protected function getErrorMessages(FormInterface $form)
    {
        $errors = [];
        foreach ($form->getErrors() as $error) {
            if (empty($error)) {
                continue;
            }
            if ($form->isRoot()) {
                $errors[] = $this->toErrorArray($error);
            } else {
                $errors[] = $this->toErrorArray($error, $form);
            }
        }
        foreach ($form->all() as $child) {
            if (empty($child)) {
                continue;
            }
            if (!$child->isValid()) {
                foreach ($this->getErrorMessages($child) as $error) {
                    $errors[] = $error;
                }
            }
        }

        return $errors;
    }

    /**
     * @param FormError|null $error
     * @param FormInterface|null $form
     * @return array
     */
    protected function toErrorArray(FormError $error = null, FormInterface $form = null)<commit_msg>- Changed variable not to be null<commit_after><?php

namespace MediaMonks\RestApiBundle\Exception;

use MediaMonks\RestApiBundle\Response\Error;
use MediaMonks\RestApiBundle\Util\StringUtil;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;

class FormValidationException extends \Exception
{
    const ERROR_TYPE_GENERAL = 'general';
    const ERROR_TYPE_CSRF = 'csrf';

    /**
     * @var FormInterface
     */
    protected $form;

    /**
     * FormValidationException constructor.
     * @param FormInterface $form
     * @param int|string $message
     * @param \Exception|string $code
     */
    public function __construct(
        FormInterface $form,
        $message = Error::MESSAGE_FORM_VALIDATION,
        $code = Error::CODE_FORM_VALIDATION
    ) {
        $this->form    = $form;
        $this->message = $message;
        $this->code    = $code;
    }

    /**
     * @return array
     */
    public function getFieldErrors()
    {
        return $this->getErrorMessages($this->form);
    }

    /**
     * @param FormInterface $form
     * @return array
     */
    protected function getErrorMessages(FormInterface $form)
    {
        $errors = [];
        foreach ($form->getErrors() as $error) {
            if (empty($error)) {
                continue;
            }
            if ($form->isRoot()) {
                $errors[] = $this->toErrorArray($error);
            } else {
                $errors[] = $this->toErrorArray($error, $form);
            }
        }
        foreach ($form->all() as $child) {
            if (empty($child)) {
                continue;
            }
            if (!$child->isValid()) {
                foreach ($this->getErrorMessages($child) as $error) {
                    $errors[] = $error;
                }
            }
        }

        return $errors;
    }

    /**
     * @param FormError $error
     * @param FormInterface|null $form
     * @return array
     */
    protected function toErrorArray(FormError $error, FormInterface $form = null)
{'fraction_non_alphanumeric': 0.12518301610541727, 'fraction_numerical': 0.0, 'mean_word_length': 1.8825597749648382, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '2254907', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1131, 'n_words': 236}