Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 8, 2023
1 parent 28c2504 commit deb2c37
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/Type/Php/GetClassDynamicReturnTypeExtension.php
Expand Up @@ -17,7 +17,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeWithClassName;
Expand All @@ -37,19 +37,23 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
$args = $functionCall->getArgs();

if (count($args) === 0) {
if ($scope->isInTrait()) {
return new ClassStringType();
}

if ($scope->isInClass()) {
return new ConstantStringType($scope->getClassReflection()->getName(), true);
}

return new ConstantBooleanType(false);
}

if ($scope->isInTrait()) {
return new StringType();
}

$argType = $scope->getType($args[0]->value);

if ($scope->isInTrait() && $argType instanceof ThisType) {
return new ClassStringType();
}

return TypeTraverser::map(
$argType,
static function (Type $type, callable $traverse): Type {
Expand Down
Expand Up @@ -625,23 +625,55 @@ public function testBug3633(): void
$this->analyse([__DIR__ . '/data/bug-3633.php'], [
[
'Strict comparison using === between class-string<Bug3633\HelloWorld> and \'Bug3633\\\OtherClass\' will always evaluate to false.',
23,
37,
],
[
'Strict comparison using === between \'Bug3633\\\HelloWorld\' and \'Bug3633\\\HelloWorld\' will always evaluate to true.',
41,
],
[
'Strict comparison using === between \'Bug3633\\\HelloWorld\' and \'Bug3633\\\OtherClass\' will always evaluate to false.',
44,
],
[
'Strict comparison using === between class-string<Bug3633\OtherClass> and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
35,
64,
],
[
'Strict comparison using === between \'Bug3633\\\OtherClass\' and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
71,
],
[
'Strict comparison using === between \'Bug3633\\\OtherClass\' and \'Bug3633\\\OtherClass\' will always evaluate to true.',
74,
],
[
'Strict comparison using === between class-string<Bug3633\FinalClass> and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
50,
93,
],
[
'Strict comparison using === between class-string<Bug3633\FinalClass> and \'Bug3633\\\OtherClass\' will always evaluate to false.',
53,
96,
],
[
'Strict comparison using === between \'Bug3633\\\FinalClass\' and \'Bug3633\\\FinalClass\' will always evaluate to true.',
102,
],
[
'Strict comparison using === between \'Bug3633\\\FinalClass\' and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
106,
],
[
'Strict comparison using === between \'Bug3633\\\FinalClass\' and \'Bug3633\\\OtherClass\' will always evaluate to false.',
109,
],
[
'Strict comparison using !== between \'Bug3633\\\FinalClass\' and \'Bug3633\\\FinalClass\' will always evaluate to false.',
112,
],
[
'Strict comparison using === between \'Bug3633\\\FinalClass\' and \'Bug3633\\\FinalClass\' will always evaluate to true.',
59,
115,
],
]);
}
Expand Down
71 changes: 67 additions & 4 deletions tests/PHPStan/Rules/Comparison/data/bug-3633.php
Expand Up @@ -3,50 +3,93 @@
namespace Bug3633;

trait Foo {
public function test(): void {
public function test($obj): void {
if (get_class($this) === HelloWorld::class) {
echo "OK";
}
if (get_class($this) === OtherClass::class) {
echo "OK";
}

if (get_class() === HelloWorld::class) {
echo "OK";
}
if (get_class() === OtherClass::class) {
echo "OK";
}

if (get_class($obj) === HelloWorld::class) {
echo "OK";
}
if (get_class($obj) === OtherClass::class) {
echo "OK";
}
}
}

class HelloWorld {
use Foo;

public function bar(): void {
public function bar($obj): void {
if (get_class($this) === HelloWorld::class) {
echo "OK";
}
if (get_class($this) === OtherClass::class) {
echo "OK";
}

if (get_class() === HelloWorld::class) {
echo "OK";
}
if (get_class() === OtherClass::class) {
echo "OK";
}

if (get_class($obj) === HelloWorld::class) {
echo "OK";
}
if (get_class($obj) === OtherClass::class) {
echo "OK";
}


$this->test();
}
}

class OtherClass {
use Foo;

public function bar(): void {
public function bar($obj): void {
if (get_class($this) === HelloWorld::class) {
echo "OK";
}
if (get_class($this) === OtherClass::class) {
echo "OK";
}

if (get_class() === HelloWorld::class) {
echo "OK";
}
if (get_class() === OtherClass::class) {
echo "OK";
}

if (get_class($obj) === HelloWorld::class) {
echo "OK";
}
if (get_class($obj) === OtherClass::class) {
echo "OK";
}

$this->test();
}
}

final class FinalClass {
use Foo;

public function bar(): void {
public function bar($obj): void {
if (get_class($this) === HelloWorld::class) {
echo "OK";
}
Expand All @@ -60,6 +103,26 @@ public function bar(): void {
echo "OK";
}

if (get_class() === HelloWorld::class) {
echo "OK";
}
if (get_class() === OtherClass::class) {
echo "OK";
}
if (get_class() !== FinalClass::class) {
echo "OK";
}
if (get_class() === FinalClass::class) {
echo "OK";
}

if (get_class($obj) === HelloWorld::class) {
echo "OK";
}
if (get_class($obj) === OtherClass::class) {
echo "OK";
}

$this->test();
}
}

0 comments on commit deb2c37

Please sign in to comment.