-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed '$this instanceof X will always be false' in traits
- Loading branch information
Showing
9 changed files
with
377 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TraitInstanceOf; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
trait Trait1 { | ||
public function test(): string { | ||
assertType('$this(TraitInstanceOf\ATrait1Class)', $this); | ||
if ($this instanceof WithoutFoo) { | ||
assertType('$this(TraitInstanceOf\ATrait1Class)&TraitInstanceOf\WithoutFoo', $this); | ||
return 'hello world'; | ||
} | ||
|
||
if ($this instanceof FinalOther) { | ||
assertType('*NEVER*', $this); | ||
return 'hello world'; | ||
} | ||
|
||
assertType('$this(TraitInstanceOf\ATrait1Class)', $this); | ||
if ($this instanceof Trait2) { | ||
assertType('*NEVER*', $this); | ||
return 'hello world'; | ||
} | ||
|
||
if ($this instanceof FinalTrait2Class) { | ||
assertType('*NEVER*', $this); | ||
return 'hello world'; | ||
} | ||
|
||
assertType('$this(TraitInstanceOf\ATrait1Class)', $this); | ||
throw new \Error(); | ||
} | ||
} | ||
|
||
trait Trait2 { | ||
public function test(): string { | ||
assertType('$this(TraitInstanceOf\FinalTrait2Class)', $this); | ||
|
||
if ($this instanceof FinalTrait2Class) { | ||
assertType('$this(TraitInstanceOf\FinalTrait2Class)&TraitInstanceOf\FinalTrait2Class', $this); | ||
return 'hello world'; | ||
} | ||
|
||
if ($this instanceof ATrait1Class) { | ||
assertType('*NEVER*', $this); | ||
return 'hello world'; | ||
} | ||
|
||
if ($this instanceof FinalOther) { | ||
assertType('*NEVER*', $this); | ||
return 'hello world'; | ||
} | ||
|
||
return 'hello world'; | ||
} | ||
} | ||
|
||
final class FinalOther { | ||
} | ||
|
||
final class FinalTrait2Class { | ||
use Trait2; | ||
} | ||
|
||
class WithoutFoo {} | ||
|
||
class ATrait1Class { | ||
use Trait1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug3632; | ||
|
||
trait Foo { | ||
public function test(): string { | ||
if ($this instanceof HelloWorld) { | ||
return 'hello world'; | ||
} | ||
if ($this instanceof OtherClass) { | ||
return 'other class'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
} | ||
|
||
class HelloWorld | ||
{ | ||
use Foo; | ||
|
||
function bar(): string { | ||
return $this->test(); | ||
} | ||
} | ||
|
||
class OtherClass { | ||
use Foo; | ||
|
||
function bar(): string { | ||
return $this->test(); | ||
} | ||
} |
Oops, something went wrong.