Issues (21)

src/StructuredParser.php (3 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use function in_array;
8
9
/**
10
 * Structured type representation to PHP array parser for PostgreSQL Server.
11
 */
12
final class StructuredParser
13
{
14
    /**
15
     * Converts structured (composite) type value from PostgreSQL to PHP array
16
     *
17
     * @param string $value String to convert.
18
     */
19
    public function parse(string $value): array|null
20
    {
21
        if ($value[0] !== '(') {
22
            return null;
23
        }
24
25
        return $this->parseComposite($value);
26
    }
27
28
    /**
29
     * Parses PostgreSQL composite type value encoded in string.
30
     *
31
     * @param string $value String to parse.
32
     */
33
    private function parseComposite(string $value): array
34
    {
35
        for ($result = [], $i = 1;; ++$i) {
36
            $result[] = match ($value[$i]) {
37
                ',', ')' => null,
38
                '"' => $this->parseQuotedString($value, $i),
39
                default => $this->parseUnquotedString($value, $i),
40
            };
41
42
            if ($value[$i] === ')') {
43
                return $result;
44
            }
45
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. 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
    }
}
Loading history...
46
    }
47
48
    /**
49
     * Parses quoted string.
50
     */
51
    private function parseQuotedString(string $value, int &$i): string
52
    {
53
        for ($result = '', ++$i;; ++$i) {
54
            if ($value[$i] === '\\') {
55
                ++$i;
56
            } elseif ($value[$i] === '"') {
57
                ++$i;
58
                return $result;
59
            }
60
61
            $result .= $value[$i];
62
        }
0 ignored issues
show
Bug Best Practice introduced by
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
    }
}
Loading history...
63
    }
64
65
    /**
66
     * Parses unquoted string.
67
     */
68
    private function parseUnquotedString(string $value, int &$i): string
69
    {
70
        for ($result = '';; ++$i) {
71
            if (in_array($value[$i], [',', ')'], true)) {
72
                return $result;
73
            }
74
75
            $result .= $value[$i];
76
        }
0 ignored issues
show
Bug Best Practice introduced by
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
    }
}
Loading history...
77
    }
78
}
79