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

EntityDefinitionCreator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 21 4
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
}