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

linter: initial support of complex instanceof for if/else and ternary #1038

Merged
merged 21 commits into from
Jul 13, 2021

Conversation

i582
Copy link
Contributor

@i582 i582 commented Jul 7, 2021

Description

  1. Added processing in the if / else chain (including for expressions):
/**
 * @param mixed $a
 */
function f($a) {
  if ($a instanceof Foo) {
    exprtype($a, "\Foo");
  } else if ($a instanceof Boo) {
    exprtype($a, "\Boo");
  } else {
    exprtype($a, "mixed");
  }

  exprtype($a, "mixed");
}
  1. Added processing for negation (including for expressions):
/**
 * @param Foo|Boo|Zoo $a
 */
function f($a) {
  if (!$a instanceof Foo) {
    exprtype($a, "\Boo|\Zoo");
  } else if (!$a instanceof Foo) {
    exprtype($a, "mixed");
  } else {
    exprtype($a, "\Foo");
  }
}
  1. Added support for the ternary operator:
/**
 * @param Foo|Boo $a
 */
function f($a) {
  $_ = $a instanceof Foo ? 
  			exprtype($a, "\Foo") : 
  			0;

  $_ = $a instanceof Foo ? 
			exprtype($a, "\Foo") : 
			exprtype($a, "\Boo");

  $_ = isset($a->aaa) && $a->aaa instanceof Foo ? 
			exprtype($a->aaa, "\Foo") : 
			exprtype($a->aaa, "mixed");
}

/**
 * @param mixed $a
 */
function f($a) {
  $_ = $a instanceof Foo ? 
  			exprtype($a, "\Foo") : 
  			exprtype($a, "mixed");
  $_ = $a instanceof Foo ? 
  			exprtype($a, "\Foo") : 
  			($a instanceof Boo ? 
  				exprtype($a, "\Boo") : 
  				exprtype($a, "mixed"));
}

/**
 * @param Foo|Boo $a
 */
function f($a) {
  $_ = !$a instanceof Foo ? 
  			0 : 
  			exprtype($a, "\Foo");

  $_ = !$a instanceof Foo ? 
			exprtype($a, "\Boo") : 
			exprtype($a, "\Foo");

  $_ = isset($a->aaa) && !$a->aaa instanceof Foo ? 
			exprtype($a->aaa, "mixed") : 
			exprtype($a->aaa, "\Foo");
}
  1. Added initial processing of variables in conditions:
if (($a = new Foo) && $a instanceof Foo) {
  exprtype($a, "\Foo");
}

if ((($a = new Foo) || ($a = 10)) && $a instanceof Foo) {
  exprtype($a, "\Foo");
} else {
  exprtype($a, "precise int");
}

Epic #1037
Fixes #370
#363

@i582 i582 added the enhancement New feature or request label Jul 7, 2021
Copy link
Contributor

@quasilyte quasilyte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move closure improvements to another PR.
We can merge them right away then.

@i582
Copy link
Contributor Author

i582 commented Jul 7, 2021

Please move closure improvements to another PR.
We can merge them right away then.

Done.

@i582 i582 mentioned this pull request Jul 7, 2021
@i582 i582 added this to the v0.4.0 milestone Jul 7, 2021
@quasilyte
Copy link
Contributor

quasilyte commented Jul 13, 2021

We discussed some ! nesting.
If you think that it's not an issue right now, feel free to merge.

@i582 i582 merged commit 48cb61f into master Jul 13, 2021
@i582 i582 deleted the pmakhnev/add_smartcasts_for_instanceof branch July 13, 2021 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No warnings for if-else with instanceof chains
2 participants