Annotations::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Vsmoraes\DynamoMapper;
3
4
use Vsmoraes\DynamoMapper\Exception\AnnotationNotFound;
5
use Vsmoraes\DynamoMapper\Exception\InvalidAttributeType;
6
use ICanBoogie\Inflector;
7
8
class Annotations
9
{
10
    /**
11
     * @var \ReflectionClass
12
     */
13
    private $reflectionClass;
14
15
    public function __construct(\ReflectionClass $reflectionClass)
16
    {
17
        $this->reflectionClass = $reflectionClass;
18
    }
19
20
    /**
21
     * @param string $attribute
22
     * @return string
23
     * @throws AnnotationNotFound
24
     * @throws InvalidAttributeType
25
     */
26
    public function getAttributeType(string $attribute): string
27
    {
28
        $attribute = Inflector::get()->camelize($attribute, Inflector::DOWNCASE_FIRST_LETTER);
29
        $docblock = $this->reflectionClass->getProperty($attribute)
30
            ->getDocComment();
31
32
        $annotations = [];
33
        preg_match_all('/@var\s*([^\s]+)/i', $docblock, $annotations, PREG_SET_ORDER);
34
35
        if (empty($annotations) || ! isset($annotations[0][1])) {
36
            throw new AnnotationNotFound;
37
        }
38
39
        $type = strtolower($annotations[0][1]);
40
41
        return $type;
42
    }
43
}
44