ConstraintsNormalizer::normalize()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 9.2222
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Bdf\Form\View;
4
5
use Bdf\Form\Validator\ConstraintValueValidator;
6
use Bdf\Form\Validator\ValueValidatorInterface;
7
use Symfony\Component\Validator\Constraints\Count;
8
use Symfony\Component\Validator\Constraints\Length;
9
use Symfony\Component\Validator\Constraints\NotBlank;
10
11
/**
12
 * Normalize symfony constraints to array
13
 * This normalization process permit to filter unserializable values, and disociate validation business and view rendering
14
 */
15
final class ConstraintsNormalizer
16
{
17
    /**
18
     * @var array<class-string<\Symfony\Component\Validator\Constraint>, array<string, mixed>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<\Symf..., array<string, mixed>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<\Symfony\Component\Validator\Constraint>, array<string, mixed>>.
Loading history...
19
     */
20
    static private $constraints = [
21
        NotBlank::class => [],
22
        Length::class => ['min' => null, 'max' => null],
23
        Count::class => ['min' => null, 'max' => null],
24
    ];
25
26
    /**
27
     * Process normalization
28
     * The return value consists of and array with constraint class as key, and associative array of constraints attributes as value
29
     *
30
     * @param ValueValidatorInterface $validator
31
     *
32
     * @return array
33
     * @see FieldViewInterface::constraints()
34
     */
35 47
    public static function normalize(ValueValidatorInterface $validator): array
36
    {
37 47
        if (!$validator instanceof ConstraintValueValidator) {
38 1
            return [];
39
        }
40
41 47
        $normalizedConstraints = [];
42
43 47
        foreach ($validator->constraints() as $constraint) {
44 26
            $className = get_class($constraint);
45
46 26
            if (isset(self::$constraints[$className])) {
47 21
                $normalizedConstraints[$className] = array_intersect_key((array) $constraint, self::$constraints[$className]);
48 20
            } elseif (($option = $constraint->getDefaultOption()) !== null) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $option is correct as $constraint->getDefaultOption() targeting Symfony\Component\Valida...int::getDefaultOption() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49 20
                $value = $constraint->{$option};
50
51 20
                if (is_scalar($value)) {
52 8
                    $normalizedConstraints[$className] = [$option => $value];
53
                }
54
            }
55
        }
56
57 47
        return $normalizedConstraints;
58
    }
59
}
60