Failed Conditions
Pull Request — release/1.0.0 (#4)
by Yo
01:35
created

MinMaxHelper::appendCollectionDoc()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 5
nop 2
dl 0
loc 8
ccs 6
cts 6
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
        $this->appendNumberMinMax($doc, $constraint);
54
55 7
        if ($constraint instanceof Assert\LessThan) {
56 1
            $doc->setInclusiveMax(false);
57 6
        } elseif ($constraint instanceof Assert\GreaterThan) {
58 1
            $doc->setInclusiveMin(false);
59
        }
60 7
    }
61
62
    /**
63
     * @param CollectionDoc $doc
64
     * @param Constraint    $constraint
65
     */
66 3
    private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
67
    {
68 3
        if ($constraint instanceof Assert\Count) {
69 3
            if (null !== $constraint->min) {
70 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

70
                $doc->setMinItem(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
71
            }
72 3
            if (null !== $constraint->max) {
73 2
                $doc->setMaxItem($constraint->max);
74
            }
75
        }
76 3
    }
77
78
    /**
79
     * @param NumberDoc $doc
80
     * @param Constraint $constraint
81
     */
82 7
    private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint)
83
    {
84 7
        if ($constraint instanceof Assert\Range) {
85 3
            if (null !== $constraint->min) {
86 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

86
                $doc->setMin(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
87
            }
88 3
            if (null !== $constraint->max) {
89 3
                $doc->setMax($constraint->max);
90
            }
91 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
92 4
            || $constraint instanceof Assert\LessThan
93
        ) {
94 2
            $doc->setMax($constraint->value);
95 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
96 2
            || $constraint instanceof Assert\GreaterThan
97
        ) {
98 2
            $doc->setMin($constraint->value);
99
        }
100 7
    }
101
}
102