Skip to content

Commit eff22ec

Browse files
authored
Extension for SplFileObject (fixed #106) (#112)
1 parent 49a29c7 commit eff22ec

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ services:
5959
tags:
6060
- exceptionRules.dynamicConstructorThrowTypeExtension
6161

62+
-
63+
class: Pepakriz\PHPStanExceptionRules\Extension\SplFileObjectExtension
64+
tags:
65+
- exceptionRules.dynamicConstructorThrowTypeExtension
66+
6267
-
6368
class: Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension
6469
tags:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pepakriz\PHPStanExceptionRules\Extension;
4+
5+
use LogicException;
6+
use Pepakriz\PHPStanExceptionRules\DynamicConstructorThrowTypeExtension;
7+
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
8+
use PhpParser\Node\Expr\New_;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Reflection\MethodReflection;
11+
use PHPStan\Type\ObjectType;
12+
use PHPStan\Type\Type;
13+
use PHPStan\Type\UnionType;
14+
use RuntimeException;
15+
use SplFileObject;
16+
use function is_a;
17+
18+
class SplFileObjectExtension implements DynamicConstructorThrowTypeExtension
19+
{
20+
21+
/**
22+
* @throws UnsupportedClassException
23+
*/
24+
public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
25+
{
26+
if (is_a($methodReflection->getDeclaringClass()->getName(), SplFileObject::class, true)) {
27+
return new UnionType([
28+
new ObjectType(RuntimeException::class),
29+
new ObjectType(LogicException::class),
30+
]);
31+
}
32+
33+
throw new UnsupportedClassException();
34+
}
35+
36+
}

tests/src/Rules/PhpInternalsTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Pepakriz\PHPStanExceptionRules\Extension\IntdivExtension;
1010
use Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension;
1111
use Pepakriz\PHPStanExceptionRules\Extension\ReflectionExtension;
12+
use Pepakriz\PHPStanExceptionRules\Extension\SplFileObjectExtension;
1213
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
1314
use PHPStan\Rules\Rule;
1415
use Throwable;
@@ -23,6 +24,7 @@ protected function getRule(): Rule
2324
{
2425
$reflectionClassExtension = new ReflectionExtension($this->createBroker());
2526
$dateTimeExtension = new DateTimeExtension();
27+
$splFileObjectExtension = new SplFileObjectExtension();
2628
$jsonEncodeDecodeExtension = new JsonEncodeDecodeExtension();
2729
$intdivExtension = new IntdivExtension();
2830
return new ThrowsPhpDocRule(
@@ -37,6 +39,7 @@ protected function getRule(): Rule
3739
[
3840
$reflectionClassExtension,
3941
$dateTimeExtension,
42+
$splFileObjectExtension,
4043
],
4144
[
4245
$jsonEncodeDecodeExtension,

tests/src/Rules/data/throws-php-internal-functions.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ReflectionObject;
1111
use ReflectionProperty;
1212
use ReflectionZendExtension;
13+
use SplFileObject;
1314
use stdClass;
1415
use Throwable;
1516
use function rand;
@@ -103,6 +104,11 @@ public function testDateTime(): void
103104
new DateTime(rand(0, 1) === 0 ? '2018-01-01' : 123); // error: Missing @throws Exception annotation
104105
}
105106

107+
public function testSplFileObject(): void
108+
{
109+
new SplFileObject(); // error: Missing @throws LogicException annotation; Missing @throws RuntimeException annotation
110+
}
111+
106112
public function testIntdiv(): void
107113
{
108114
/** @var int $integer */

0 commit comments

Comments
 (0)