Skip to content

Extension for SplFileObject (fixed #106) #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2019
Merged
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
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ services:
tags:
- exceptionRules.dynamicConstructorThrowTypeExtension

-
class: Pepakriz\PHPStanExceptionRules\Extension\SplFileObjectExtension
tags:
- exceptionRules.dynamicConstructorThrowTypeExtension

-
class: Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension
tags:
Expand Down
36 changes: 36 additions & 0 deletions src/Extension/SplFileObjectExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Pepakriz\PHPStanExceptionRules\Extension;

use LogicException;
use Pepakriz\PHPStanExceptionRules\DynamicConstructorThrowTypeExtension;
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
use PhpParser\Node\Expr\New_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use RuntimeException;
use SplFileObject;
use function is_a;

class SplFileObjectExtension implements DynamicConstructorThrowTypeExtension
{

/**
* @throws UnsupportedClassException
*/
public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
{
if (is_a($methodReflection->getDeclaringClass()->getName(), SplFileObject::class, true)) {
return new UnionType([
new ObjectType(RuntimeException::class),
new ObjectType(LogicException::class),
]);
}

throw new UnsupportedClassException();
}

}
3 changes: 3 additions & 0 deletions tests/src/Rules/PhpInternalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Pepakriz\PHPStanExceptionRules\Extension\IntdivExtension;
use Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension;
use Pepakriz\PHPStanExceptionRules\Extension\ReflectionExtension;
use Pepakriz\PHPStanExceptionRules\Extension\SplFileObjectExtension;
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
use PHPStan\Rules\Rule;
use Throwable;
Expand All @@ -23,6 +24,7 @@ protected function getRule(): Rule
{
$reflectionClassExtension = new ReflectionExtension($this->createBroker());
$dateTimeExtension = new DateTimeExtension();
$splFileObjectExtension = new SplFileObjectExtension();
$jsonEncodeDecodeExtension = new JsonEncodeDecodeExtension();
$intdivExtension = new IntdivExtension();
return new ThrowsPhpDocRule(
Expand All @@ -37,6 +39,7 @@ protected function getRule(): Rule
[
$reflectionClassExtension,
$dateTimeExtension,
$splFileObjectExtension,
],
[
$jsonEncodeDecodeExtension,
Expand Down
6 changes: 6 additions & 0 deletions tests/src/Rules/data/throws-php-internal-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ReflectionObject;
use ReflectionProperty;
use ReflectionZendExtension;
use SplFileObject;
use stdClass;
use Throwable;
use function rand;
Expand Down Expand Up @@ -103,6 +104,11 @@ public function testDateTime(): void
new DateTime(rand(0, 1) === 0 ? '2018-01-01' : 123); // error: Missing @throws Exception annotation
}

public function testSplFileObject(): void
{
new SplFileObject(); // error: Missing @throws LogicException annotation; Missing @throws RuntimeException annotation
}

public function testIntdiv(): void
{
/** @var int $integer */
Expand Down