Completed
Pull Request — release/1.0.0 (#4)
by Yo
01:43
created

DocTypeHelper::isAbstractType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
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\ArrayDoc;
7
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\BooleanDoc;
8
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\FloatDoc;
9
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\IntegerDoc;
10
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc;
11
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ScalarDoc;
12
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc;
13
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
14
15
/**
16
 * Class DocTypeHelper
17
 */
18
class DocTypeHelper
19
{
20
    /** @var ConstraintPayloadDocHelper */
21
    private $constraintPayloadDocHelper;
22
    /** @var TypeGuesser */
23
    private $typeGuesser;
24
25
    /**
26
     * @param ConstraintPayloadDocHelper $constraintPayloadDocHelper
27
     */
28 17
    public function __construct(ConstraintPayloadDocHelper $constraintPayloadDocHelper, TypeGuesser $typeGuesser)
29
    {
30 17
        $this->constraintPayloadDocHelper = $constraintPayloadDocHelper;
31 17
        $this->typeGuesser = $typeGuesser;
32 17
    }
33
34
    /**
35
     * @param Constraint[] $constraintList
36
     *
37
     * @return TypeDoc
38
     */
39 17
    public function guess(array $constraintList) : TypeDoc
40
    {
41 17
        return $this->getDocFromTypeConstraintOrPayloadDocIfExist($constraintList)
42 3
            ?? $this->typeGuesser->guessTypeFromConstraintList($constraintList)
43 17
            ?? new TypeDoc()
44
        ;
45
    }
46
47
    /**
48
     * @param Constraint[] $constraintList
49
     *
50
     * @return TypeDoc|null
51
     */
52 17
    protected function getDocFromTypeConstraintOrPayloadDocIfExist(array $constraintList)
53
    {
54 17
        $doc = null;
55
        // Check if a Type constraint exist or if a constraint have a type documentation
56 17
        foreach ($constraintList as $constraint) {
57 16
            if (null !== ($typeFromPayload = $this->constraintPayloadDocHelper->getTypeIfExist($constraint))) {
58 1
                $doc = $this->normalizeType($typeFromPayload);
59 15
            } elseif ($constraint instanceof Assert\Type) {
60 14
                $doc = $this->normalizeType(strtolower($constraint->type));
61
            }
62
63 16
            if (null !== $doc) {
64 16
                break;
65
            }
66
        }
67
68 17
        return $doc;
69
    }
70
71
    /**
72
     * @param string $type
73
     *
74
     * @return TypeDoc|null
75
     */
76 15
    private function normalizeType(string $type)
77
    {
78 15
        if ('scalar' === $type) {
79 1
            return new ScalarDoc();
80 14
        } elseif ('string' === $type) {
81 1
            return new StringDoc();
82 13
        } elseif ('bool' === $type || 'boolean' === $type) {
83 2
            return new BooleanDoc();
84 11
        } elseif ('int' === $type || 'integer' === $type) {
85 2
            return new IntegerDoc();
86 9
        } elseif (in_array($type, ['float', 'long', 'double', 'real', 'numeric'])) {
87 5
            return new FloatDoc();
88 4
        } elseif ('array' === $type) {
89 2
            return new ArrayDoc();
90 2
        } elseif ('object' === $type) {
91 1
            return new ObjectDoc();
92
        }
93
94 1
        return null;
95
    }
96
}
97