Skip to content

Commit 8d711e8

Browse files
committed
String offset access should always return a string
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent b541c5a commit 8d711e8

File tree

4 files changed

+7
-53
lines changed

4 files changed

+7
-53
lines changed

psalm-baseline.xml

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -176,49 +176,6 @@
176176
<MixedOperand>
177177
<code><![CDATA[$this->str[$this->last]]]></code>
178178
</MixedOperand>
179-
<PossiblyNullArgument>
180-
<code><![CDATA[$this->str[$this->last + 1]]]></code>
181-
<code><![CDATA[$this->str[$this->last]]]></code>
182-
<code><![CDATA[$this->str[$this->last]]]></code>
183-
<code><![CDATA[$this->str[$this->last]]]></code>
184-
<code><![CDATA[$this->str[$this->last]]]></code>
185-
<code><![CDATA[$this->str[$this->last]]]></code>
186-
<code><![CDATA[$this->str[$this->last]]]></code>
187-
<code><![CDATA[$this->str[$this->last]]]></code>
188-
<code><![CDATA[$this->str[$this->last]]]></code>
189-
<code><![CDATA[$this->str[$this->last]]]></code>
190-
<code><![CDATA[$token]]></code>
191-
<code><![CDATA[$token]]></code>
192-
<code><![CDATA[$token]]></code>
193-
<code><![CDATA[$token]]></code>
194-
<code><![CDATA[$token]]></code>
195-
</PossiblyNullArgument>
196-
<PossiblyNullOperand>
197-
<code><![CDATA[$this->str[$this->last++]]]></code>
198-
<code><![CDATA[$this->str[$this->last++]]]></code>
199-
<code><![CDATA[$this->str[$this->last]]]></code>
200-
<code><![CDATA[$this->str[$this->last]]]></code>
201-
<code><![CDATA[$this->str[$this->last]]]></code>
202-
<code><![CDATA[$this->str[$this->last]]]></code>
203-
<code><![CDATA[$this->str[$this->last]]]></code>
204-
<code><![CDATA[$this->str[$this->last]]]></code>
205-
<code><![CDATA[$this->str[$this->last]]]></code>
206-
<code><![CDATA[$this->str[$this->last]]]></code>
207-
<code><![CDATA[$this->str[$this->last]]]></code>
208-
<code><![CDATA[$this->str[$this->last]]]></code>
209-
<code><![CDATA[$this->str[$this->last]]]></code>
210-
<code><![CDATA[$this->str[$this->last]]]></code>
211-
<code><![CDATA[$this->str[$this->last]]]></code>
212-
<code><![CDATA[$this->str[$this->last]]]></code>
213-
<code><![CDATA[$this->str[$this->last]]]></code>
214-
<code><![CDATA[$this->str[$this->last]]]></code>
215-
<code><![CDATA[$this->str[$this->last]]]></code>
216-
<code><![CDATA[$this->str[++$this->last]]]></code>
217-
<code><![CDATA[$this->str[++$this->last]]]></code>
218-
<code><![CDATA[$this->str[++$this->last]]]></code>
219-
<code><![CDATA[$this->str[++$this->last]]]></code>
220-
<code><![CDATA[$this->str[++$this->last]]]></code>
221-
</PossiblyNullOperand>
222179
<PossiblyNullPropertyFetch>
223180
<code><![CDATA[$next->type]]></code>
224181
<code><![CDATA[$next->value]]></code>
@@ -231,9 +188,6 @@
231188
<RiskyTruthyFalsyComparison>
232189
<code><![CDATA[! $flags]]></code>
233190
<code><![CDATA[! $flags]]></code>
234-
<code><![CDATA[Context::isComment($token)]]></code>
235-
<code><![CDATA[Context::isComment($token)]]></code>
236-
<code><![CDATA[Context::isComment($token, $end)]]></code>
237191
<code><![CDATA[empty($delimiter)]]></code>
238192
</RiskyTruthyFalsyComparison>
239193
</file>

src/Lexer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public function parseComment(): Token|null
546546
$token = $this->str[$this->last];
547547

548548
// Bash style comments. (#comment\n)
549-
if (Context::isComment($token)) {
549+
if (Context::isComment($token) !== null) {
550550
while (++$this->last < $this->len && $this->str[$this->last] !== "\n") {
551551
$token .= $this->str[$this->last];
552552
}
@@ -562,7 +562,7 @@ public function parseComment(): Token|null
562562
// C style comments. (/*comment*\/)
563563
if (++$this->last < $this->len) {
564564
$token .= $this->str[$this->last];
565-
if (Context::isComment($token)) {
565+
if (Context::isComment($token) !== null) {
566566
// There might be a conflict with "*" operator here, when string is "*/*".
567567
// This can occurs in the following statements:
568568
// - "SELECT */* comment */ FROM ..."
@@ -633,7 +633,7 @@ public function parseComment(): Token|null
633633
$end = true;
634634
}
635635

636-
if (Context::isComment($token, $end)) {
636+
if (Context::isComment($token, $end) !== null) {
637637
// Checking if this comment did not end already (```--\n```).
638638
if ($this->str[$this->last] !== "\n") {
639639
while (++$this->last < $this->len && $this->str[$this->last] !== "\n") {

src/UtfString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public function offsetExists(mixed $offset): bool
6161
*
6262
* @param int $offset the offset to be returned
6363
*/
64-
public function offsetGet(mixed $offset): string|null
64+
public function offsetGet(mixed $offset): string
6565
{
66-
return $this->characters[$offset] ?? null;
66+
return $this->characters[$offset] ?? '';
6767
}
6868

6969
/**

tests/Misc/UtfStringTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function testArrayAccess(): void
3333

3434
// offsetGet
3535
$this->assertEquals('.', $str[self::TEST_PHRASE_LEN - 1]);
36-
$this->assertNull($str[-1]);
37-
$this->assertNull($str[self::TEST_PHRASE_LEN]);
36+
$this->assertEquals('', $str[-1]);
37+
$this->assertEquals('', $str[self::TEST_PHRASE_LEN]);
3838
}
3939

4040
public function testSet(): void

0 commit comments

Comments
 (0)