Failed Conditions
Pull Request — master (#3260)
by Michael
61:30
created

DB2Statement   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 389
Duplicated Lines 0 %

Test Coverage

Coverage 53.52%

Importance

Changes 0
Metric Value
wmc 57
eloc 141
dl 0
loc 389
ccs 114
cts 213
cp 0.5352
rs 5.04
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A bindValue() 0 3 1
A bindParam() 0 24 4
B castObject() 0 52 6
A rowCount() 0 3 2
B execute() 0 35 7
A columnCount() 0 3 2
A bind() 0 6 2
A setFetchMode() 0 13 3
A fetchColumn() 0 13 3
A closeCursor() 0 11 2
A writeStringToStream() 0 4 2
A createTemporaryFile() 0 9 2
A getIterator() 0 3 1
B fetch() 0 44 11
A fetchAll() 0 22 6
A copyStreamToStream() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like DB2Statement 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.

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 DB2Statement, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\IBMDB2;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver\Statement;
9
use Doctrine\DBAL\Driver\StatementIterator;
10
use Doctrine\DBAL\FetchMode;
11
use Doctrine\DBAL\ParameterType;
12
use IteratorAggregate;
13
use ReflectionClass;
14
use ReflectionObject;
15
use ReflectionProperty;
16
use stdClass;
17
use const CASE_LOWER;
18
use const DB2_BINARY;
19
use const DB2_CHAR;
20
use const DB2_LONG;
21
use const DB2_PARAM_FILE;
22
use const DB2_PARAM_IN;
23
use function array_change_key_case;
24
use function array_key_exists;
25
use function count;
26
use function db2_bind_param;
27
use function db2_execute;
28
use function db2_fetch_array;
29
use function db2_fetch_assoc;
30
use function db2_fetch_both;
31
use function db2_fetch_object;
32
use function db2_free_result;
33
use function db2_num_fields;
34
use function db2_num_rows;
35
use function error_get_last;
36
use function fclose;
37
use function fwrite;
38
use function gettype;
39
use function is_object;
40
use function is_resource;
41
use function is_string;
42
use function ksort;
43
use function sprintf;
44
use function stream_copy_to_stream;
45
use function stream_get_meta_data;
46
use function strtolower;
47
use function tmpfile;
48
49
class DB2Statement implements IteratorAggregate, Statement
50
{
51
    /** @var resource */
52
    private $stmt;
53
54
    /** @var mixed[] */
55
    private $bindParam = [];
56
57
    /**
58
     * Map of LOB parameter positions to the tuples containing reference to the variable bound to the driver statement
59
     * and the temporary file handle bound to the underlying statement
60
     *
61
     * @var mixed[][]
62
     */
63
    private $lobs = [];
64
65
    /** @var string Name of the default class to instantiate when fetching class instances. */
66
    private $defaultFetchClass = '\stdClass';
67
68
    /** @var mixed[] Constructor arguments for the default class to instantiate when fetching class instances. */
69
    private $defaultFetchClassCtorArgs = [];
70
71
    /** @var int */
72
    private $defaultFetchMode = FetchMode::MIXED;
73
74
    /**
75
     * Indicates whether the statement is in the state when fetching results is possible
76
     *
77
     * @var bool
78
     */
79
    private $result = false;
80
81
    /**
82
     * @param resource $stmt
83
     */
84 215
    public function __construct($stmt)
85
    {
86 215
        $this->stmt = $stmt;
87 215
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 213
    public function bindValue($param, $value, $type = ParameterType::STRING) : void
93
    {
94 213
        $this->bindParam($param, $value, $type);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 213
    public function bindParam($column, &$variable, $type = ParameterType::STRING, $length = null) : void
101
    {
102 213
        switch ($type) {
103
            case ParameterType::INTEGER:
104 213
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_LONG);
105 213
                break;
106
107
            case ParameterType::LARGE_OBJECT:
108 213
                if (isset($this->lobs[$column])) {
109
                    [, $handle] = $this->lobs[$column];
110
                    fclose($handle);
111
                }
112
113 213
                $handle = $this->createTemporaryFile();
114 213
                $path   = stream_get_meta_data($handle)['uri'];
115
116 213
                $this->bind($column, $path, DB2_PARAM_FILE, DB2_BINARY);
117
118 213
                $this->lobs[$column] = [&$variable, $handle];
119 213
                break;
120
121
            default:
122 213
                $this->bind($column, $variable, DB2_PARAM_IN, DB2_CHAR);
123 213
                break;
124
        }
125
    }
126 213
127
    /**
128
     * @param int   $position Parameter position
129
     * @param mixed $variable
130
     *
131
     * @throws DB2Exception
132
     */
133
    private function bind($position, &$variable, int $parameterType, int $dataType) : void
134
    {
135 213
        $this->bindParam[$position] =& $variable;
136
137 213
        if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) {
138
            throw DB2Exception::fromStatementError($this->stmt);
139 213
        }
140
    }
141
142 213
    /**
143
     * {@inheritdoc}
144
     */
145
    public function closeCursor() : void
146
    {
147 109
        $this->bindParam = [];
148
149 109
        if (! $this->result) {
150
            return;
151 109
        }
152
153
        db2_free_result($this->stmt);
154
155 109
        $this->result = false;
156
    }
157 109
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function columnCount()
162
    {
163 109
        return db2_num_fields($this->stmt) ?: 0;
164
    }
165 109
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function execute($params = null) : void
170
    {
171
        if ($params === null) {
172
            ksort($this->bindParam);
173
174
            $params = [];
175
176
            foreach ($this->bindParam as $column => $value) {
177
                $params[] = $value;
178
            }
179
        }
180
181
        foreach ($this->lobs as [$source, $target]) {
182
            if (is_resource($source)) {
183
                $this->copyStreamToStream($source, $target);
184
185
                continue;
186
            }
187
188
            $this->writeStringToStream($source, $target);
189
        }
190 215
191
        $retval = db2_execute($this->stmt, $params);
192 215
193 215
        foreach ($this->lobs as [, $handle]) {
194
            fclose($handle);
195 215
        }
196
197 215
        $this->lobs = [];
198 213
199
        if ($retval === false) {
200
            throw DB2Exception::fromStatementError($this->stmt);
201
        }
202 215
203 213
        $this->result = true;
204 212
    }
205
206 212
    /**
207
     * {@inheritdoc}
208
     */
209 213
    public function setFetchMode($fetchMode, ...$args) : void
210
    {
211
        $this->defaultFetchMode = $fetchMode;
212 215
213
        if (isset($args[0])) {
214 215
            $this->defaultFetchClass = $args[0];
215 213
        }
216
217
        if (! isset($args[1])) {
218 215
            return;
219
        }
220 215
221
        $this->defaultFetchClassCtorArgs = (array) $args[2];
222
    }
223
224 215
    /**
225
     * {@inheritdoc}
226 215
     */
227
    public function getIterator()
228
    {
229
        return new StatementIterator($this);
230
    }
231
232 215
    /**
233
     * {@inheritdoc}
234 215
     */
235 215
    public function fetch($fetchMode = null, ...$args)
236 215
    {
237
        // do not try fetching from the statement if it's not expected to contain result
238 215
        // in order to prevent exceptional situation
239
        if (! $this->result) {
240
            return false;
241
        }
242
243
        $fetchMode = $fetchMode ?: $this->defaultFetchMode;
244 215
        switch ($fetchMode) {
245
            case FetchMode::COLUMN:
246 215
                return $this->fetchColumn();
247
248
            case FetchMode::MIXED:
249
                return db2_fetch_both($this->stmt);
250
251
            case FetchMode::ASSOCIATIVE:
252 215
                return db2_fetch_assoc($this->stmt);
253
254
            case FetchMode::CUSTOM_OBJECT:
255
                $className = $this->defaultFetchClass;
256 215
                $ctorArgs  = $this->defaultFetchClassCtorArgs;
257 47
258
                if (count($args) > 0) {
259
                    $className = $args[0];
260 215
                    $ctorArgs  = $args[1] ?? [];
261 215
                }
262
263 36
                $result = db2_fetch_object($this->stmt);
264
265
                if ($result instanceof stdClass) {
266 200
                    $result = $this->castObject($result, $className, $ctorArgs);
267
                }
268
269 215
                return $result;
270
271
            case FetchMode::NUMERIC:
272 144
                return db2_fetch_array($this->stmt);
273 144
274
            case FetchMode::STANDARD_OBJECT:
275 144
                return db2_fetch_object($this->stmt);
276 144
277 144
            default:
278 144
                throw new DB2Exception('Given Fetch-Style ' . $fetchMode . ' is not supported.');
279
        }
280
    }
281 144
282
    /**
283 144
     * {@inheritdoc}
284 144
     */
285
    public function fetchAll($fetchMode = null, ...$args)
286
    {
287 144
        $rows = [];
288
289
        switch ($fetchMode) {
290 212
            case FetchMode::CUSTOM_OBJECT:
291
                while (($row = $this->fetch($fetchMode, ...$args)) !== false) {
292
                    $rows[] = $row;
293 145
                }
294
                break;
295
            case FetchMode::COLUMN:
296
                while (($row = $this->fetchColumn()) !== false) {
297
                    $rows[] = $row;
298
                }
299
                break;
300
            default:
301
                while (($row = $this->fetch($fetchMode)) !== false) {
302
                    $rows[] = $row;
303 215
                }
304
        }
305 215
306
        return $rows;
307 215
    }
308
309 144
    /**
310 144
     * {@inheritdoc}
311
     */
312 144
    public function fetchColumn($columnIndex = 0)
313
    {
314 212
        $row = $this->fetch(FetchMode::NUMERIC);
315 212
316
        if ($row === false) {
317 212
            return false;
318
        }
319 215
320 201
        if (! array_key_exists($columnIndex, $row)) {
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type object; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

320
        if (! array_key_exists($columnIndex, /** @scrutinizer ignore-type */ $row)) {
Loading history...
321
            throw DBALException::invalidColumnIndex($columnIndex, count($row));
322
        }
323
324 215
        return $row[$columnIndex];
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330 212
    public function rowCount() : int
331
    {
332 212
        return @db2_num_rows($this->stmt) ? : 0;
333
    }
334 212
335 212
    /**
336
     * Casts a stdClass object to the given class name mapping its' properties.
337
     *
338 212
     * @param stdClass      $sourceObject     Object to cast from.
339
     * @param string|object $destinationClass Name of the class or class instance to cast to.
340
     * @param mixed[]       $ctorArgs         Arguments to use for constructing the destination class instance.
341
     *
342
     * @return object
343
     *
344 213
     * @throws DB2Exception
345
     */
346 213
    private function castObject(stdClass $sourceObject, $destinationClass, array $ctorArgs = [])
347
    {
348
        if (! is_string($destinationClass)) {
349
            if (! is_object($destinationClass)) {
350
                throw new DB2Exception(sprintf(
351
                    'Destination class has to be of type string or object, %s given.',
352
                    gettype($destinationClass)
353
                ));
354
            }
355
        } else {
356
            $destinationClass = new ReflectionClass($destinationClass);
357
            $destinationClass = $destinationClass->newInstanceArgs($ctorArgs);
358
        }
359
360 144
        $sourceReflection           = new ReflectionObject($sourceObject);
361
        $destinationClassReflection = new ReflectionObject($destinationClass);
362 144
        /** @var ReflectionProperty[] $destinationProperties */
363
        $destinationProperties = array_change_key_case($destinationClassReflection->getProperties(), CASE_LOWER);
364
365
        foreach ($sourceReflection->getProperties() as $sourceProperty) {
366
            $sourceProperty->setAccessible(true);
367
368
            $name  = $sourceProperty->getName();
369
            $value = $sourceProperty->getValue($sourceObject);
370 144
371 144
            // Try to find a case-matching property.
372
            if ($destinationClassReflection->hasProperty($name)) {
373
                $destinationProperty = $destinationClassReflection->getProperty($name);
374 144
375 144
                $destinationProperty->setAccessible(true);
376
                $destinationProperty->setValue($destinationClass, $value);
377 144
378
                continue;
379 144
            }
380 144
381
            $name = strtolower($name);
382 144
383 144
            // Try to find a property without matching case.
384
            // Fallback for the driver returning either all uppercase or all lowercase column names.
385
            if (isset($destinationProperties[$name])) {
386 144
                $destinationProperty = $destinationProperties[$name];
387
388
                $destinationProperty->setAccessible(true);
389
                $destinationProperty->setValue($destinationClass, $value);
390
391
                continue;
392
            }
393
394
            $destinationClass->$name = $value;
395 144
        }
396
397
        return $destinationClass;
398
    }
399 144
400
    /**
401
     * @return resource
402
     *
403
     * @throws DB2Exception
404
     */
405
    private function createTemporaryFile()
406
    {
407
        $handle = @tmpfile();
408 144
409
        if ($handle === false) {
410
            throw new DB2Exception('Could not create temporary file: ' . error_get_last()['message']);
411 144
        }
412
413
        return $handle;
414
    }
415
416
    /**
417
     * @param resource $source
418
     * @param resource $target
419 213
     *
420
     * @throws DB2Exception
421 213
     */
422
    private function copyStreamToStream($source, $target) : void
423 213
    {
424
        if (@stream_copy_to_stream($source, $target) === false) {
425
            throw new DB2Exception('Could not copy source stream to temporary file: ' . error_get_last()['message']);
426
        }
427 213
    }
428
429
    /**
430
     * @param resource $target
431
     *
432
     * @throws DB2Exception
433
     */
434
    private function writeStringToStream(string $string, $target) : void
435
    {
436 212
        if (@fwrite($target, $string) === false) {
437
            throw new DB2Exception('Could not write string to temporary file: ' . error_get_last()['message']);
438 212
        }
439
    }
440
}
441