Code Duplication    Length = 29-30 lines in 2 locations

src/Database.php 2 locations

@@ 135-163 (lines=29) @@
132
     * @param string $className Class name if we want to create object
133
     * @return array Collection of arrays or objects
134
     */
135
    public function &fetch($sql, $className = null)
136
    {
137
        // Return value
138
        $result = array();
139
140
        if (isset($this->driver)) {
141
            // Store timestamp
142
            $tsLast = microtime(true);
143
144
            try {
145
                // Perform database query
146
                if (!isset($className)) { // Return array
147
                    $result = $this->driver->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
148
                } else { // Create object of passed class name
149
                    $result = $this->driver->query($sql)->fetchAll(\PDO::FETCH_CLASS, $className, array(&$this));
150
                }
151
            } catch (\PDOException $e) {
152
                echo("\n" . $sql . '-' . $e->getMessage());
153
            }
154
155
            // Store queries count
156
            $this->count++;
157
158
            // Count elapsed time
159
            $this->elapsed += microtime(true) - $tsLast;
160
        }
161
162
        return $result;
163
    }
164
165
    /**
166
     * Special accelerated function to retrieve db record fields instead of objects
@@ 215-244 (lines=30) @@
212
     * @param string $className Class name if we want to create object
213
     * @return array|object Record as array or object
214
     */
215
    public function &fetchOne($sql, $className = null)
216
    {
217
        // Return value, configure to return correct type
218
        $result = isset($className) ? new \stdClass() : array();
219
220
        if (isset($this->driver)) {
221
            // Store timestamp
222
            $tsLast = microtime(true);
223
224
            try {
225
                // Perform database query
226
                if (!isset($className)) { // Return array
227
                    $result = $this->driver->query($sql)->fetch(\PDO::FETCH_ASSOC);
228
                } else { // Create object of passed class name
229
                    $result = $this->driver->query($sql)->fetchObject($className, array(&$this));
230
                }
231
232
            } catch (\PDOException $e) {
233
                echo("\n" . $sql . '-' . $e->getMessage());
234
            }
235
236
            // Store queries count
237
            $this->count++;
238
239
            // Count elapsed time
240
            $this->elapsed += microtime(true) - $tsLast;
241
        }
242
243
        return $result;
244
    }
245
246
    /**
247
     * Get one record from database by its field value