Skip to content

Commit 0de69d8

Browse files
authored
Add Expr Throw - PHP 8 (#146)
* Add Expr Throw - PHP 8 * check:lint only with PHP 8.0 * fix phpstan check - instanceof PHPStan\Reflection\MethodReflection
1 parent 52a9223 commit 0de69d8

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

.github/workflows/tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343

4444
- name: "Check lint"
4545
run: "composer run-script check:lint"
46+
if: ${{ matrix.php-version == '8.0' }}
4647

4748
- name: "Check CodeStyle"
4849
run: "composer run-script check:cs"

src/Rules/ThrowsPhpDocRule.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public function processNode(Node $node, Scope $scope): array
168168
$method = $scope->getFunction();
169169
$isMethodWhitelisted = $method instanceof MethodReflection && $this->isWhitelistedMethod($method);
170170
if ($node instanceof MethodReturnStatementsNode) {
171-
if ($isMethodWhitelisted && $method instanceof MethodReflection) {
171+
if ($isMethodWhitelisted) {
172172
return $this->processWhitelistedMethod($method, $node->getStartLine());
173173
}
174174

@@ -195,6 +195,10 @@ public function processNode(Node $node, Scope $scope): array
195195
return $this->processThrow($node, $scope);
196196
}
197197

198+
if ($node instanceof Expr\Throw_) {
199+
return $this->processExprThrow($node, $scope);
200+
}
201+
198202
if ($node instanceof MethodCall) {
199203
return $this->processMethodCall($node, $scope);
200204
}
@@ -315,6 +319,16 @@ private function processThrow(Throw_ $node, Scope $scope): array
315319
return $this->processThrowsTypes($exceptionType);
316320
}
317321

322+
/**
323+
* @return RuleError[]
324+
*/
325+
private function processExprThrow(Expr\Throw_ $node, Scope $scope): array
326+
{
327+
$exceptionType = $scope->getType($node->expr);
328+
329+
return $this->processThrowsTypes($exceptionType);
330+
}
331+
318332
/**
319333
* @return RuleError[]
320334
*/

tests/src/Rules/ThrowsPhpDocRuleTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ public function testBasicThrows(): void
106106
$this->analyseFile(__DIR__ . '/data/throws-annotations.php');
107107
}
108108

109+
/**
110+
* @requires PHP 8.0
111+
*/
112+
public function testExpressionThrows(): void
113+
{
114+
$this->analyseFile(__DIR__ . '/data/expression-throws.php');
115+
}
116+
109117
public function testTryCatch(): void
110118
{
111119
$this->analyseFile(__DIR__ . '/data/try-catch.php');
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pepakriz\PHPStanExceptionRules\Rules\ExpressionThrows;
4+
5+
use RuntimeException;
6+
7+
class FooException extends RuntimeException
8+
{
9+
public static function create(): self
10+
{
11+
return new self();
12+
}
13+
}
14+
15+
/**
16+
* @throws FooException
17+
*/
18+
function foo1() {
19+
$callable = fn() => throw new FooException();
20+
}
21+
22+
/**
23+
* @throws FooException
24+
*/
25+
function foo2() {
26+
return match (random_bytes(1)) {
27+
'a' => 'b',
28+
default => throw new FooException(),
29+
};
30+
}
31+
32+
/**
33+
* @throws FooException
34+
*/
35+
function foo3() {
36+
$value = $nullableValue ?? throw new FooException();
37+
}
38+
39+
/**
40+
* @throws FooException
41+
*/
42+
function foo4() {
43+
$value = $falsableValue ?: throw new FooException();
44+
}
45+
46+
47+
class FutureThrows
48+
{
49+
private ?string $name;
50+
51+
/**
52+
* @throws FooException
53+
*/
54+
public function ok1(): string
55+
{
56+
return $this->name ?? throw new FooException();
57+
}
58+
59+
/**
60+
* @throws FooException
61+
*/
62+
public function ok2(): string
63+
{
64+
return $this->name ?? $this->ok2();
65+
}
66+
67+
/**
68+
* @throws FooException
69+
*/
70+
public function ok3(): string
71+
{
72+
return $this->name ?? throw FooException::create();
73+
}
74+
75+
public function err(): string
76+
{
77+
return $this->name ?? throw new FooException(); // error: Missing @throws Pepakriz\PHPStanExceptionRules\Rules\ExpressionThrows\FooException annotation
78+
}
79+
}

0 commit comments

Comments
 (0)