Passed
Push — master ( 254c29...f2eb24 )
by Yo
03:16
created

MinMaxHelper::appendCollectionDoc()   C

Complexity

Conditions 15
Paths 30

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 22
nc 30
nop 2
dl 0
loc 35
ccs 23
cts 23
cp 1
crap 15
rs 5.9166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 23
    public function append(TypeDoc $doc, Constraint $constraint) : void
21
    {
22 23
        if ($doc instanceof StringDoc) {
23 5
            $this->appendStringDoc($doc, $constraint);
24 18
        } elseif ($doc instanceof NumberDoc) {
25 7
            $this->appendNumberDoc($doc, $constraint);
26 11
        } elseif ($doc instanceof CollectionDoc) {
27 11
            $this->appendCollectionDoc($doc, $constraint);
28
        }
29 23
    }
30
31
    /**
32
     * @param StringDoc  $doc
33
     * @param Constraint $constraint
34
     */
35 5
    private function appendStringDoc(StringDoc $doc, Constraint $constraint) : void
36
    {
37 5
        if ($constraint instanceof Assert\Length) {
38 3
            if (null !== $constraint->min) {
39 2
                $doc->setMinLength((int) $constraint->min);
40
            }
41 3
            if (null !== $constraint->max) {
42 3
                $doc->setMaxLength((int) $constraint->max);
43
            }
44 2
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinLength()) {
45
            // Not blank so minimum 1 character
46 1
            $doc->setMinLength(1);
47 1
        } elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxLength()) {
48
            // Blank so maximum 0 character
49 1
            $doc->setMaxLength(0);
50
        }
51 5
    }
52
53
    /**
54
     * @param NumberDoc  $doc
55
     * @param Constraint $constraint
56
     */
57 7
    private function appendNumberDoc(NumberDoc $doc, Constraint $constraint) : void
58
    {
59 7
        $this->appendNumberMinMax($doc, $constraint);
60
61 7
        if ($constraint instanceof Assert\LessThan) {
62 1
            $doc->setInclusiveMax(false);
63 6
        } elseif ($constraint instanceof Assert\GreaterThan) {
64 1
            $doc->setInclusiveMin(false);
65
        }
66 7
    }
67
68
    /**
69
     * @param CollectionDoc $doc
70
     * @param Constraint    $constraint
71
     */
72 11
    private function appendCollectionDoc(CollectionDoc $doc, Constraint $constraint) : void
73
    {
74 11
        if ($constraint instanceof Assert\Choice) {
75 3
            if (null !== $constraint->min) {
76 2
                $doc->setMinItem((int) $constraint->min);
77
            }
78 3
            if (null !== $constraint->max) {
79 3
                $doc->setMaxItem((int) $constraint->max);
80
            }
81 8
        } elseif ($constraint instanceof Assert\Count) {
82 3
            if (null !== $constraint->min) {
83 2
                $doc->setMinItem((int) $constraint->min);
84
            }
85 3
            if (null !== $constraint->max) {
86 3
                $doc->setMaxItem((int) $constraint->max);
87
            }
88 5
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) {
89
            // Not blank so minimum 1 item
90 1
            $doc->setMinItem(1);
91
        }/* // Documentation does not mention array, counter to NotBlank constraint
92
         elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
93
            // Blank so maximum 0 item
94
            $doc->setMaxItem(0);
95
        }*/
96 11
        if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) {
97 2
            $doc->setMinItem(
98 2
                $constraint instanceof Assert\GreaterThanOrEqual
99 1
                    ? $constraint->value
100 2
                    : $constraint->value + 1
101
            );
102 9
        } elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) {
103 2
            $doc->setMaxItem(
104 2
                $constraint instanceof Assert\LessThanOrEqual
105 1
                    ? $constraint->value
106 2
                    : $constraint->value - 1
107
            );
108
        }
109 11
    }
110
111
    /**
112
     * @param NumberDoc $doc
113
     * @param Constraint $constraint
114
     */
115 7
    private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void
116
    {
117 7
        if ($constraint instanceof Assert\Range) {
118 3
            if (null !== $constraint->min) {
119 2
                $doc->setMin($constraint->min);
120
            }
121 3
            if (null !== $constraint->max) {
122 3
                $doc->setMax($constraint->max);
123
            }
124 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
125 4
            || $constraint instanceof Assert\LessThan
126
        ) {
127 2
            $doc->setMax($constraint->value);
128 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
129 2
            || $constraint instanceof Assert\GreaterThan
130
        ) {
131 2
            $doc->setMin($constraint->value);
132
        }
133 7
    }
134
}
135