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

MinMaxHelper   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 68.25%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 120
ccs 43
cts 63
cp 0.6825
rs 9.28
c 0
b 0
f 0
wmc 39

5 Methods

Rating   Name   Duplication   Size   Complexity  
C appendCollectionDoc() 0 36 16
B appendNumberMinMax() 0 17 8
B appendStringDoc() 0 15 8
A append() 0 8 4
A appendNumberDoc() 0 8 3
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\ArrayDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\CollectionDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\NumberDoc;
9
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
10
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
11
12
/**
13
 * Class MinMaxHelper
14
 */
15
class MinMaxHelper
16
{
17
    /**
18
     * @param TypeDoc    $doc
19
     * @param Constraint $constraint
20
     */
21 13
    public function append(TypeDoc $doc, Constraint $constraint)
22
    {
23 13
        if ($doc instanceof StringDoc) {
24 3
            $this->appendStringDoc($doc, $constraint);
25 10
        } elseif ($doc instanceof NumberDoc) {
26 7
            $this->appendNumberDoc($doc, $constraint);
27 3
        } elseif ($doc instanceof CollectionDoc) {
28 3
            $this->appendCollectionDoc($doc, $constraint);
29
        }
30 13
    }
31
32
    /**
33
     * @param StringDoc  $doc
34
     * @param Constraint $constraint
35
     */
36 3
    private function appendStringDoc(StringDoc $doc, Constraint $constraint)
37
    {
38 3
        if ($constraint instanceof Assert\Length) {
39 3
            if (null !== $constraint->min) {
40 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

40
                $doc->setMinLength(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
41
            }
42 3
            if (null !== $constraint->max) {
43 3
                $doc->setMaxLength($constraint->max);
44
            }
45
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) {
46
            // Not blank so minimum 1 character
47
            $doc->setMinLength(1);
48
        } elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) {
49
            // Blank so maximum 0 character
50
            $doc->setMaxLength(0);
51
        }
52 3
    }
53
54
    /**
55
     * @param NumberDoc  $doc
56
     * @param Constraint $constraint
57
     */
58 7
    private function appendNumberDoc(NumberDoc $doc, Constraint $constraint)
59
    {
60 7
        $this->appendNumberMinMax($doc, $constraint);
61
62 7
        if ($constraint instanceof Assert\LessThan) {
63 1
            $doc->setInclusiveMax(false);
64 6
        } elseif ($constraint instanceof Assert\GreaterThan) {
65 1
            $doc->setInclusiveMin(false);
66
        }
67 7
    }
68
69
    /**
70
     * @param CollectionDoc $doc
71
     * @param Constraint    $constraint
72
     */
73 3
    private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint)
74
    {
75 3
        if ($constraint instanceof Assert\Choice) {
76
            if (null !== $constraint->min) {
77
                $doc->setMinItem($constraint->min);
78
            }
79
            if (null !== $constraint->max) {
80
                $doc->setMaxItem($constraint->max);
81
            }
82 3
        } elseif ($constraint instanceof Assert\Count) {
83 3
            if (null !== $constraint->min) {
84 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

84
                $doc->setMinItem(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
85
            }
86 3
            if (null !== $constraint->max) {
87 3
                $doc->setMaxItem($constraint->max);
88
            }
89
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) {
90
            // Not blank so minimum 1 item
91
            $doc->setMinItem(1);
92
        }/* // Documentation does not mention array, counter to NotBlank constraint
93
         elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
94
            // Blank so maximum 0 item
95
            $doc->setMaxItem(0);
96
        }*/
97 3
        if ($doc instanceof ArrayDoc) {
98
            if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) {
99
                $doc->setMinItem(
100
                    $constraint instanceof Assert\GreaterThanOrEqual
101
                        ? $constraint->value
102
                        : $constraint->value + 1
103
                );
104
            } elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) {
105
                $doc->setMaxItem(
106
                    $constraint instanceof Assert\LessThanOrEqual
107
                        ? $constraint->value
108
                        : $constraint->value - 1
109
                );
110
            }
111
        }
112 3
    }
113
114
    /**
115
     * @param NumberDoc $doc
116
     * @param Constraint $constraint
117
     */
118 7
    private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint)
119
    {
120 7
        if ($constraint instanceof Assert\Range) {
121 3
            if (null !== $constraint->min) {
122 2
                $doc->setMin($constraint->min);
1 ignored issue
show
Bug introduced by
$constraint->min of type mixed is incompatible with the type double|integer 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

122
                $doc->setMin(/** @scrutinizer ignore-type */ $constraint->min);
Loading history...
123
            }
124 3
            if (null !== $constraint->max) {
125 3
                $doc->setMax($constraint->max);
126
            }
127 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
128 4
            || $constraint instanceof Assert\LessThan
129
        ) {
130 2
            $doc->setMax($constraint->value);
131 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
132 2
            || $constraint instanceof Assert\GreaterThan
133
        ) {
134 2
            $doc->setMin($constraint->value);
135
        }
136 7
    }
137
}
138