Failed Conditions
Pull Request — master (#17)
by Yo
01:27
created

MinMaxHelper::appendCollectionDoc()   C

Complexity

Conditions 13
Paths 18

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 17
nc 18
nop 2
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 13
rs 6.6166
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 || $constraint instanceof Assert\Count) {
75 6
            if (null !== $constraint->min) {
76 4
                $doc->setMinItem((int) $constraint->min);
77
            }
78 6
            if (null !== $constraint->max) {
79 6
                $doc->setMaxItem((int) $constraint->max);
80
            }
81 5
        } elseif ($constraint instanceof Assert\NotBlank && null === $doc->getMinItem()) {
82
            // Not blank so minimum 1 item
83 1
            $doc->setMinItem(1);
84
        }/* // Documentation does not mention array, counter to NotBlank constraint
85
         elseif ($constraint instanceof Assert\Blank && null === $doc->getMaxItem()) {
86
            // Blank so maximum 0 item
87
            $doc->setMaxItem(0);
88
        }*/
89 11
        if ($constraint instanceof Assert\GreaterThan || $constraint instanceof Assert\GreaterThanOrEqual) {
90 2
            $doc->setMinItem(
91 2
                $constraint instanceof Assert\GreaterThanOrEqual
92 1
                    ? $constraint->value
93 2
                    : $constraint->value + 1
94
            );
95 9
        } elseif ($constraint instanceof Assert\LessThan || $constraint instanceof Assert\LessThanOrEqual) {
96 2
            $doc->setMaxItem(
97 2
                $constraint instanceof Assert\LessThanOrEqual
98 1
                    ? $constraint->value
99 2
                    : $constraint->value - 1
100
            );
101
        }
102 11
    }
103
104
    /**
105
     * @param NumberDoc $doc
106
     * @param Constraint $constraint
107
     */
108 7
    private function appendNumberMinMax(NumberDoc $doc, Constraint $constraint) : void
109
    {
110 7
        if ($constraint instanceof Assert\Range) {
111 3
            if (null !== $constraint->min) {
112 2
                $doc->setMin($constraint->min);
113
            }
114 3
            if (null !== $constraint->max) {
115 3
                $doc->setMax($constraint->max);
116
            }
117 4
        } elseif ($constraint instanceof Assert\LessThanOrEqual
118 4
            || $constraint instanceof Assert\LessThan
119
        ) {
120 2
            $doc->setMax($constraint->value);
121 2
        } elseif ($constraint instanceof Assert\GreaterThanOrEqual
122 2
            || $constraint instanceof Assert\GreaterThan
123
        ) {
124 2
            $doc->setMin($constraint->value);
125
        }
126 7
    }
127
128
    /**
129
     * @param array  $docArray
130
     * @param string $key
131
     * @param mixed  $value
132
     *
133
     * @return array
134
     */
135
    private function appendIfNotNull(array $docArray, string $key, $value) : array
0 ignored issues
show
Unused Code introduced by
The method appendIfNotNull() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
136
    {
137
        if (null !== $value) {
138
            $docArray[$key] = $value;
139
        }
140
141
        return $docArray;
142
    }
143
}
144