| Total Complexity | 12 |
| Total Lines | 64 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | 6 | public function parse(string $value): array|null |
|
| 18 | { |
||
| 19 | 6 | if ($value[0] !== '(') { |
|
| 20 | 1 | return null; |
|
| 21 | } |
||
| 22 | |||
| 23 | 6 | return $this->parseComposite($value); |
|
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Parse PostgreSQL composite type encoded in string. |
||
| 28 | * |
||
| 29 | * @param string $value String to parse. |
||
| 30 | */ |
||
| 31 | 6 | private function parseComposite(string $value): array |
|
| 32 | { |
||
| 33 | 6 | for ($result = [], $i = 1;; ++$i) { |
|
| 34 | 6 | $result[] = match ($value[$i]) { |
|
| 35 | 6 | ',', ')' => null, |
|
| 36 | 6 | '"' => $this->parseQuotedString($value, $i), |
|
| 37 | 6 | default => $this->parseUnquotedString($value, $i), |
|
| 38 | 6 | }; |
|
| 39 | |||
| 40 | 6 | if ($value[$i] === ')') { |
|
| 41 | 6 | return $result; |
|
| 42 | } |
||
| 43 | } |
||
|
|
|||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Parses quoted string. |
||
| 48 | */ |
||
| 49 | 6 | private function parseQuotedString(string $value, int &$i): string |
|
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Parses unquoted string. |
||
| 65 | */ |
||
| 66 | 6 | private function parseUnquotedString(string $value, int &$i): string |
|
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 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: