Failed Conditions
Push — release/1.0.0 ( 254c29...0a806c )
by Yo
01:19
created

MinMaxHelper::append()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 2
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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\CollectionDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\NumberDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
9
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
10
11
/**
12
 * Class MinMaxHelper
13
 */
14
class MinMaxHelper
15
{
16
    /**
17
     * @param TypeDoc    $doc
18
     * @param Constraint $constraint
19
     */
20 13
    public function append(TypeDoc $doc, Constraint $constraint)
21
    {
22 13
        if ($doc instanceof StringDoc) {
23 3
            $this->appendStringDoc($doc, $constraint);
24 10
        } elseif ($doc instanceof NumberDoc) {
25 7
            $this->appendNumberDoc($doc, $constraint);
26 3
        } elseif ($doc instanceof CollectionDoc) {
27 3
            $this->appendCollectionDoc($doc, $constraint);
28
        }
29 13
    }
30
31
    /**
32
     * @param StringDoc  $doc
33
     * @param Constraint $constraint
34
     */
35 3
    private function appendStringDoc(StringDoc $doc, Constraint $constraint)
36
    {
37 3
        if ($constraint instanceof Assert\Length) {
38 3
            if (null !== $constraint->min) {
39 2
                $doc->setMinLength($constraint->min);
0 ignored issues
show
Bug introduced by
$constraint->min of type mixed is incompatible with the type integer expected by parameter $minLength of Yoanm\JsonRpcServerDoc\D...ringDoc::setMinLength(). ( Ignorable by Annotation )

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

39
                $doc->setMinLength(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
40
            }
41 3
            if (null !== $constraint->max) {
42 2
                $doc->setMaxLength($constraint->max);
43
            }
44
        }
45 3
    }
46
47
    /**
48
     * @param NumberDoc  $doc
49
     * @param Constraint $constraint
50
     */
51 7
    private function appendNumberDoc(NumberDoc $doc, Constraint $constraint)
52
    {
53 7
        if ($constraint instanceof Assert\Range) {
54 3
            if (null !== $constraint->min) {
55 2
                $doc->setMin($constraint->min);
0 ignored issues
show
Bug introduced by
$constraint->min of type mixed is incompatible with the type integer|double expected by parameter $min of Yoanm\JsonRpcServerDoc\D...ype\NumberDoc::setMin(). ( Ignorable by Annotation )

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

55
                $doc->setMin(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
56
            }
57 3
            if (null !== $constraint->max) {
58 3
                $doc->setMax($constraint->max);
59
            }
60 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
61 4
            || $constraint instanceof Assert\LessThan
62
        ) {
63 2
            $doc->setMax($constraint->value);
64 2
            if ($constraint instanceof Assert\LessThan) {
65 2
                $doc->setInclusiveMax(false);
66
            }
67 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
68 2
            || $constraint instanceof Assert\GreaterThan
69
        ) {
70 2
            $doc->setMin($constraint->value);
71 2
            if ($constraint instanceof Assert\GreaterThan) {
72 1
                $doc->setInclusiveMin(false);
73
            }
74
        }
75 7
    }
76
77
    /**
78
     * @param CollectionDoc $doc
79
     * @param Constraint    $constraint
80
     */
81 3
    private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
82
    {
83 3
        if ($constraint instanceof Assert\Count) {
84 3
            if (null !== $constraint->min) {
85 2
                $doc->setMinItem($constraint->min);
0 ignored issues
show
Bug introduced by
$constraint->min of type mixed is incompatible with the type integer expected by parameter $minItem of Yoanm\JsonRpcServerDoc\D...ectionDoc::setMinItem(). ( Ignorable by Annotation )

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

85
                $doc->setMinItem(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
86
            }
87 3
            if (null !== $constraint->max) {
88 2
                $doc->setMaxItem($constraint->max);
89
            }
90
        }
91 3
    }
92
}
93