Skip to content
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

Closed
sad-spirit opened this issue Feb 28, 2021 · 3 comments
Labels

Comments

@sad-spirit
Copy link
Contributor

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

ERROR: TypeDoesNotContainType - 26:23 - Type null for $this->pairFirst is always null
ERROR: TypeDoesNotContainType - 40:13 - Type null for $this->pairFirst is always null

@psalm-github-bot
Copy link

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');
Psalm output (using commit 3f4bb25):

ERROR: TypeDoesNotContainType - 26:23 - Type null for $this->pairFirst is always null

ERROR: TypeDoesNotContainType - 40:13 - Type null for $this->pairFirst is always null

@muglug muglug added the bug label Feb 28, 2021
@muglug
Copy link
Collaborator

muglug commented Feb 28, 2021

Sorry! Your example, simplified again: https://psalm.dev/r/3d9228342e

@psalm-github-bot
Copy link

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;
    }
}
Psalm output (using commit 3f4bb25):

ERROR: TypeDoesNotContainType - 13:23 - Type null for $this->foo is always null

ERROR: TypeDoesNotContainType - 18:13 - Type null for $this->foo is always null

@muglug muglug closed this as completed in 7354ec9 May 23, 2021
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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants