Failed Conditions
Pull Request — release/1.0.0 (#7)
by Yo
02:47
created

StringDocHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 74
ccs 28
cts 32
cp 0.875
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B append() 0 68 9
1
<?php
2
namespace Yoanm\JsonRpcParamsSymfonyConstraintDoc\App\Helper;
3
4
use Symfony\Component\Validator\Constraint;
5
use Symfony\Component\Validator\Constraints as Assert;
6
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
8
9
/**
10
 * Class StringDocHelper
11
 */
12
class StringDocHelper
13
{
14
    /**
15
     * @param TypeDoc $doc
16
     * @param Constraint $constraint
17
     */
18 20
    public function append(TypeDoc $doc, Constraint $constraint)
19
    {
20
        // If format already defined or type is defined and is not a string nor scalar => give up
21 20
        if (!$doc instanceof StringDoc) {
22 1
            return;
23
        }
24
25 19
        $constraintClass = get_class($constraint);
26
        $constraintForFormatList = [
27 19
            Assert\Bic::class,
28
            Assert\CardScheme::class,
29
            Assert\Country::class,
30
            Assert\Currency::class,
31
            Assert\Date::class,
32
            Assert\DateTime::class,
33
            Assert\Email::class,
34
            Assert\File::class,
35
            Assert\Iban::class,
36
            Assert\Ip::class,
37
            Assert\Isbn::class,
38
            Assert\Issn::class,
39
            Assert\Language::class,
40
            Assert\Locale::class,
41
            Assert\Luhn::class,
42
            Assert\Time::class,
43
            Assert\Url::class,
44
            Assert\Uuid::class,
45
        ];
46
47 19
        if (in_array($constraintClass, $constraintForFormatList)) {
48 18
            if (Assert\DateTime::class === $constraintClass) {
49 1
                $format = 'datetime';
50
            } else {
51 17
                $format = lcfirst((new \ReflectionClass($constraint))->getShortName());
52
            }
53 18
            $doc->setFormat($format);
54
55 18
            if ($constraint instanceof Assert\Uuid) {
56 1
                $formatDescription = sprintf(
57 1
                    '%s (%s)',
58 1
                    ucfirst($format),
59 1
                    implode(
60 1
                        ', ',
61 1
                        array_map(
62
                            function ($version) {
63 1
                                return sprintf('v%s', $version);
64 1
                            },
65 1
                            $constraint->versions
66
                        )
67
                    )
68
                );
69 1
                $doc->setDescription(
70 1
                    sprintf(
71 1
                        '%s%s%s',
72 1
                        $doc->getDescription(),
73 1
                        strlen($doc->getDescription()) ? ' ' : '',
74 18
                        $formatDescription
75
                    )
76
                );
77
            }
78 1
        } elseif ($constraint instanceof Assert\Regex) {
79 1
            $doc->setFormat($constraint->pattern);
80
        } elseif ($constraint instanceof Assert\Range) {
81
            // If it's a string range it must be a date range check (either it must be an integer or float value)
82
            $doc->setFormat('date');
83
        } elseif ($constraint instanceof Assert\Expression) {
84
            // If it's a string range it must be a date range check (either it must be an integer or float value)
85
            $doc->setFormat($constraint->expression);
86
        }
87 19
    }
88
}
89