Passed
Push — master ( 76b08e...5cd6b2 )
by Ryuichi
58:40 queued 56:20
created

EntityManager::getEntity()   D

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 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 44
c 1
b 0
f 0
nc 86
nop 1
dl 0
loc 59
ccs 35
cts 41
cp 0.8537
crap 22.3809
rs 4.1666

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