Skip to content
Open
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
40 changes: 39 additions & 1 deletion src/PhpDoc/PhpDocInheritanceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,45 @@ public function resolvePhpDocForMethod(
return $this->resolveMethodPhpDocFromParentClass($traitMethod, $resolvedPhpDocBlock, $declaringClass, $trait, $currentResolvedPhpDoc, $currentPositionalParameterNames);
}

return $currentResolvedPhpDoc;
$result = $currentResolvedPhpDoc;

// Inherit @throws from non-abstract trait methods
if ($result === null || $result->getThrowsTag() === null) {
foreach ($declaringClass->getTraits() as $trait) {
if (!$trait->hasNativeMethod($methodName)) {
continue;
}

$traitMethod = $trait->getNativeMethod($methodName);
if ($traitMethod->getDocComment() === null) {
continue;
}
if ($declaringClass->getFileName() === null) {
continue;
}

$traitResolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$declaringClass->getFileName(),
$declaringClass->getName(),
$trait->getName(),
$methodName,
$traitMethod->getDocComment(),
);

$throwsTag = $traitResolvedPhpDoc->getThrowsTag();
if ($throwsTag === null) {
continue;
}

if ($result === null) {
$result = ResolvedPhpDocBlock::createEmpty();
}
$result = $result->withThrowsTag($throwsTag);
break;
}
}

return $result;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/PhpDoc/ResolvedPhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,14 @@ public function getThrowsTag(): ?ThrowsTag
return $this->throwsTag;
}

public function withThrowsTag(ThrowsTag $throwsTag): self
{
$result = clone $this;
$result->throwsTag = $throwsTag;

return $result;
}

/**
* @return array<MixinTag>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function testRule(): void
]);
}

public function testBug10315(): void
{
$this->analyse([__DIR__ . '/data/bug-10315.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return [
Expand Down
62 changes: 62 additions & 0 deletions tests/PHPStan/Rules/Exceptions/data/bug-10315.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug10315;

class MyException extends \RuntimeException
{
}

trait DriverPoolAbstractTrait
{
/**
* @throws MyException
*/
protected function driverReadMultiple(): array
{
throw new MyException();
}
}

trait CacheItemPoolTrait
{
public function getItems(): array
{
try {
$result = $this->driverReadMultiple();
} catch (MyException) {
$result = [];
}

return $result;
}
}

// Scenario A: base uses DriverPoolAbstractTrait, child uses CacheItemPoolTrait and overrides
abstract class AbstractPoolA
{
use DriverPoolAbstractTrait;
}

class RedisDriverA extends AbstractPoolA
{
use CacheItemPoolTrait;

protected function driverReadMultiple(): array
{
return ['key' => 'value'];
}
}

// Scenario B: class uses both traits and overrides the method directly
class DirectDriverB
{
use DriverPoolAbstractTrait;
use CacheItemPoolTrait;

protected function driverReadMultiple(): array
{
return ['key' => 'value'];
}
}
Loading