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

Variable assignment inside if-statement gives unexpected type #2413

Closed
Cxarli opened this issue Sep 9, 2019 · 10 comments
Closed

Variable assignment inside if-statement gives unexpected type #2413

Cxarli opened this issue Sep 9, 2019 · 10 comments
Labels
Milestone

Comments

@Cxarli
Copy link

Cxarli commented Sep 9, 2019

Bug report

Version: PHPStan - PHP Static Analysis Tool 0.11.x-dev@f6fed5f (but also occurs on the web version)

If you assign a variable inside an if-statement, it seems the type hinting derived from the rest of that if-statement is lost inside the actual clause.

Code snippet that reproduces the problem

<?php
class TestClass {
    /**
     * @var string
     */
    public $field = 'value';
}

/**
 * @return TestClass|null
 */
function getTest() {
    // Impossible but successfully mutes the `Function getTest() never returns TestClass` warning
    if (1 + 1 == 3) return new TestClass;

    return null;
}

/**
 * @return void
 */
function main() {
    if (is_object($test = getTest())) {
        // Cannot access property $field on TestClass|null
        echo $test->field;
    }
}

https://phpstan.org/r/16f1a2ba-5624-4f82-b716-0544d7427594

Expected output

I didn't expect this warning to happen. If you place the assignment before the if-statement, everything is fine. The (private) code base I'm working on has this all over the place however, so it makes using phpstan a bit harder.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Sep 9, 2019
@cs278
Copy link
Contributor

cs278 commented Jan 12, 2020

Confirmed this is still an issue in 0.12.

@guilliamxavier
Copy link

I stumbled upon the same issue with an if (is_string($x = get_string_or_false())) { /* $x is string but phpstan thinks is string|false */ }: https://phpstan.org/r/22136e13-8b8d-47e0-914e-13cab7476b82 (f1 warns, f2 doesn't)

@phpstan-bot
Copy link
Contributor

@Cxarli PHPStan now reports different result with your code snippet:

@@ @@
-25: Cannot access property $field on TestClass|null.
+No errors

@phpstan-bot
Copy link
Contributor

phpstan-bot commented Mar 3, 2021

@Cxarli PHPStan now reports different result with your code snippet:

@@ @@
-25: Cannot access property $field on TestClass|null.
+ 6: Property TestClass::$field has no typehint specified.
+12: Function getTest() has no return typehint specified.
+22: Function main() has no return typehint specified.
Full report
Line Error
6 Function main() has no return typehint specified.
12 Function main() has no return typehint specified.
22 Function main() has no return typehint specified.

@Cxarli
Copy link
Author

Cxarli commented Mar 3, 2021

That seems to fix it. Closed

@Cxarli Cxarli closed this as completed Mar 3, 2021
@ondrejmirtes
Copy link
Member

This issue isn't solved, it just moved to level 8: https://phpstan.org/r/c761685b-cc60-4d01-bbbd-c903d75453c2

@ondrejmirtes ondrejmirtes reopened this Mar 15, 2021
@phpstan-bot
Copy link
Contributor

@guilliamxavier After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-6: Parameter #1 $json of function json_decode expects string, string|false given.
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-25: Cannot access property $field on TestClass|null.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@b2bf703

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue May 11, 2021
@github-actions
Copy link

github-actions bot commented Jun 9, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants