1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Db\Pgsql; |
||
6 | |||
7 | use function in_array; |
||
8 | |||
9 | /** |
||
10 | * Array representation to PHP array parser for PostgreSQL Server. |
||
11 | */ |
||
12 | final class ArrayParser |
||
13 | { |
||
14 | /** |
||
15 | * Convert an array from PostgresSQL to PHP. |
||
16 | * |
||
17 | * @param string|null $value String to convert. |
||
18 | */ |
||
19 | 13 | public function parse(string|null $value): array|null |
|
20 | { |
||
21 | 13 | return $value !== null && $value[0] === '{' |
|
22 | 13 | ? $this->parseArray($value) |
|
23 | 13 | : null; |
|
24 | } |
||
25 | |||
26 | /** |
||
27 | * Parse PostgreSQL array encoded in string. |
||
28 | * |
||
29 | * @param string $value String to parse. |
||
30 | * @param int $i parse starting position. |
||
31 | */ |
||
32 | 13 | private function parseArray(string $value, int &$i = 0): array |
|
33 | { |
||
34 | 13 | if ($value[++$i] === '}') { |
|
35 | 1 | ++$i; |
|
36 | 1 | return []; |
|
37 | } |
||
38 | |||
39 | 13 | for ($result = [];; ++$i) { |
|
40 | 13 | $result[] = match ($value[$i]) { |
|
41 | 13 | '{' => $this->parseArray($value, $i), |
|
42 | 13 | ',', '}' => null, |
|
43 | 13 | '"' => $this->parseQuotedString($value, $i), |
|
44 | 13 | default => $this->parseUnquotedString($value, $i), |
|
45 | 13 | }; |
|
46 | |||
47 | 13 | if ($value[$i] === '}') { |
|
48 | 13 | ++$i; |
|
49 | 13 | return $result; |
|
50 | } |
||
51 | } |
||
0 ignored issues
–
show
|
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Parses quoted string. |
||
56 | */ |
||
57 | 7 | private function parseQuotedString(string $value, int &$i): string |
|
58 | { |
||
59 | 7 | for ($result = '', ++$i;; ++$i) { |
|
60 | 7 | if ($value[$i] === '\\') { |
|
61 | 2 | ++$i; |
|
62 | 7 | } elseif ($value[$i] === '"') { |
|
63 | 7 | ++$i; |
|
64 | 7 | return $result; |
|
65 | } |
||
66 | |||
67 | 7 | $result .= $value[$i]; |
|
68 | } |
||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return string . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Parses unquoted string. |
||
73 | */ |
||
74 | 13 | private function parseUnquotedString(string $value, int &$i): string|null |
|
75 | { |
||
76 | 13 | for ($result = '';; ++$i) { |
|
77 | 13 | if (in_array($value[$i], [',', '}'], true)) { |
|
78 | 13 | return $result !== 'NULL' |
|
79 | 11 | ? $result |
|
80 | 13 | : null; |
|
81 | } |
||
82 | |||
83 | 13 | $result .= $value[$i]; |
|
84 | } |
||
85 | } |
||
86 | } |
||
87 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: