Completed
Push — master ( c6b9dc...9b6af1 )
by Paweł
02:25
created

EntityDefinitionCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Agares\MicroORM;
5
6
class EntityDefinitionCreator implements EntityDefinitionCreatorInterface
7
{
8
    /**
9
     * @var FieldNameMapperInterface
10
     */
11
    private $fieldNameMapper;
12
13
    public function __construct(FieldNameMapperInterface $fieldNameMapper)
14
    {
15
        $this->fieldNameMapper = $fieldNameMapper;
16
    }
17
18
    public function create(string $className) : EntityDefinition
19
    {
20
        $entityReflection = new \ReflectionClass($className);
21
        $methods = $entityReflection->getMethods(\ReflectionMethod::IS_PUBLIC);
22
23
        $entityDefinition = new EntityDefinition($className);
24
        foreach ($methods as $method) {
25
            $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...
26
27
            if (strpos($methodName, 'get') !== 0) {
28
                continue;
29
            }
30
31
            $fieldName = $this->fieldNameMapper->map($methodName);
32
            $fieldType = $method->getReturnType() === NULL ? 'string' : (string) $method->getReturnType();
33
34
            $entityDefinition->addField(new EntityFieldDefinition($fieldName, $fieldType));
35
        }
36
37
        return $entityDefinition;
38
    }
39
}