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
14 changes: 14 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
use PHPStan\Type\VoidType;
use Throwable;
use Traversable;
use TypeError;
use function array_fill_keys;
use function array_filter;
use function array_key_exists;
Expand Down Expand Up @@ -3314,11 +3315,24 @@ private function processAssignVar(
if ($propertyReflection->canChangeTypeAfterAssignment()) {
$scope = $scope->assignExpression($var, $assignedExprType);
}
if (!$propertyReflection->getWritableType()->isSuperTypeOf($assignedExprType)->yes()) {
$throwPoints[] = ThrowPoint::createExplicit($scope, new ObjectType(TypeError::class), $assignedExpr, false);
}
} else {
// fallback
$assignedExprType = $scope->getType($assignedExpr);
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
$scope = $scope->assignExpression($var, $assignedExprType);
// simulate dynamic property assign by __set to get throw points
if (!$propertyHolderType->hasMethod('__set')->no()) {
$throwPoints = array_merge($throwPoints, $this->processExprNode(
new MethodCall($var->var, '__set'),
$scope,
static function (): void {
},
$context,
)->getThrowPoints());
}
}

} elseif ($var instanceof Expr\StaticPropertyFetch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,38 @@ public function testBug6262(): void
$this->analyse([__DIR__ . '/data/bug-6262.php'], []);
}

public function testBug6256(): void
{
if (PHP_VERSION_ID < 70400) {
self::markTestSkipped('Test requires PHP 7.4.');
}

$this->analyse([__DIR__ . '/data/bug-6256.php'], [
[
'Dead catch - TypeError is never thrown in the try block.',
25,
],
[
'Dead catch - TypeError is never thrown in the try block.',
31,
],
[
'Dead catch - TypeError is never thrown in the try block.',
45,
],
[
'Dead catch - Exception is never thrown in the try block.',
57,
],
[
'Dead catch - Throwable is never thrown in the try block.',
63,
],
[
'Dead catch - Exception is never thrown in the try block.',
100,
],
]);
}

}
115 changes: 115 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-6256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php // lint >= 7.4

namespace Bug6256;

use Exception;

final class A
{
public int $integerType = 1;
public $mixedType;
public string $stringType;
/** @var string|int */
public $stringOrIntType;

function doFoo()
{
try {
$this->integerType = "string";
} catch (\TypeError $e) {
// not dead
}

try {
$this->mixedType = "string";
} catch (\TypeError $e) {
// dead
}

try {
$this->stringType = "string";
} catch (\TypeError $e) {
// dead
}

/** @var string|int $intOrString */
$intOrString = '';
try {
$this->integerType = $intOrString;
} catch (\TypeError $e) {
// not dead
}

try {
$this->stringOrIntType = 1;
} catch (\TypeError $e) {
// dead
}

try {
$this->integerType = "string";
} catch (\Error $e) {
// not dead
}

try {
$this->integerType = "string";
} catch (\Exception $e) {
// dead
}

try {
$this->dynamicProperty = 1;
} catch (\Throwable $e) {
// dead
}
}
}

final class B {

/**
* @throws Exception
*/
public function __set(string $name, $value)
{
throw new Exception();
}

function doFoo()
{
try {
$this->dynamicProperty = "string";
} catch (\Exception $e) {
// not dead
}
}
}

final class C {

/**
* @throws void
*/
public function __set(string $name, $value) {}

function doFoo()
{
try {
$this->dynamicProperty = "string";
} catch (\Exception $e) {
// dead
}
}
}

class D {
function doFoo()
{
try {
$this->dynamicProperty = "string";
} catch (\Exception $e) {
// not dead because class is not final
}
}
}