Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6366,8 +6366,23 @@
$declaringClass = $propertyReflection->getDeclaringClass();
if ($declaringClass->hasNativeProperty($propertyName)) {
$nativeProperty = $declaringClass->getNativeProperty($propertyName);
$propertyNativeType = $nativeProperty->getNativeType();

$assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes();

Check warning on line 6371 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $nativeProperty = $declaringClass->getNativeProperty($propertyName); $propertyNativeType = $nativeProperty->getNativeType(); - $assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes(); + $assignedTypeIsCompatible = !$propertyNativeType->isSuperTypeOf($assignedExprType)->no(); if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true);

Check warning on line 6371 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $nativeProperty = $declaringClass->getNativeProperty($propertyName); $propertyNativeType = $nativeProperty->getNativeType(); - $assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes(); + $assignedTypeIsCompatible = $assignedExprType->isSuperTypeOf($propertyNativeType)->yes(); if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true);

Check warning on line 6371 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $nativeProperty = $declaringClass->getNativeProperty($propertyName); $propertyNativeType = $nativeProperty->getNativeType(); - $assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes(); + $assignedTypeIsCompatible = !$propertyNativeType->isSuperTypeOf($assignedExprType)->no(); if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true);

Check warning on line 6371 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $nativeProperty = $declaringClass->getNativeProperty($propertyName); $propertyNativeType = $nativeProperty->getNativeType(); - $assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes(); + $assignedTypeIsCompatible = $assignedExprType->isSuperTypeOf($propertyNativeType)->yes(); if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true);
if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) {
foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) {
$accepts = $propertyNativeType->accepts($type, true);
if ($accepts->yes()) {

Check warning on line 6375 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true); - if ($accepts->yes()) { + if (!$accepts->no()) { $assignedTypeIsCompatible = true; continue; }

Check warning on line 6375 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ if (!$assignedTypeIsCompatible && !$assignedExprType instanceof MixedType) { foreach (TypeUtils::flattenTypes($assignedExprType->toCoercedArgumentType(true)) as $type) { $accepts = $propertyNativeType->accepts($type, true); - if ($accepts->yes()) { + if (!$accepts->no()) { $assignedTypeIsCompatible = true; continue; }
$assignedTypeIsCompatible = true;
continue;
}
$assignedTypeIsCompatible = false;
break;
}
}

if (
!$nativeProperty->getNativeType()->accepts($assignedExprType, true)->yes()
!$assignedTypeIsCompatible
) {
$throwPoints[] = InternalThrowPoint::createExplicit($scope, new ObjectType(TypeError::class), $assignedExpr, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,32 @@ public function testPropertyHooks(): void
]);
}

public function testBug9146(): void
{
$this->analyse([__DIR__ . '/data/bug-9146.php'], [
[
'Dead catch - TypeError is never thrown in the try block.',
52,
],
[
'Dead catch - TypeError is never thrown in the try block.',
80,
],
]);
}

public function testBug9146NonStrict(): void
{
$this->analyse([__DIR__ . '/data/bug-9146-non-strict.php'], [
[
'Dead catch - TypeError is never thrown in the try block.',
52,
],
[
'Dead catch - TypeError is never thrown in the try block.',
80,
],
]);
}

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

namespace Bug9146NonStrict;

final class HelloWorld
{
public int $number;
public function __construct(mixed $number)
{
try {
$this->number = $number;
} catch (\TypeError $e) {
throw new \UnexpectedValueException();
}
}
}

final class HelloWorld2
{
public string $name;
public function setName(mixed $value): void
{
try {
$this->name = $value;
} catch (\TypeError $e) {
throw new \InvalidArgumentException('Expected string');
}
}
}

final class HelloWorld3
{
public float $amount;
public function setAmount(mixed $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}

// Dead catch: int assigned to ?float, PHP coerces int to float without TypeError
final class FloatNullCoercion
{
public ?float $amount;
public function setAmount(int $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) { // error: Dead catch - TypeError is never thrown in the try block.
echo "caught";
}
}
}

// Not dead: int|string assigned to int, string part could throw TypeError
final class PartialTypeMatch
{
public int $number;
/** @param int|string $value */
public function setNumber($value): void
{
try {
$this->number = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}

final class MixedWillNotThrow
{
public mixed $name;
public function setName(mixed $value): void
{
try {
$this->name = $value;
} catch (\TypeError $e) {
throw new \InvalidArgumentException('Expected string');
}
}
}

final class IntJustWarnsOnFloat
{
public int $amount;
public function setAmount(float $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}
97 changes: 97 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-9146.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types = 1);

namespace Bug9146;

final class HelloWorld
{
public int $number;
public function __construct(mixed $number)
{
try {
$this->number = $number;
} catch (\TypeError $e) {
throw new \UnexpectedValueException();
}
}
}

final class HelloWorld2
{
public string $name;
public function setName(mixed $value): void
{
try {
$this->name = $value;
} catch (\TypeError $e) {
throw new \InvalidArgumentException('Expected string');
}
}
}

final class HelloWorld3
{
public float $amount;
public function setAmount(mixed $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}

// Dead catch: int assigned to ?float, PHP coerces int to float without TypeError
final class FloatNullCoercion
{
public ?float $amount;
public function setAmount(int $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) { // error: Dead catch - TypeError is never thrown in the try block.
echo "caught";
}
}
}

// Not dead: int|string assigned to int, string part could throw TypeError
final class PartialTypeMatch
{
public int $number;
/** @param int|string $value */
public function setNumber($value): void
{
try {
$this->number = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}

final class MixedWillNotThrow
{
public mixed $name;
public function setName(mixed $value): void
{
try {
$this->name = $value;
} catch (\TypeError $e) {
throw new \InvalidArgumentException('Expected string');
}
}
}

final class IntDoesNotAcceptFloat
{
public int $amount;
public function setAmount(float $value): void
{
try {
$this->amount = $value;
} catch (\TypeError $e) {
echo "caught";
}
}
}
Loading