Skip to content

Commit c1579f1

Browse files
committed
Allow also $object::class with Identical
1 parent 9167061 commit c1579f1

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

‎src/Analyser/TypeSpecifier.php‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,25 @@ public function specifyTypesInCondition(
244244
}
245245
}
246246

247+
$rightType = $scope->getType($expr->right);
248+
if (
249+
$expr->left instanceof ClassConstFetch &&
250+
$expr->left->class instanceof Expr &&
251+
$expr->left->name instanceof Node\Identifier &&
252+
$expr->right instanceof ClassConstFetch &&
253+
$rightType instanceof ConstantStringType &&
254+
strtolower($expr->left->name->toString()) === 'class'
255+
) {
256+
return $this->specifyTypesInCondition(
257+
$scope,
258+
new Instanceof_(
259+
$expr->left->class,
260+
new Name($rightType->getValue())
261+
),
262+
$context
263+
);
264+
}
265+
247266
if ($context->true()) {
248267
$type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left));
249268
$leftTypes = $this->create($expr->left, $type, $context, false, $scope);
@@ -362,10 +381,7 @@ public function specifyTypesInCondition(
362381
) {
363382
return $this->specifyTypesInCondition(
364383
$scope,
365-
new Instanceof_(
366-
$expr->left->class,
367-
new Name($rightType->getValue())
368-
),
384+
new Expr\BinaryOp\Identical($expr->left, $expr->right),
369385
$context
370386
);
371387
}
Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
declare(strict_types = 1);
44

55
namespace Bug4896;
66

77
use function PHPStan\Testing\assertType;
88

9-
/** @var \DateTime|\DateInterval $command */
10-
$command = new \DateTime();
11-
12-
switch ($command::class) {
13-
case \DateTime::class:
14-
assertType(\DateTime::class, $command);
15-
var_dump($command->getTimestamp());
16-
break;
17-
case \DateInterval::class:
18-
assertType(\DateInterval::class, $command);
19-
echo " Hello Date Interval " . $command->format('d');
20-
break;
9+
class Foo
10+
{
11+
12+
public function doFoo(\DateTime|\DateInterval $command): void
13+
{
14+
switch ($command::class) {
15+
case \DateTime::class:
16+
assertType(\DateTime::class, $command);
17+
break;
18+
case \DateInterval::class:
19+
assertType(\DateInterval::class, $command);
20+
break;
21+
}
22+
23+
}
24+
25+
}
26+
27+
class Bar
28+
{
29+
30+
public function doFoo(\DateTime|\DateInterval $command): void
31+
{
32+
match ($command::class) {
33+
\DateTime::class => assertType(\DateTime::class, $command),
34+
\DateInterval::class => assertType(\DateInterval::class, $command),
35+
};
36+
}
37+
2138
}
Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
1-
<?php
1+
<?php // lint >= 8.0
22

33
namespace Bug5843;
44

55
use function PHPStan\Testing\assertType;
66

7-
function foo(object $object): void
7+
class Foo
88
{
9-
switch ($object::class) {
10-
case \DateTime::class:
11-
assertType(\DateTime::class, $object);
12-
$object->modify('+1 day');
13-
break;
14-
case \Throwable::class:
15-
assertType(\Throwable::class, $object);
16-
$object->getPrevious();
17-
break;
9+
10+
function doFoo(object $object): void
11+
{
12+
switch ($object::class) {
13+
case \DateTime::class:
14+
assertType(\DateTime::class, $object);
15+
break;
16+
case \Throwable::class:
17+
assertType(\Throwable::class, $object);
18+
break;
19+
}
20+
}
21+
22+
}
23+
24+
class Bar
25+
{
26+
27+
function doFoo(object $object): void
28+
{
29+
match ($object::class) {
30+
\DateTime::class => assertType(\DateTime::class, $object),
31+
\Throwable::class => assertType(\Throwable::class, $object),
32+
};
1833
}
34+
1935
}

0 commit comments

Comments
 (0)