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

Type lost on str_starts_with() invoke #9593

Open
SCIF opened this issue Apr 1, 2023 · 4 comments
Open

Type lost on str_starts_with() invoke #9593

SCIF opened this issue Apr 1, 2023 · 4 comments

Comments

@SCIF
Copy link
Contributor

SCIF commented Apr 1, 2023

https://psalm.dev/r/b5e0e01fa3

Probably introduced some side-effect in #9534 but not sure tbh. I checked PR's content and it does look correct to me, so don't have idea how to fix that.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/b5e0e01fa3
<?php

/**
 * @return string|null
 */
function getStr() {
    return rand() > 5 ? '' : null;
}

$b = getStr();
$c = getStr();

if (!is_string($b) || !str_starts_with($b, 'prefix') || strlen($b) > 4) {
    throw new \Exception('Assert');
}
/** @psalm-trace $b */;
 
if (!is_string($c) || strlen($c) > 4) {
    throw new \Exception('Assert');
}
Psalm output (using commit fd4f7b7):

ERROR: PossiblyNullArgument - 13:64 - Argument 1 of strlen cannot be null, possibly null value provided

INFO: Trace - 16:23 - $b: null|string

@SCIF
Copy link
Contributor Author

SCIF commented Apr 12, 2023

@fluffycondor , could you please step in, if possible?

@fluffycondor
Copy link
Contributor

fluffycondor commented Apr 13, 2023

Looks like #9575 causes it :(
Currently negation of =assertions does nothing. Sorry to hear it even resets the type.
It works as expected without early returning
https://psalm.dev/r/8745d43ac6
Or you can split the condition
https://psalm.dev/r/f8959696d0

@psalm-github-bot
Copy link

psalm-github-bot bot commented Apr 13, 2023

I found these snippets:

https://psalm.dev/r/8745d43ac6
<?php

/**
 * @return string|null
 */
function getStr() {
    return rand() > 5 ? '' : null;
}

$b = getStr();

if (is_string($b) && str_starts_with($b, 'prefix') && strlen($b) > 4) {
	/** @psalm-trace $b */;
} else {
    throw new \Exception('Assert');
}
Psalm output (using commit 71bb951):

INFO: Trace - 13:24 - $b: non-empty-string
https://psalm.dev/r/f8959696d0
<?php

/**
 * @return string|null
 */
function getStr() {
    return rand() > 5 ? '' : null;
}

$b = getStr();

if (!is_string($b)) {
    throw new \Exception('Assert');
}
if (!str_starts_with($b, 'prefix') || strlen($b) > 4) {
    throw new \Exception('Assert');
}
/** @psalm-trace $b */;
Psalm output (using commit 71bb951):

INFO: Trace - 18:23 - $b: string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants