Passed
Push — master ( b6c2a5...d6fa37 )
by
unknown
13:29
created

PDOStatement::fetchOne()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Core\Database\Driver;
19
20
use Doctrine\DBAL\Driver\PDO\Exception as PDOException;
21
use Doctrine\DBAL\Driver\PDO\Statement as DoctrineDbalPDOStatement;
22
use PDO;
23
24
class PDOStatement extends DoctrineDbalPDOStatement
25
{
26
    /**
27
     * The method fetchAll() is moved into a separate trait to switch method signatures
28
     * depending on the PHP major version in use to support PHP8
29
     */
30
    use PDOStatementImplementation;
31
32
    /**
33
     * Map resources to string like is done for e.g. in mysqli driver
34
     *
35
     * @param mixed $record
36
     * @return mixed
37
     */
38
    protected function mapResourceToString($record)
39
    {
40
        if (is_array($record)) {
41
            return array_map(
42
                static function ($value) {
43
                    if (is_resource($value)) {
44
                        $value = stream_get_contents($value);
45
                    }
46
47
                    return $value;
48
                },
49
                $record
50
            );
51
        }
52
53
        return $record;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
60
    {
61
        try {
62
            $record = parent::fetch($fetchMode, $cursorOrientation, $cursorOffset);
63
            $record = $this->mapResourceToString($record);
64
            return $record;
65
        } catch (\PDOException $exception) {
66
            throw new PDOException($exception);
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function fetchOne($columnIndex = 0)
74
    {
75
        try {
76
            $record = parent::fetchColumn($columnIndex);
77
            $record = $this->mapResourceToString($record);
78
            return $record;
79
        } catch (\PDOException $exception) {
80
            throw new PDOException($exception);
81
        }
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function fetchColumn($columnIndex = 0)
88
    {
89
        return $this->fetchOne($columnIndex);
90
    }
91
}
92