Passed
Push — master ( b2b78e...4de28a )
by Sébastien
07:25
created

PdoResultSet::asObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Bdf\Prime\Connection\Result;
4
5
use Bdf\Prime\Exception\DBALException;
6
use Doctrine\DBAL\Driver\Statement;
7
8
/**
9
 * Adapt PDO statement to result set
10
 *
11
 * @deprecated Use DoctrineResultSet
0 ignored issues
show
introduced by
The text '@deprecated Use DoctrineResultSet' does not match the standard format: @deprecated in deprecation-version and is removed from removal-version. extra-info.
Loading history...
introduced by
Each @deprecated tag must have a @see tag immediately following it.
Loading history...
12
 */
13
/*final*/ class PdoResultSet implements ResultSetInterface
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a class comment
Loading history...
Coding Style introduced by
Expected 0 spaces before class keyword; 1 found
Loading history...
Coding Style introduced by
Single line block comment not allowed; use inline ("// text") comment instead
Loading history...
14
{
15
    /**
16
     * @var Statement|\PDOStatement
17
     */
18
    private $statement;
19
20
    /**
21
     * @var int
0 ignored issues
show
Bug introduced by
Expected "integer" but found "int" for @var tag in member variable comment
Loading history...
22
     */
23
    private $key = 0;
24
25
    /**
26
     * @var mixed
27
     */
28
    private $current;
29
30
31
    /**
32
     * PdoResultSet constructor.
33
     *
34
     * @param Statement|\PDOStatement $statement
35
     */
36 607
    public function __construct($statement)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
37
    {
38 607
        $this->statement = $statement;
39 607
        $this->statement->setFetchMode(\PDO::FETCH_ASSOC);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

39
        /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_ASSOC);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
40 607
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 4
    public function current()
46
    {
47 4
        if ($this->current === null) {
48 2
            $this->rewind();
49
        }
50
51 4
        return $this->current;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function next()
58
    {
59 2
        $this->current = $this->statement->fetch();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

59
        $this->current = /** @scrutinizer ignore-deprecated */ $this->statement->fetch();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60 2
        ++$this->key;
61 2
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @psalm-suppress InvalidReturnType
67
     * @psalm-suppress InvalidReturnStatement
68
     */
69 2
    public function key()
70
    {
71 2
        return $this->key;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function valid()
78
    {
79 2
        return $this->current !== false;
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $mode should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
83
     * {@inheritdoc}
84
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
85 6
    public function fetchMode($mode, $options = null)
86
    {
87
        switch ($mode) {
88 6
            case self::FETCH_ASSOC:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_ASSOC has been deprecated: Use asAssociative() instead ( Ignorable by Annotation )

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

88
            case /** @scrutinizer ignore-deprecated */ self::FETCH_ASSOC:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
89
                $this->statement->setFetchMode(\PDO::FETCH_ASSOC);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

89
                /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_ASSOC);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
91
92 6
            case self::FETCH_NUM:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...SetInterface::FETCH_NUM has been deprecated: Use asList() instead ( Ignorable by Annotation )

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

92
            case /** @scrutinizer ignore-deprecated */ self::FETCH_NUM:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
93 1
                $this->statement->setFetchMode(\PDO::FETCH_NUM);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

93
                /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_NUM);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
94 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
95
96 5
            case self::FETCH_OBJECT:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_OBJECT has been deprecated: Use asObject() instead ( Ignorable by Annotation )

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

96
            case /** @scrutinizer ignore-deprecated */ self::FETCH_OBJECT:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
97 2
                $this->statement->setFetchMode(\PDO::FETCH_OBJ);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

97
                /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_OBJ);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98 2
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
99
100 3
            case self::FETCH_COLUMN:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_COLUMN has been deprecated: Use asColumn() instead ( Ignorable by Annotation )

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

100
            case /** @scrutinizer ignore-deprecated */ self::FETCH_COLUMN:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
101 2
                $this->statement->setFetchMode(\PDO::FETCH_COLUMN, $options ?: 0);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

101
                /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_COLUMN, $options ?: 0);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
102 2
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
103
104 1
            case self::FETCH_CLASS:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_CLASS has been deprecated: Use asClass() instead ( Ignorable by Annotation )

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

104
            case /** @scrutinizer ignore-deprecated */ self::FETCH_CLASS:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
105 1
                $this->statement->setFetchMode(\PDO::FETCH_CLASS, (string) $options);
0 ignored issues
show
Bug introduced by
(string)$options of type string is incompatible with the type integer expected by parameter $columnNumber of PDOStatement::setFetchMode(). ( Ignorable by Annotation )

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

105
                $this->statement->setFetchMode(\PDO::FETCH_CLASS, /** @scrutinizer ignore-type */ (string) $options);
Loading history...
Coding Style introduced by
A cast statement should not be followed as per the coding-style.
Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...atement::setFetchMode() has been deprecated: Use one of the fetch- or iterate-related methods. ( Ignorable by Annotation )

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

105
                /** @scrutinizer ignore-deprecated */ $this->statement->setFetchMode(\PDO::FETCH_CLASS, (string) $options);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106 1
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
107
108
            default:
109
                throw new DBALException('Unsupported fetch mode '.$mode);
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
110
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end switch"
Loading history...
111
112 6
        return $this;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function asAssociative(): ResultSetInterface
119
    {
120
        return $this->fetchMode(self::FETCH_ASSOC);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_ASSOC has been deprecated: Use asAssociative() instead ( Ignorable by Annotation )

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

120
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_ASSOC);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function asList(): ResultSetInterface
127
    {
128
        return $this->fetchMode(self::FETCH_NUM);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...SetInterface::FETCH_NUM has been deprecated: Use asList() instead ( Ignorable by Annotation )

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

128
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_NUM);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function asObject(): ResultSetInterface
135
    {
136
        return $this->fetchMode(self::FETCH_OBJECT);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_OBJECT has been deprecated: Use asObject() instead ( Ignorable by Annotation )

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

136
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_OBJECT);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
137
    }
138
139
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $className should have a doc-comment as per coding-style.
Loading history...
140
     * {@inheritdoc}
141
     */
142
    public function asClass(string $className): ResultSetInterface
143
    {
144
        return $this->fetchMode(self::FETCH_CLASS, $className);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_CLASS has been deprecated: Use asClass() instead ( Ignorable by Annotation )

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

144
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_CLASS, $className);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
145
    }
146
147
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
148
     * {@inheritdoc}
149
     */
150
    public function asColumn(int $column = 0): ResultSetInterface
151
    {
152
        return $this->fetchMode(self::FETCH_COLUMN, $column);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_COLUMN has been deprecated: Use asColumn() instead ( Ignorable by Annotation )

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

152
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_COLUMN, $column);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 585
    public function all()
159
    {
160 585
        return $this->statement->fetchAll();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetchAll() has been deprecated: Use fetchAllNumeric(), fetchAllAssociative() or fetchFirstColumn() instead. ( Ignorable by Annotation )

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

160
        return /** @scrutinizer ignore-deprecated */ $this->statement->fetchAll();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->statement->fetchAll() returns the type array|array<mixed,mixed> which is incompatible with the return type mandated by Bdf\Prime\Connection\Res...sultSetInterface::all() of Bdf\Prime\Connection\Result\list.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 4
    public function rewind()
167
    {
168 4
        $this->current = $this->statement->fetch();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

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

168
        $this->current = /** @scrutinizer ignore-deprecated */ $this->statement->fetch();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
169 4
        $this->key = 0;
170 4
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 457
    public function count()
176
    {
177 457
        return $this->statement->rowCount();
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function isRead(): bool
184
    {
185
        return true;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function isWrite(): bool
192
    {
193
        return false;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function hasWrite(): bool
200
    {
201
        return false;
202
    }
203
204
    /**
205
     * Close the cursor on result set destruction
206
     */
207 607
    public function __destruct()
208
    {
209 607
        $this->statement->closeCursor();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\Res...tatement::closeCursor() has been deprecated: Use Result::free() instead. ( Ignorable by Annotation )

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

209
        /** @scrutinizer ignore-deprecated */ $this->statement->closeCursor();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
210 607
    }
211
}
212