| Total Complexity | 12 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 10 | class CompositeParser |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Convert composite type from PostgreSQL to PHP array |
||
| 14 | * |
||
| 15 | * @param string $value String to convert. |
||
| 16 | */ |
||
| 17 | 7 | public function parse(string $value): array|null |
|
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Parse PostgreSQL composite type encoded in string. |
||
| 28 | * |
||
| 29 | * @param string $value String to parse. |
||
| 30 | */ |
||
| 31 | 7 | private function parseComposite(string $value): array |
|
| 32 | { |
||
| 33 | 7 | for ($result = [], $i = 1;; ++$i) { |
|
| 34 | 7 | $result[] = match ($value[$i]) { |
|
| 35 | 7 | ',', ')' => null, |
|
| 36 | 7 | '"' => $this->parseQuotedString($value, $i), |
|
| 37 | 7 | default => $this->parseUnquotedString($value, $i), |
|
| 38 | 7 | }; |
|
| 39 | |||
| 40 | 7 | if ($value[$i] === ')') { |
|
| 41 | 7 | return $result; |
|
| 42 | } |
||
| 43 | } |
||
|
|
|||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Parses quoted string. |
||
| 48 | */ |
||
| 49 | 7 | private function parseQuotedString(string $value, int &$i): string |
|
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Parses unquoted string. |
||
| 65 | */ |
||
| 66 | 7 | private function parseUnquotedString(string $value, int &$i): string |
|
| 74 | } |
||
| 75 | } |
||
| 77 |
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: