Passed
Pull Request — master (#561)
by
unknown
02:54
created

UtfString::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser;
6
7
use ArrayAccess;
8
use Exception;
9
use Stringable;
10
11
use function count;
12
use function implode;
13
use function mb_check_encoding;
14
use function mb_str_split;
15
16
/**
17
 * Implementation for UTF-8 strings.
18
 *
19
 * The subscript operator in PHP, when used with string will return a byte and not a character. Because in UTF-8
20
 * strings a character may occupy more than one byte, the subscript operator may return an invalid character.
21
 *
22
 * Because the lexer relies on the subscript operator this class had to be implemented.
23
 *
24
 * Implements array-like access for UTF-8 strings.
25
 *
26
 * In this library, this class should be used to parse UTF-8 queries.
27
 *
28
 * @implements ArrayAccess<int, string>
29
 */
30
class UtfString implements ArrayAccess, Stringable
31
{
32
    /**
33
     * The multi-byte characters.
34
     *
35
     * @var list<string>
0 ignored issues
show
Bug introduced by
The type PhpMyAdmin\SqlParser\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
     */
37
    public array $characters;
38
39
    /** @param string $str the string */
40 28
    public function __construct(string $str)
41
    {
42 28
        if (mb_check_encoding($str, 'UTF-8')) {
43 26
            $this->characters = mb_str_split($str, 1, 'UTF-8');
0 ignored issues
show
Documentation Bug introduced by
It seems like mb_str_split($str, 1, 'UTF-8') of type string[] is incompatible with the declared type PhpMyAdmin\SqlParser\list of property $characters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        } else {
45 2
            $this->characters = [];
46
        }
47
    }
48
49
    /**
50
     * Checks if the given offset exists.
51
     *
52
     * @param int $offset the offset to be checked
53
     */
54 2
    public function offsetExists(mixed $offset): bool
55
    {
56 2
        return $offset >= 0 && $offset < count($this->characters);
57
    }
58
59
    /**
60
     * Gets the character at given offset.
61
     *
62
     * @param int $offset the offset to be returned
63
     */
64 22
    public function offsetGet(mixed $offset): string|null
65
    {
66 22
        return $this->characters[$offset] ?? null;
67
    }
68
69
    /**
70
     * Sets the value of a character.
71
     *
72
     * @param int    $offset the offset to be set
73
     * @param string $value  the value to be set
74
     *
75
     * @throws Exception not implemented.
76
     */
77 2
    public function offsetSet(mixed $offset, mixed $value): void
78
    {
79 2
        throw new Exception('Not implemented.');
80
    }
81
82
    /**
83
     * Unsets an index.
84
     *
85
     * @param int $offset the value to be unset
86
     *
87
     * @throws Exception not implemented.
88
     */
89 2
    public function offsetUnset(mixed $offset): void
90
    {
91 2
        throw new Exception('Not implemented.');
92
    }
93
94
    /**
95
     * Returns the length in characters of the string.
96
     */
97 10
    public function length(): int
98
    {
99 10
        return count($this->characters);
100
    }
101
102
    /**
103
     * Returns the contained string.
104
     */
105 2
    public function __toString(): string
106
    {
107 2
        return implode('', $this->characters);
108
    }
109
}
110