Passed
Push — master ( c4f127...730a00 )
by Sergei
24:33 queued 20:27
created

CompositeParser::parseUnquotedString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql\Composite;
6
7
/**
8
 * Composite type representation to PHP array parser for PostgreSQL Server.
9
 */
10
final class CompositeParser
11
{
12
    /**
13
     * Converts composite type value 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
     * Parses PostgreSQL composite type value 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
        }
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...
44
    }
45
46
    /**
47
     * Parses quoted string.
48
     */
49 6
    private function parseQuotedString(string $value, int &$i): string
50
    {
51 6
        for ($result = '', ++$i;; ++$i) {
52 6
            if ($value[$i] === '\\') {
53 1
                ++$i;
54 6
            } elseif ($value[$i] === '"') {
55 6
                ++$i;
56 6
                return $result;
57
            }
58
59 6
            $result .= $value[$i];
60
        }
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...
61
    }
62
63
    /**
64
     * Parses unquoted string.
65
     */
66 6
    private function parseUnquotedString(string $value, int &$i): string
67
    {
68 6
        for ($result = '';; ++$i) {
69 6
            if (in_array($value[$i], [',', ')'], true)) {
70 6
                return $result;
71
            }
72
73 6
            $result .= $value[$i];
74
        }
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...
75
    }
76
}
77