Skip to content

Commit 7073711

Browse files
authored
ThrowsPhpDocRule: rule should not change AST (fixed #113) (#114)
1 parent eff22ec commit 7073711

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

src/Rules/ThrowsPhpDocRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,11 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array
465465
return [];
466466
}
467467

468-
$classReflection = $scope->getClassReflection();
468+
$classReflection = $scope->getTraitReflection();
469+
if ($classReflection === null) {
470+
$classReflection = $scope->getClassReflection();
471+
}
472+
469473
if ($classReflection === null) {
470474
try {
471475
$methodReflection = $this->broker->getFunction(new Name($node->name->toString()), $scope);
@@ -489,7 +493,7 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array
489493
if (!$node->hasAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END)) {
490494
$node->setAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END, true);
491495
if ($node->stmts === null) {
492-
$node->stmts = [];
496+
throw new ShouldNotHappenException();
493497
}
494498
$node->stmts[] = new FunctionEnd($node);
495499
}

tests/src/Rules/Bug113Test.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pepakriz\PHPStanExceptionRules\Rules;
4+
5+
use Pepakriz\PHPStanExceptionRules\CheckedExceptionService;
6+
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeExtension;
7+
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeService;
8+
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
9+
use Pepakriz\PHPStanExceptionRules\Rules\Data\CheckedException;
10+
use Pepakriz\PHPStanExceptionRules\Rules\DynamicExtension\DynamicExtension;
11+
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
12+
use PHPStan\Rules\Rule;
13+
use ReflectionException;
14+
use RuntimeException;
15+
16+
/**
17+
* @extends RuleTestCase<ThrowsPhpDocRule>
18+
*/
19+
class Bug113Test extends RuleTestCase
20+
{
21+
22+
protected function getRule(): Rule
23+
{
24+
$defaultThrowTypeService = new DefaultThrowTypeService([], []);
25+
26+
$extensions = [
27+
new DynamicExtension(),
28+
new DefaultThrowTypeExtension($defaultThrowTypeService),
29+
];
30+
31+
return new ThrowsPhpDocRule(
32+
new CheckedExceptionService(
33+
[
34+
RuntimeException::class,
35+
CheckedException::class,
36+
ReflectionException::class,
37+
]
38+
),
39+
new DynamicThrowTypeService(
40+
$extensions,
41+
$extensions,
42+
$extensions,
43+
$extensions
44+
),
45+
$defaultThrowTypeService,
46+
$this->createThrowsAnnotationReader(),
47+
$this->createBroker(),
48+
false,
49+
true,
50+
false,
51+
false,
52+
[]
53+
);
54+
}
55+
56+
public function testBasicThrows(): void
57+
{
58+
$this->analyse(__DIR__ . '/data/bug113.php');
59+
}
60+
61+
}

tests/src/Rules/data/bug113.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pepakriz\PHPStanExceptionRules\Rules\Bug113;
4+
5+
trait FooTrait
6+
{
7+
8+
abstract public function getString(): string;
9+
10+
}
11+
12+
class Foo
13+
{
14+
15+
use FooTrait;
16+
17+
public function getString(): string
18+
{
19+
return 'Foo';
20+
}
21+
22+
}

0 commit comments

Comments
 (0)