Complex classes like Component often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Component, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Component extends Driver |
||
6 | { |
||
7 | protected $prepared = array(); |
||
8 | |||
9 | 24 | public function exec($query, $values = array()) |
|
18 | |||
19 | 72 | public function query($select, $values = array(), $fetch = 'row') |
|
30 | |||
31 | 32 | public function all($select, $values = array(), $fetch = 'row') |
|
32 | { |
||
33 | 32 | $rows = array(); |
|
34 | 32 | if ($stmt = $this->query($select, $values, $fetch)) { |
|
35 | 32 | while ($row = $this->fetch($stmt)) { |
|
36 | 31 | $rows[] = $row; |
|
37 | 31 | } |
|
38 | 32 | $this->close($stmt); |
|
39 | 32 | } |
|
40 | |||
41 | 32 | return $rows; |
|
42 | } |
||
43 | |||
44 | 14 | public function ids($select, $values = array()) |
|
45 | { |
||
46 | 14 | $ids = array(); |
|
47 | 14 | if ($stmt = $this->query($select, $values, 'row')) { |
|
48 | 14 | while ($row = $this->fetch($stmt)) { |
|
49 | 12 | $ids[] = (int) array_shift($row); |
|
50 | 12 | } |
|
51 | 14 | $this->close($stmt); |
|
52 | 14 | } |
|
53 | |||
54 | 14 | return (!empty($ids)) ? $ids : false; |
|
55 | } |
||
56 | |||
57 | 48 | public function row($select, $values = array(), $fetch = 'row') |
|
58 | { |
||
59 | 48 | if ($stmt = $this->query($select, $values, $fetch)) { |
|
60 | 48 | $row = $this->fetch($stmt); |
|
61 | 48 | $this->close($stmt); |
|
62 | 48 | } |
|
63 | |||
64 | 48 | return (isset($row) && !empty($row)) ? $row : false; |
|
65 | } |
||
66 | |||
67 | 30 | public function value($select, $values = array()) |
|
71 | |||
72 | 19 | public function insert($table, array $data, $and = '') |
|
73 | { |
||
74 | 19 | if (isset($this->prepared[$table])) { |
|
75 | 13 | return $this->execute($table, $data); |
|
76 | } |
||
77 | 19 | $single = (count(array_filter(array_keys($data), 'is_string')) > 0) ? $data : false; |
|
78 | 19 | if ($single) { |
|
79 | 8 | $data = array_keys($data); |
|
80 | 8 | } |
|
81 | 19 | if (stripos($table, ' INTO ') !== false) { // eg. 'OR IGNORE INTO table' |
|
82 | 2 | $query = "INSERT {$table} "; |
|
83 | 2 | } else { |
|
84 | 19 | $query = "INSERT INTO {$table} "; |
|
85 | } |
||
86 | 19 | $query .= '('.implode(', ', $data).') VALUES ('.implode(', ', array_fill(0, count($data), '?')).') '.$and; |
|
87 | 19 | $stmt = $this->prepare($query); |
|
88 | 19 | if ($single && $stmt) { |
|
|
|||
89 | 8 | $id = $this->insert($stmt, array_values($single)); |
|
90 | 8 | $this->close($stmt); |
|
91 | |||
92 | 8 | return $id; |
|
93 | } |
||
94 | |||
95 | 15 | return $stmt; |
|
96 | } |
||
97 | |||
98 | 18 | public function update($table, $id, array $data, $and = '') |
|
99 | { |
||
100 | 18 | if (isset($this->prepared[$table])) { |
|
101 | 16 | $data[] = $id; |
|
102 | |||
103 | 16 | return $this->execute($table, $data); |
|
104 | } |
||
105 | 18 | $first = each($data); |
|
106 | 18 | $single = (is_array($first['value'])) ? $first['value'] : false; |
|
107 | 18 | if ($single) { |
|
108 | 8 | $data = array_keys($single); |
|
109 | 8 | } |
|
110 | 18 | if (stripos($table, ' SET ') !== false) { // eg. 'table SET date = NOW(),' |
|
111 | 1 | $query = "UPDATE {$table} "; |
|
112 | 1 | } else { |
|
113 | 18 | $query = "UPDATE {$table} SET "; |
|
114 | } |
||
115 | 18 | $query .= implode(' = ?, ', $data).' = ? WHERE '.$id.' = ? '.$and; |
|
116 | 18 | $stmt = $this->prepare($query); |
|
117 | 18 | if ($single && $stmt) { |
|
118 | 8 | $affected = $this->update($stmt, $first['key'], array_values($single)); |
|
119 | 8 | $this->close($stmt); |
|
120 | |||
121 | 8 | return $affected; |
|
122 | } |
||
123 | |||
124 | 12 | return $stmt; |
|
125 | } |
||
126 | |||
127 | 2 | public function upsert($table, $id, array $data) |
|
128 | { |
||
129 | 2 | if (isset($this->prepared[$table]['ref']) && $this->execute($table, $id)) { |
|
130 | 2 | $data[] = $id; |
|
131 | 2 | if ($row = $this->fetch($table)) { |
|
132 | 1 | return ($this->execute($this->prepared[$table]['ref']['update'], $data)) ? array_shift($row) : false; |
|
133 | } else { |
||
134 | 2 | return $this->execute($this->prepared[$table]['ref']['insert'], $data); |
|
135 | } |
||
136 | } |
||
137 | 2 | $first = each($data); |
|
138 | 2 | $single = (is_array($first['value'])) ? $first['value'] : false; |
|
139 | 2 | if ($single) { |
|
140 | 1 | $data = array_keys($single); |
|
141 | 1 | } |
|
142 | 2 | if ($stmt = $this->prepare("SELECT {$id} FROM {$table} WHERE {$id} = ?", 'row')) { |
|
143 | 2 | $this->prepared[$stmt]['ref']['update'] = $this->update($table, $id, $data); |
|
144 | 2 | $this->prepared[$stmt]['ref']['insert'] = $this->insert($table, array_merge($data, array($id))); |
|
145 | 2 | } |
|
146 | 2 | if ($single && $stmt) { |
|
147 | 1 | $id = $this->upsert($stmt, $first['key'], array_values($single)); |
|
148 | 1 | $this->close($stmt); |
|
149 | |||
150 | 1 | return $id; |
|
151 | } |
||
152 | |||
153 | 2 | return $stmt; |
|
154 | } |
||
155 | |||
156 | 76 | public function prepare($query, $fetch = null) |
|
185 | |||
186 | 75 | public function execute($stmt, $values = null) |
|
212 | |||
213 | 72 | public function fetch($stmt) |
|
221 | |||
222 | 75 | public function close($stmt) |
|
234 | |||
235 | 2 | public function debug($query, $values = array()) |
|
249 | |||
250 | 10 | public function log($value = null) // 'sql', 'count', 'prepared', 'executed', 'errors'?, 'average', 'total', 'time' |
|
268 | } |
||
269 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: