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
16 changes: 16 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ public function findSpecifiedType(
}
}
}
$isInTrait = $scope->isInTrait();
foreach ($sureTypes as $sureType) {
if (self::isSpecified($typeSpecifierScope, $node, $sureType[0])) {
$results[] = TrinaryLogic::createMaybe();
Expand All @@ -322,6 +323,11 @@ public function findSpecifiedType(
$argumentType = $scope->getType($assignedInCallVar->expr);
}

if ($isInTrait && self::isThis($sureType[0]) && $resultType->getObjectClassNames() !== []) {
Copy link
Contributor

Choose a reason for hiding this comment

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

$results[] = TrinaryLogic::createMaybe();
continue;
}

$results[] = $resultType->isSuperTypeOf($argumentType)->result;
}

Expand All @@ -340,6 +346,11 @@ public function findSpecifiedType(
/** @var Type $resultType */
$resultType = $sureNotType[1];

if ($isInTrait && self::isThis($sureNotType[0]) && $resultType->getObjectClassNames() !== []) {
$results[] = TrinaryLogic::createMaybe();
continue;
}

$results[] = $resultType->isSuperTypeOf($argumentType)->negate()->result;
}

Expand Down Expand Up @@ -376,6 +387,11 @@ private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
) && $scope->hasExpressionType($expr)->yes();
}

private static function isThis(Expr $expr): bool
{
return $expr instanceof Expr\Variable && $expr->name === 'this';
}

/**
* @param Node\Arg[] $args
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,4 +1179,10 @@ public function testBug14177(): void
$this->analyse([__DIR__ . '/data/bug-14177.php'], []);
}

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

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