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

Calling non-nullable by-ref param with unset should result in error #1916

Closed
muglug opened this issue Feb 20, 2019 · 5 comments
Closed

Calling non-nullable by-ref param with unset should result in error #1916

muglug opened this issue Feb 20, 2019 · 5 comments

Comments

@muglug
Copy link
Contributor

muglug commented Feb 20, 2019

Snippet:

<?php
function foo(string &$s) : void {}
function bar() : void {
  foo($byref);
}

Expected: complain about mismatching non-nullable param with null argument
Actual: no issue - https://phpstan.org/r/18f1ffe1-738c-46e2-8c57-f10e2e078867

@adaamz
Copy link
Contributor

adaamz commented Feb 21, 2019

ref https://3v4l.org/RCajG

@ondrejmirtes
Copy link
Member

This is more complicated. We have different types of by-ref parameters:

  1. Built-in functions with read-write parameter. Types are checked.
  2. Built-in functions with write parameter. Types are not checked.
  3. Userland functions with by-ref parameters - we don't know whether they are write or read-write so we assume they can always create new variable. (they are write) Types are not checked.

Currently, read-write parameter is represented as PassedByReference::createReadsArgument() and write as PassedByReference::createsNewVariable().

For this feature request to be fulfilled, we would need to have a third state - "this userland function might create a variable, but check the type anyway".

@muglug
Copy link
Contributor Author

muglug commented Feb 26, 2019

3. Userland functions with by-ref parameters - we don't know whether they are write or read-write so we assume they can always create new variable. (they are write) Types are not checked.

But that's erroneous - all userland functions with a declared non-nullable type are read-write - types are always checked e.g.

function foo(string &$s) : void {}

$bar = [];
foo($bar);

@muglug
Copy link
Contributor Author

muglug commented Feb 26, 2019

When only a @param type is included I still treat it as a limitation on the read portion of the variable, and I'm working on improving support for @param-out to allow changing types in the write phase (currently it just allows type narrowing).

@phpstan-bot
Copy link
Contributor

@muglug After the latest push in 1.11.x, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+5: Variable $bar might not be defined.
Full report
Line Error
5 Variable $bar might not be defined.

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

No branches or pull requests

4 participants