-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeDoesNotContainType - psalm still considers a property always null even if it may be set in called method #5298
Labels
Comments
I found these snippets: https://psalm.dev/r/2afe4e3c4b<?php
class UnicodeUnescaper
{
/**
* First codepoint of Unicode surrogate pair
* @var int|null
*/
private $pairFirst;
public function unescapeUnicode(string $escaped): string
{
$this->pairFirst = null;
$unescaped = '';
foreach (
preg_split(
"/(\\\\(?:\\\\|[0-9a-fA-F]{4}|\\+[0-9a-fA-F]{6}))/",
$escaped,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
) ?: [] as $part
) {
if ('\\' === $part[0] && '\\' !== $part[1]) {
$unescaped .= $this->handlePossibleSurrogatePairs(hexdec(ltrim($part, '\\+')));
} elseif (null !== $this->pairFirst) {
break;
} elseif ('\\' === $part[0]) {
$unescaped .= '\\';
} elseif (false !== strpos($part, '\\')) {
throw new Exception('Invalid Unicode escape');
} else {
$unescaped .= $part;
}
}
if (null !== $this->pairFirst) {
throw new Exception('Unfinished Unicode surrogate pair');
}
return $unescaped;
}
private function handlePossibleSurrogatePairs(int $codepoint): string
{
$utf8 = '';
$isSurrogateFirst = 0xD800 <= $codepoint && 0xDBFF >= $codepoint;
$isSurrogateSecond = 0xDC00 <= $codepoint && 0xDFFF >= $codepoint;
if ((null !== $this->pairFirst) !== $isSurrogateSecond) {
throw new Exception("Invalid Unicode surrogate pair");
} elseif (null !== $this->pairFirst) {
$utf8 = 'pair';
$this->pairFirst = null;
} elseif ($isSurrogateFirst) {
$this->pairFirst = $codepoint;
} else {
$utf8 = 'symbol';
}
return $utf8;
}
}
// this will trigger an Exception psalm claims cannot be triggered
(new UnicodeUnescaper())->unescapeUnicode('wrong: \\db99');
|
Sorry! Your example, simplified again: https://psalm.dev/r/3d9228342e |
I found these snippets: https://psalm.dev/r/3d9228342e<?php
class A
{
private ?int $foo = null;
public function bar(): void
{
$this->foo = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$this->setFoo();
} elseif ($this->foo !== null) {
break;
}
}
if (null !== $this->foo) {}
}
private function setFoo(): void
{
$this->foo = 5;
}
}
|
sad-spirit
added a commit
to sad-spirit/pg-builder
that referenced
this issue
Jun 12, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Issue #5231 was closed, but only the "simplified" code example was actually fixed.
The code I posted originally still triggers the error with latest psalm:
https://psalm.dev/r/2afe4e3c4b
The text was updated successfully, but these errors were encountered: