| Conditions | 18 |
| Paths | 19 |
| Total Lines | 63 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public static function transformPostgresArrayToPHPArray(string $postgresArray): array |
||
| 30 | { |
||
| 31 | $trimmed = \trim($postgresArray); |
||
| 32 | |||
| 33 | if ($trimmed === '' || \strtolower($trimmed) === self::POSTGRESQL_NULL_VALUE) { |
||
| 34 | return []; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (\str_contains($trimmed, '},{') || \str_starts_with($trimmed, '{{')) { |
||
| 38 | throw InvalidArrayFormatException::multiDimensionalArrayNotSupported(); |
||
| 39 | } |
||
| 40 | |||
| 41 | if ($trimmed === self::POSTGRESQL_EMPTY_ARRAY) { |
||
| 42 | return []; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Check for malformed nesting - this is a more specific check than the one above |
||
| 46 | // But we need to exclude cases where curly braces are part of quoted strings |
||
| 47 | $content = \trim($trimmed, '{}'); |
||
| 48 | $inQuotes = false; |
||
| 49 | $escaping = false; |
||
| 50 | |||
| 51 | for ($i = 0, $len = \strlen($content); $i < $len; $i++) { |
||
| 52 | $char = $content[$i]; |
||
| 53 | |||
| 54 | if ($escaping) { |
||
| 55 | $escaping = false; |
||
| 56 | |||
| 57 | continue; |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($char === '\\' && $inQuotes) { |
||
| 61 | $escaping = true; |
||
| 62 | |||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($char === '"') { |
||
| 67 | $inQuotes = !$inQuotes; |
||
|
|
|||
| 68 | } elseif (($char === '{' || $char === '}') && !$inQuotes) { |
||
| 69 | throw InvalidArrayFormatException::invalidFormat('Malformed array nesting detected'); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // Check for unclosed quotes |
||
| 74 | if ($inQuotes) { |
||
| 75 | throw InvalidArrayFormatException::invalidFormat('Unclosed quotes in array'); |
||
| 76 | } |
||
| 77 | |||
| 78 | // First try with json_decode for properly quoted values |
||
| 79 | $jsonArray = '['.\trim($trimmed, '{}').']'; |
||
| 80 | |||
| 81 | /** @var array<int, mixed>|null $decoded */ |
||
| 82 | $decoded = \json_decode($jsonArray, true, 512, JSON_BIGINT_AS_STRING); |
||
| 83 | |||
| 84 | // If json_decode fails, try manual parsing for unquoted strings |
||
| 85 | if ($decoded === null && \json_last_error() !== JSON_ERROR_NONE) { |
||
| 86 | return self::parsePostgresArrayManually($content); |
||
| 87 | } |
||
| 88 | |||
| 89 | return \array_map( |
||
| 90 | static fn (mixed $value): mixed => \is_string($value) ? self::unescapeString($value) : $value, |
||
| 91 | (array) $decoded |
||
| 92 | ); |
||
| 262 |