Issues (3)

src/Service/Form/FormErrorsSerializer.php (1 issue)

Labels
Severity
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Service\Form;
13
14
use Symfony\Component\Form\FormInterface;
15
16
class FormErrorsSerializer
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $errors;
22
23
    /**
24
     * @param FormInterface $form
25
     *
26
     * @return array
27
     */
28 4
    public function serialize(FormInterface $form): array
29
    {
30 4
        $this->errors = $this->serializeErrors($form);
31
32 4
        return $this->errors;
33
    }
34
35
    /**
36
     * @param FormInterface $form
37
     *
38
     * @return array
39
     */
40 4
    protected function serializeErrors(FormInterface $form): array
41
    {
42 4
        $errors = [];
43 4
        foreach ($form->getErrors() as $error) {
44 4
            $errors[] = $error->getMessage();
0 ignored issues
show
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            /** @scrutinizer ignore-call */ 
45
            $errors[] = $error->getMessage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        }
46
47 4
        foreach ($form->all() as $childForm) {
48 4
            if ($childForm instanceof FormInterface) {
49 4
                if ($childErrors = $this->serializeErrors($childForm)) {
50 4
                    $errors[$childForm->getName()] = $childErrors;
51
                }
52
            }
53
        }
54
55 4
        return $errors;
56
    }
57
}
58