Issues (590)

src/Connection/Result/ArrayResultSet.php (8 issues)

1
<?php
2
3
namespace Bdf\Prime\Connection\Result;
4
5
use ArrayIterator;
6
use Bdf\Prime\Connection\Result\FetchStrategy\ArrayFetchStrategyInterface;
7
use Bdf\Prime\Connection\Result\FetchStrategy\AssociativeArrayFetch;
8
use Bdf\Prime\Connection\Result\FetchStrategy\ClassArrayFetch;
9
use Bdf\Prime\Connection\Result\FetchStrategy\ColumnArrayFetch;
10
use Bdf\Prime\Connection\Result\FetchStrategy\ListArrayFetch;
11
use Bdf\Prime\Connection\Result\FetchStrategy\ObjectArrayFetch;
12
use Bdf\Prime\Exception\DBALException;
13
14
/**
15
 * Wrap simple associative array to ResultSet
16
 * This result is useful for caches
17
 *
18
 * @template T
19
 * @implements ResultSetInterface<T>
20
 */
21
final class ArrayResultSet extends ArrayIterator implements ResultSetInterface
22
{
23
    /**
24
     * @var ArrayFetchStrategyInterface<T>
25
     */
26
    private ArrayFetchStrategyInterface $strategy;
27
28
    /**
29
     * @param list<array<string, mixed>> $array
0 ignored issues
show
The type Bdf\Prime\Connection\Result\list 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...
30
     * @param int $flags
31
     * @psalm-this-out ArrayResultSet<array<string, mixed>>
32
     */
33 44
    public function __construct($array = [], $flags = 0)
34
    {
35 44
        parent::__construct($array, $flags);
0 ignored issues
show
It seems like $array can also be of type Bdf\Prime\Connection\Result\list; however, parameter $array of ArrayIterator::__construct() 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

35
        parent::__construct(/** @scrutinizer ignore-type */ $array, $flags);
Loading history...
36
37
        /** @var ArrayResultSet<array<string, mixed>> $this */
38 44
        $this->strategy = AssociativeArrayFetch::instance();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function fetchMode($mode, $options = null)
45
    {
46
        switch ($mode) {
47 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

47
            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...
48 1
                return $this->asAssociative();
49
50 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

50
            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...
51 1
                return $this->asList();
52
53 4
            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

53
            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...
54 2
                return $this->asColumn($options ?? 0);
55
56 2
            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

56
            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...
57 1
                return $this->asObject();
58
59 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

59
            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...
60 1
                return $this->asClass($options);
61
62
            default:
63
                throw new DBALException('Unsupported fetch mode ' . $mode);
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function asAssociative(): ResultSetInterface
71
    {
72
        /** @var ArrayResultSet<array<string, mixed>> $this */
73 1
        $this->strategy = AssociativeArrayFetch::instance();
74
75 1
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 2
    public function asList(): ResultSetInterface
82
    {
83
        /** @var ArrayResultSet<list<mixed>> $this */
84 2
        $this->strategy = ListArrayFetch::instance();
85
86 2
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function asObject(): ResultSetInterface
93
    {
94
        /** @var ArrayResultSet<\stdClass> $this */
95 2
        $this->strategy = ObjectArrayFetch::instance();
96
97 2
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @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...
104
     * @param list<mixed> $constructorArguments
105
     *
106
     * @return static<E>
107
     *
108
     * @template E
109
     */
110 2
    public function asClass(string $className, array $constructorArguments = []): ResultSetInterface
111
    {
112
        /** @var ArrayResultSet<E> $this */
113 2
        $this->strategy = new ClassArrayFetch($className, $constructorArguments);
114
115 2
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    public function asColumn(int $column = 0): ResultSetInterface
122
    {
123
        /** @var ArrayResultSet<mixed> $this */
124 2
        $this->strategy = new ColumnArrayFetch($column);
125
126 2
        return $this;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 26
    public function all(): array
133
    {
134 26
        return $this->strategy->all($this->getArrayCopy());
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    #[\ReturnTypeWillChange]
141 15
    public function current()
142
    {
143 15
        $value = parent::current();
144
145 15
        if ($value === null) {
146 3
            return false;
147
        }
148
149 15
        return $this->strategy->one($value);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 12
    public function rewind(): void
156
    {
157 12
        parent::rewind();
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 1
    public function count(): int
164
    {
165 1
        return parent::count();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 1
    public function isRead(): bool
172
    {
173 1
        return true;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 1
    public function isWrite(): bool
180
    {
181 1
        return false;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 1
    public function hasWrite(): bool
188
    {
189 1
        return false;
190
    }
191
}
192