Skip to content
Closed
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
6 changes: 4 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2998,12 +2998,14 @@ private function resolveNormalizedIdentical(Expr\BinaryOp\Identical $expr, Scope
$never = new NeverType();
$contextForTypes = $identicalType->getValue() ? $context->negate() : $context;
if ($leftExpr instanceof AlwaysRememberedExpr) {
$leftTypes = $this->create($unwrappedLeftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr);
$leftTypes = $this->create($leftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr)
->unionWith($this->create($unwrappedLeftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr));
} else {
$leftTypes = $this->create($leftExpr, $never, $contextForTypes, $scope)->setRootExpr($expr);
}
if ($rightExpr instanceof AlwaysRememberedExpr) {
$rightTypes = $this->create($unwrappedRightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr);
$rightTypes = $this->create($rightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr)
->unionWith($this->create($unwrappedRightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr));
} else {
$rightTypes = $this->create($rightExpr, $never, $contextForTypes, $scope)->setRootExpr($expr);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Rules/Exceptions/Bug14396Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Exceptions;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

/**
* @extends RuleTestCase<MissingCheckedExceptionInFunctionThrowsRule>
*/
class Bug14396Test extends RuleTestCase
{

protected function getRule(): Rule
{
return new MissingCheckedExceptionInFunctionThrowsRule(
new MissingCheckedExceptionInThrowsCheck(new DefaultExceptionTypeResolver(
self::createReflectionProvider(),
[],
[],
[],
[],
)),
);
}

protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return false;
}

#[RequiresPhp('>= 8.1')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/bug-14396.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/bug-14396.neon',
];
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Exceptions/bug-14396.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
treatPhpDocTypesAsCertain: false
exceptions:
check:
missingCheckedExceptionInThrows: true
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-14396.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php // lint >= 8.1

declare(strict_types=1);

namespace Bug14396;

enum Status {
case A;
case B;
case C;
}

class Item {
public function __construct(
public ?Status $status
) {}
}

/**
* @param list<Item> $list
*/
function countAFromCollection(array $list): int
{
$count = 0;

foreach ($list as $item) {
match ($item->status) {
Status::A => ++$count,
Status::B,
Status::C,
null => null,
};
}

return $count;
}

function countAFromItem(Item $item): ?int {
return match ($item->status) {
Status::A => 1,
Status::B,
Status::C,
null => null,
};
}
Loading