Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion src/Reflection/Dummy/ChangedTypeMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
private array $variants,
private ?array $namedArgumentsVariants,
private ?Type $selfOutType,
private ?Type $throwType,
)
{
}
Expand Down Expand Up @@ -122,7 +123,7 @@ public function isBuiltin(): TrinaryLogic

public function getThrowType(): ?Type
{
return $this->reflection->getThrowType();
return $this->throwType;
}

public function hasSideEffects(): TrinaryLogic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,18 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentVariants = $namedArgumentVariants !== null
? array_map($variantFn, $namedArgumentVariants)
: null;
$throwType = $method->getThrowType();
$throwType = $throwType !== null
? $this->transformStaticType($throwType)
: null;

return new ChangedTypeMethodReflection(
$declaringClass,
$method,
$variants,
$namedArgumentVariants,
$selfOutType,
$throwType,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,18 @@ private function transformMethodWithStaticType(ClassReflection $declaringClass,
$namedArgumentsVariants = $namedArgumentsVariants !== null
? array_map($variantFn, $namedArgumentsVariants)
: null;
$throwType = $method->getThrowType();
$throwType = $throwType !== null
? $this->transformStaticType($throwType)
: null;

return new ChangedTypeMethodReflection(
$declaringClass,
$method,
$variants,
$namedArgumentsVariants,
$selfOutType,
$throwType,
);
}

Expand Down
41 changes: 41 additions & 0 deletions tests/PHPStan/Rules/Exceptions/Bug11900Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Exceptions;

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

/**
* @extends RuleTestCase<MissingCheckedExceptionInMethodThrowsRule>
*/
class Bug11900Test extends RuleTestCase
{

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

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

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

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Exceptions/bug-11900.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
exceptions:
implicitThrows: false
check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-11900.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php // lint >= 8.4

namespace Bug11900;

use Exception;
use Throwable;

abstract class ADataException extends Exception
{
/**
* @return void
* @throws static
*/
public function throw1(): void
{
throw $this;
}

/**
* @return void
* @throws static
*/
public static function throw2(): void
{
throw new static();
}
}

final class TestDataException extends ADataException
{
}

class TestPhpStan
{
/**
* @throws TestDataException
*/
public function validate(TestDataException $e): void
{
$e->throw1();
}

/**
* @throws TestDataException
*/
public function validate2(): void
{
TestDataException::throw2();
}
}
Loading