Completed
Push — master ( 38f40f...304183 )
by Ryuichi
05:24
created

EntityManager::getEntity()   C

Complexity

Conditions 21
Paths 86

Size

Total Lines 59
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 22.3809

Importance

Changes 0
Metric Value
cc 21
eloc 44
nc 86
nop 1
dl 0
loc 59
ccs 35
cts 41
cp 0.8537
crap 22.3809
rs 6.2662
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace WebStream\Database;
3
4
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
6
/**
7
 * EntityManager
8
 * @author Ryuichi TANAKA.
9
 * @since 2015/01/11
10
 * @version 0.7
11
 */
12
class EntityManager
13
{
14
    use Injector;
15
16
    /**
17
     * @var string エンティティクラスパス
18
     */
19
    private $classpath;
20
21
    /**
22
     * @var array<string> カラムメタ情報
23
     */
24
    private $columnMeta;
25
26
    /**
27
     * コンストラクタ
28
     * @param string エンティティクラスパス
29
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment エンティティクラスパス at position 0 could not be parsed: Unknown type name 'エンティティクラスパス' at position 0 in エンティティクラスパス.
Loading history...
30 12
    public function __construct($classpath)
31
    {
32 12
        $this->classpath = $classpath;
33
    }
34
35
    /**
36
     * カラムメタ情報を設定する
37
     * @param string カラムメタ情報
38
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment カラムメタ情報 at position 0 could not be parsed: Unknown type name 'カラムメタ情報' at position 0 in カラムメタ情報.
Loading history...
39 12
    public function setColumnMeta(array $columnMeta)
40
    {
41 12
        $this->columnMeta = $columnMeta;
42
    }
43
44
    /**
45
     * 列データをエンティティに変換して返却する
46
     * @param array<string> 列データ
47
     * @return object 列データエンティティ
48
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment 列データ at position 0 could not be parsed: Unknown type name '列データ' at position 0 in 列データ.
Loading history...
49 12
    public function getEntity($row)
50
    {
51 12
        $instance = new $this->classpath();
52 12
        $propertyMap = null;
53
54
        // EntityPropertyを使っている場合はリフレクションを使用しない
55 12
        if (!array_key_exists("WebStream\Database\EntityProperty", class_uses($instance))) {
56 6
            $propertyMap = [];
57 6
            $refClass = new \ReflectionClass($instance);
58 6
            $properties = $refClass->getProperties();
59 6
            foreach ($properties as $property) {
60 6
                if ($property->isPrivate() || $property->isProtected()) {
61 6
                    $property->setAccessible(true);
62
                }
63 6
                $propertyMap[strtolower($property->getName())] = $property;
64
            }
65
        }
66
67 12
        foreach ($row as $col => $value) {
68 12
            switch ($this->columnMeta[$col]) {
69 12
                case 'LONG':      // mysql:int
70 12
                case 'SHORT':     // mysql:smallint
71 12
                case 'int4':      // postgres:int
72 12
                case 'int2':      // postgres:smallint
73 12
                case 'integer':   // sqlite:int
74 12
                case 'smallint':  // sqlite:smallint
75 12
                    $value = intval($value);
76 12
                    break;
77 12
                case 'LONGLONG':  // mysql:bigint
78 12
                case 'int8':      // postgres:bigint
79 12
                case 'bigint':    // sqlite:bigint
80
                    $value = doubleval($value);
81
                    break;
82 12
                case 'DATETIME':  // mysql:datetime
83 12
                case 'DATE':      // mysql:date
84 12
                case 'timestamp': // postgres:timestamp, sqlite:timestamp
85 12
                case 'date':      // postgres:date, sqlite:date
86
                    $value = new \DateTime($value);
87
                    break;
88
                default: // string
89
                    break;
90
            }
91
92 12
            $col = strtolower(preg_replace_callback('/_([a-zA-Z])/', function ($matches) {
93
                return ucfirst($matches[1]);
94 12
            }, $col));
95
96 12
            if ($propertyMap === null) {
97 6
                $instance->{$col} = $value;
98
            } else {
99 6
                if (array_key_exists($col, $propertyMap)) {
100 6
                    $propertyMap[$col]->setValue($instance, $value);
101
                } else {
102
                    $this->logger->error("Column '$col' is failed mapping in " . $this->classpath);
103
                }
104
            }
105
        }
106
107 12
        return $instance;
108
    }
109
}
110