Issues (590)

src/Connection/Result/DoctrineResultSet.php (7 issues)

1
<?php
2
3
namespace Bdf\Prime\Connection\Result;
4
5
use Bdf\Prime\Connection\Result\FetchStrategy\AssociativeDoctrineFetch;
6
use Bdf\Prime\Connection\Result\FetchStrategy\ClassDoctrineFetch;
7
use Bdf\Prime\Connection\Result\FetchStrategy\ColumnDoctrineFetch;
8
use Bdf\Prime\Connection\Result\FetchStrategy\DoctrineFetchStrategyInterface;
9
use Bdf\Prime\Connection\Result\FetchStrategy\ListDoctrineFetch;
10
use Bdf\Prime\Connection\Result\FetchStrategy\ObjectDoctrineFetch;
11
use Bdf\Prime\Exception\DBALException;
12
use Doctrine\DBAL\Result;
13
14
/**
15
 * Adapt Doctrine result to result set
16
 *
17
 * @template T
18
 * @implements ResultSetInterface<T>
19
 */
20
final class DoctrineResultSet implements ResultSetInterface
21
{
22
    /**
23
     * @var Result
24
     * @readonly
25
     */
26
    private Result $result;
27
28
    private int $key = 0;
29
30
    /**
31
     * @var T|false|null
0 ignored issues
show
The type Bdf\Prime\Connection\Result\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     */
33
    private $current;
34
35
    /**
36
     * @var DoctrineFetchStrategyInterface<T>
37
     */
38
    private DoctrineFetchStrategyInterface $strategy;
39
40
41
    /**
42
     * PdoResultSet constructor.
43
     *
44
     * @param Result $result
45
     * @psalm-this-out DoctrineResultSet<array<string, mixed>>
46
     */
47 817
    public function __construct(Result $result)
48
    {
49 817
        $this->result = $result;
50
        /** @var DoctrineResultSet<array<string, mixed>> $this */
51 817
        $this->strategy = AssociativeDoctrineFetch::instance();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    #[\ReturnTypeWillChange]
58 686
    public function current()
59
    {
60 686
        if ($this->current === null) {
61 607
            $this->rewind();
62
        }
63
64
        /** @var T */
65 686
        return $this->current;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 462
    public function next(): void
72
    {
73 462
        $this->current = $this->strategy->one($this->result);
74 462
        ++$this->key;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    #[\ReturnTypeWillChange]
81 2
    public function key()
82
    {
83 2
        return $this->key;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 473
    public function valid(): bool
90
    {
91 473
        return $this->current !== false;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 5
    public function fetchMode($mode, $options = null)
98
    {
99
        switch ($mode) {
100 5
            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

100
            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...
101
                return $this->asAssociative();
102
103 5
            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

103
            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...
104 1
                return $this->asList();
105
106 4
            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

106
            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...
107 1
                return $this->asObject();
108
109 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

109
            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...
110 2
                return $this->asColumn($options ?? 0);
111
112 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

112
            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...
113 1
                return $this->asClass($options);
114
115
            default:
116
                throw new DBALException('Unsupported fetch mode '.$mode);
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function asAssociative(): ResultSetInterface
124
    {
125
        /** @var DoctrineResultSet<array<string, mixed>> $this */
126 1
        $this->strategy = AssociativeDoctrineFetch::instance();
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 2
    public function asList(): ResultSetInterface
135
    {
136
        /** @var DoctrineResultSet<list<mixed>> $this */
137 2
        $this->strategy = ListDoctrineFetch::instance();
138
139 2
        return $this;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 6
    public function asObject(): ResultSetInterface
146
    {
147
        /** @var DoctrineResultSet<\stdClass> $this */
148 6
        $this->strategy = ObjectDoctrineFetch::instance();
149
150 6
        return $this;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     *
156
     * @param class-string<E> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<E> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<E>.
Loading history...
157
     * @param list<mixed> $constructorArguments
158
     *
159
     * @return static<E>
160
     *
161
     * @template E
162
     */
163 3
    public function asClass(string $className, array $constructorArguments = []): ResultSetInterface
164
    {
165
        /** @var DoctrineResultSet<E> $this */
166 3
        $this->strategy = new ClassDoctrineFetch($className, $constructorArguments);
167
168 3
        return $this;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174 49
    public function asColumn(int $column = 0): ResultSetInterface
175
    {
176
        /** @var DoctrineResultSet<mixed> $this */
177 49
        $this->strategy = new ColumnDoctrineFetch($column);
178
179 49
        return $this;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 133
    public function all(): array
186
    {
187 133
        return $this->strategy->all($this->result);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 690
    public function rewind(): void
194
    {
195 690
        $this->current = $this->strategy->one($this->result);
196 690
        $this->key = 0;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function count(): int
203
    {
204
        return $this->result->rowCount();
205
    }
206
207
    /**
208
     * {@inheritdoc}
209
     */
210 1
    public function isRead(): bool
211
    {
212 1
        return true;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218 1
    public function isWrite(): bool
219
    {
220 1
        return false;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 1
    public function hasWrite(): bool
227
    {
228 1
        return false;
229
    }
230
231
    /**
232
     * Close the cursor on result set destruction
233
     */
234 817
    public function __destruct()
235
    {
236 817
        $this->result->free();
237
    }
238
}
239