Skip to content
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 @@ -62,6 +62,11 @@ services:
tags:
- exceptionRules.dynamicConstructorThrowTypeExtension

-
class: Pepakriz\PHPStanExceptionRules\Extension\DOMDocumentExtension
tags:
- exceptionRules.dynamicMethodThrowTypeExtension

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

namespace Pepakriz\PHPStanExceptionRules\Extension;

use DOMDocument;
use ErrorException;
use Pepakriz\PHPStanExceptionRules\DynamicMethodThrowTypeExtension;
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
use Pepakriz\PHPStanExceptionRules\UnsupportedFunctionException;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VoidType;
use function is_a;

class DOMDocumentExtension implements DynamicMethodThrowTypeExtension
{

/**
* @throws UnsupportedClassException
* @throws UnsupportedFunctionException
*/
public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
if (!is_a($methodReflection->getDeclaringClass()->getName(), DOMDocument::class, true)) {
throw new UnsupportedClassException();
}

if ($methodReflection->getName() === 'load' || $methodReflection->getName() === 'loadHTMLFile') {
return new ObjectType(ErrorException::class);
}

if ($methodReflection->getName() === 'loadXML' || $methodReflection->getName() === 'loadHTML') {
return $this->resolveLoadSourceType($methodCall, $scope);
}

throw new UnsupportedFunctionException();
}

private function resolveLoadSourceType(MethodCall $methodCall, Scope $scope): Type
{
$valueType = $scope->getType($methodCall->args[0]->value);
$exceptionType = new ObjectType(ErrorException::class);

foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
if ($constantString->getValue() === '') {
return $exceptionType;
}

$valueType = TypeCombinator::remove($valueType, $constantString);
}

if (!$valueType instanceof NeverType) {
return $exceptionType;
}

return new VoidType();
}

}
7 changes: 6 additions & 1 deletion tests/src/Rules/PhpInternalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Pepakriz\PHPStanExceptionRules\DefaultThrowTypeService;
use Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService;
use Pepakriz\PHPStanExceptionRules\Extension\DateTimeExtension;
use Pepakriz\PHPStanExceptionRules\Extension\DOMDocumentExtension;
use Pepakriz\PHPStanExceptionRules\Extension\IntdivExtension;
use Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension;
use Pepakriz\PHPStanExceptionRules\Extension\ReflectionExtension;
Expand All @@ -27,14 +28,18 @@ protected function getRule(): Rule
$splFileObjectExtension = new SplFileObjectExtension();
$jsonEncodeDecodeExtension = new JsonEncodeDecodeExtension();
$intdivExtension = new IntdivExtension();
$domDocumentExtension = new DOMDocumentExtension();

return new ThrowsPhpDocRule(
new CheckedExceptionService(
[
Throwable::class,
]
),
new DynamicThrowTypeService(
[],
[
$domDocumentExtension,
],
[],
[
$reflectionClassExtension,
Expand Down
18 changes: 18 additions & 0 deletions tests/src/Rules/data/throws-php-internal-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeImmutable;
use DOMDocument;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
Expand Down Expand Up @@ -130,5 +131,22 @@ public function testIntdiv(): void
intdiv(rand(0, 1) === 0 ? 20 : 10, rand(0, 1) === 0 ? 5 : 10);
}

public function testDOMDocumentObject(): void
{
$domDocument = new DOMDocument();

$domDocument->load(''); // error: Missing @throws ErrorException annotation
$domDocument->load('non empty string'); // error: Missing @throws ErrorException annotation

$domDocument->loadXML(''); // error: Missing @throws ErrorException annotation
$domDocument->loadXML('non empty string');

$domDocument->loadHTML(''); // error: Missing @throws ErrorException annotation
$domDocument->loadHTML('non empty string');

$domDocument->loadHTMLFile(''); // error: Missing @throws ErrorException annotation
$domDocument->loadHTMLFile('non empty string'); // error: Missing @throws ErrorException annotation
}

}