Conditions | 21 |
Paths | 86 |
Total Lines | 59 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function getEntity($row) |
||
50 | { |
||
51 | $instance = new $this->classpath(); |
||
52 | $propertyMap = null; |
||
53 | |||
54 | // EntityPropertyを使っている場合はリフレクションを使用しない |
||
55 | if (!array_key_exists("WebStream\Database\EntityProperty", class_uses($instance))) { |
||
56 | $propertyMap = []; |
||
57 | $refClass = new \ReflectionClass($instance); |
||
58 | $properties = $refClass->getProperties(); |
||
59 | foreach ($properties as $property) { |
||
60 | if ($property->isPrivate() || $property->isProtected()) { |
||
61 | $property->setAccessible(true); |
||
62 | } |
||
63 | $propertyMap[strtolower($property->getName())] = $property; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | foreach ($row as $col => $value) { |
||
68 | switch ($this->columnMeta[$col]) { |
||
69 | case 'LONG': // mysql:int |
||
70 | case 'SHORT': // mysql:smallint |
||
71 | case 'int4': // postgres:int |
||
72 | case 'int2': // postgres:smallint |
||
73 | case 'integer': // sqlite:int |
||
74 | case 'smallint': // sqlite:smallint |
||
75 | $value = intval($value); |
||
76 | break; |
||
77 | case 'LONGLONG': // mysql:bigint |
||
78 | case 'int8': // postgres:bigint |
||
79 | case 'bigint': // sqlite:bigint |
||
80 | $value = doubleval($value); |
||
81 | break; |
||
82 | case 'DATETIME': // mysql:datetime |
||
83 | case 'DATE': // mysql:date |
||
84 | case 'timestamp': // postgres:timestamp, sqlite:timestamp |
||
85 | case 'date': // postgres:date, sqlite:date |
||
86 | $value = new \DateTime($value); |
||
87 | break; |
||
88 | default: // string |
||
89 | break; |
||
90 | } |
||
91 | |||
92 | $col = strtolower(preg_replace_callback('/_([a-zA-Z])/', function ($matches) { |
||
93 | return ucfirst($matches[1]); |
||
94 | }, $col)); |
||
95 | |||
96 | if ($propertyMap === null) { |
||
97 | $instance->{$col} = $value; |
||
98 | } else { |
||
99 | if (array_key_exists($col, $propertyMap)) { |
||
100 | $propertyMap[$col]->setValue($instance, $value); |
||
101 | } else { |
||
102 | $this->logger->error("Column '$col' is failed mapping in " . $this->classpath); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return $instance; |
||
108 | } |
||
110 |