EntityDefinitionCreator::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Agares\MicroORM;
6
7
class EntityDefinitionCreator implements EntityDefinitionCreatorInterface
8
{
9
    /**
10
     * @var FieldNameMapperInterface
11
     */
12
    private $fieldNameMapper;
13
14
    public function __construct(FieldNameMapperInterface $fieldNameMapper)
15
    {
16
        $this->fieldNameMapper = $fieldNameMapper;
17
    }
18
19
    public function create(string $className) : EntityDefinition
20
    {
21
        $entityReflection = new \ReflectionClass($className);
22
        $methods = $entityReflection->getMethods(\ReflectionMethod::IS_PUBLIC);
23
24
        $entityDefinition = new EntityDefinition($className);
25
        foreach ($methods as $method) {
26
            $methodName = $method->getName();
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
27
28
            if (strpos($methodName, 'get') !== 0) {
29
                continue;
30
            }
31
32
            $fieldName = $this->fieldNameMapper->map($methodName);
33
            $fieldType = $method->getReturnType() === NULL ? 'string' : (string) $method->getReturnType();
34
35
            $entityDefinition->addField(new EntityFieldDefinition(lcfirst(substr($methodName, 3)), $fieldName, $fieldType));
36
        }
37
38
        return $entityDefinition;
39
    }
40
}