Entity   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 0
cts 23
cp 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntityIdType() 0 20 4
A getEntityIdName() 0 15 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Util;
5
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\ORM\Mapping\Column;
8
use Doctrine\ORM\Mapping\Id;
9
10
final class Entity
11
{
12
    /**
13
     * @param string $entity
14
     * @return null|string
15
     * @throws \Doctrine\Common\Annotations\AnnotationException
16
     * @throws \ReflectionException
17
     */
18
    public static function getEntityIdName(string $entity): ?string
19
    {
20
        $name = '';
21
        $reflectionClassName = new \ReflectionClass($entity);
22
        foreach ($reflectionClassName->getProperties() as $property) {
23
            /** @var Id $id */
24
            $id = (new AnnotationReader())
25
                ->getPropertyAnnotation($property, Id::class);
26
            if (!empty($id)) {
27
                $name = $property->getName();
28
                break;
29
            }
30
        }
31
32
        return $name;
33
    }
34
35
    /**
36
     * @param string $entity
37
     * @return string
38
     * @throws \Doctrine\Common\Annotations\AnnotationException
39
     * @throws \ReflectionException
40
     */
41
    public static function getEntityIdType(string $entity): string
42
    {
43
        $type = '';
44
        $reflectionClassName = new \ReflectionClass($entity);
45
        foreach ($reflectionClassName->getProperties() as $property) {
46
            /** @var Id $id */
47
            $id = (new AnnotationReader())
48
                ->getPropertyAnnotation($property, Id::class);
49
            if (!empty($id)) {
50
                /** @var Column $column */
51
                $column = (new AnnotationReader())
52
                    ->getPropertyAnnotation($property, Column::class);
53
                if ($column) {
54
                    $type = $column->type;
55
                }
56
                break;
57
            }
58
        }
59
60
        return $type;
61
    }
62
}
63