Skip to content

Add SimpleXMLElementExtension #143

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
Jan 27, 2021
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\SimpleXMLElementExtension
tags:
- exceptionRules.dynamicConstructorThrowTypeExtension

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

namespace Pepakriz\PHPStanExceptionRules\Extension;

use Exception;
use Pepakriz\PHPStanExceptionRules\DynamicConstructorThrowTypeExtension;
use Pepakriz\PHPStanExceptionRules\UnsupportedClassException;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\New_;
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 SimpleXMLElement;
use function is_a;

class SimpleXMLElementExtension implements DynamicConstructorThrowTypeExtension
{

/**
* @throws UnsupportedClassException
*/
public function getThrowTypeFromConstructor(MethodReflection $methodReflection, New_ $newNode, Scope $scope): Type
{
if (is_a($methodReflection->getDeclaringClass()->getName(), SimpleXMLElement::class, true)) {
return $this->resolveThrowType($newNode->args, $scope);
}

throw new UnsupportedClassException();
}

/**
* @param Arg[] $args
*/
private function resolveThrowType(array $args, Scope $scope): Type
{
$exceptionType = new ObjectType(Exception::class);
if (!isset($args[0])) {
return $exceptionType;
}

$valueType = $scope->getType($args[0]->value);
foreach (TypeUtils::getConstantStrings($valueType) as $constantString) {
try {
new SimpleXMLElement($constantString->getValue());
} catch (Exception $e) {
return $exceptionType;
}

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

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

return new VoidType();
}

}
3 changes: 3 additions & 0 deletions tests/src/Rules/PhpInternalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Pepakriz\PHPStanExceptionRules\Extension\IntdivExtension;
use Pepakriz\PHPStanExceptionRules\Extension\JsonEncodeDecodeExtension;
use Pepakriz\PHPStanExceptionRules\Extension\ReflectionExtension;
use Pepakriz\PHPStanExceptionRules\Extension\SimpleXMLElementExtension;
use Pepakriz\PHPStanExceptionRules\Extension\SplFileObjectExtension;
use Pepakriz\PHPStanExceptionRules\RuleTestCase;
use PHPStan\Rules\Rule;
Expand All @@ -31,6 +32,7 @@ protected function getRule(): Rule
$jsonEncodeDecodeExtension = new JsonEncodeDecodeExtension();
$intdivExtension = new IntdivExtension();
$domDocumentExtension = new DOMDocumentExtension();
$simpleXMLElementEtension = new SimpleXMLElementExtension();

return new ThrowsPhpDocRule(
new CheckedExceptionService(
Expand All @@ -48,6 +50,7 @@ protected function getRule(): Rule
$dateIntervalExtension,
$dateTimeExtension,
$splFileObjectExtension,
$simpleXMLElementEtension,
],
[
$jsonEncodeDecodeExtension,
Expand Down
8 changes: 8 additions & 0 deletions tests/src/Rules/data/throws-php-internal-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ReflectionObject;
use ReflectionProperty;
use ReflectionZendExtension;
use SimpleXMLElement;
use SplFileObject;
use stdClass;
use Throwable;
Expand Down Expand Up @@ -121,6 +122,13 @@ public function testSplFileObject(): void
new SplFileObject(); // error: Missing @throws LogicException annotation; Missing @throws RuntimeException annotation
}

public function testSimpleXMLElement(): void
{
new SimpleXMLElement(); // error: Missing @throws Exception annotation
new SimpleXMLElement('foo'); // error: Missing @throws Exception annotation
new SimpleXMLElement('<foo />');
}

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