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

CompositeParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
dl 0
loc 64
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 7 2
A parseQuotedString() 0 11 4
A parseUnquotedString() 0 8 3
A parseComposite() 0 11 3
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