Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public function findSpecifiedType(
continue;
}

if ($scope->isInTrait() && $sureType[0] instanceof Expr\Variable && $sureType[0]->name === 'this') {
$results[] = TrinaryLogic::createMaybe();
continue;
}

if ($this->treatPhpDocTypesAsCertain) {
$argumentType = $scope->getType($sureType[0]);
} else {
Expand All @@ -336,6 +341,11 @@ public function findSpecifiedType(
continue;
}

if ($scope->isInTrait() && $sureNotType[0] instanceof Expr\Variable && $sureNotType[0]->name === 'this') {
$results[] = TrinaryLogic::createMaybe();
continue;
}

if ($this->treatPhpDocTypesAsCertain) {
$argumentType = $scope->getType($sureNotType[0]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1207,4 +1207,10 @@ public function testBug13799(): void
]);
}

public function testBug13023(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-13023.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13023.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace Bug13023;

class SomeClass
{
use MyTrait;
}

class SomeClass2
{
use MyTrait;
}

trait MyTrait
{
public function getRandom(): int
{
$value = random_int(1, 100);
if (is_a($this, SomeClass::class)) {
return $value * $value;
}
return $value;
}
}
Loading