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

PHP8 Mixed Type Not Being Detected As Nullable #4276

Closed
DeveloperRob opened this issue Nov 30, 2020 · 1 comment
Closed

PHP8 Mixed Type Not Being Detected As Nullable #4276

DeveloperRob opened this issue Nov 30, 2020 · 1 comment
Labels
bug A crash in Phan, a recent change causing incorrect analysis results, etc. php8 Tracks changes to prepare for analyzing php 8 code

Comments

@DeveloperRob
Copy link
Contributor

Tested & confirmed on:

  • 4.0.0-alpha5
  • 3.2.6
  • master

Summary
With the introduction of a 'mixed' type in PHP8's own language, but the core engine not supporting nullable mixed (?mixed), it seems that (at least) PhanCoalescingNeverNull now doesn't pick up internal functions that have a mixed return type can return null.

Example Code

<?php
declare(strict_types=1);

class Example{
	/** @var array */
	private $decodedJson;

	public function decodeJson(){
		$this->decodedJson = json_decode('{"this:"is not valid json"', true) ?? [];
	}
}

Expected Output
No errors

Actual Output

PhanCoalescingNeverNull Using non-null json_decode('{"this:"is not valid json"', true) of type mixed as the left hand side of a null coalescing (??) operation. The right hand side may be unnecessary.
@TysonAndre
Copy link
Member

This is a bug.

I'm also able to reproduce this under php 8.0 with non-literals - the intent was to make mixed include all types including null, and ?mixed to indicate a case where Phan sees null is very plausible in one or more code paths

<?php
function decode_or_default(string $json) {
    return json_decode($json) ?? 'default';
}

@TysonAndre TysonAndre added bug A crash in Phan, a recent change causing incorrect analysis results, etc. php8 Tracks changes to prepare for analyzing php 8 code labels Dec 1, 2020
TysonAndre added a commit to TysonAndre/phan that referenced this issue Dec 1, 2020
- Work on fixing the false positives during redundant condition
  detection
- Add a separate `non-null-mixed` type
- TODO: Avoid regressions in inferred phpdoc type for array access
- TODO: Continue to infer phpdoc `?mixed` type with a `?` in issue messages
  when combining mixed with null or converting mixed to nullable.
  (and emit PhanTypeArraySuspiciousNullable)

For phan#4276
TysonAndre added a commit to TysonAndre/phan that referenced this issue Dec 7, 2020
- Work on fixing the false positives during redundant condition
  detection
- Add a separate `non-null-mixed` type
- TODO: Avoid regressions in inferred phpdoc type for array access
- TODO: Continue to infer phpdoc `?mixed` type with a `?` in issue messages
  when combining mixed with null or converting mixed to nullable.
  (and emit PhanTypeArraySuspiciousNullable)

For phan#4276
TysonAndre added a commit that referenced this issue Dec 7, 2020
- Work on fixing the false positives during redundant condition
  detection
- Add a separate `non-null-mixed` type
- Continue to infer phpdoc `?mixed` type with a `?` in issue messages
  when combining mixed with null or converting mixed to nullable.
  (and emit PhanTypeArraySuspiciousNullable)

For #4276

Support non-null-mixed

Update tests and implementation to handle edge cases uncovered by the
previous commit.

Run phpcbf
TysonAndre added a commit to TysonAndre/phan that referenced this issue Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash in Phan, a recent change causing incorrect analysis results, etc. php8 Tracks changes to prepare for analyzing php 8 code
Projects
None yet
Development

No branches or pull requests

2 participants