EntityDefinitionCreator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

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