Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use DOMDocument;
use DOMException;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function extension_loaded;

#[AutowiredService]
final class DomDocumentCreateElementDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return DOMDocument::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return extension_loaded('dom') && $methodReflection->getName() === 'createElement';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether this kind of optional runtime dependency is a good thing to have.
would it make sense to hard-code a list of valid elements instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was basically copied from SimpleXMLElementXpathMethodReturnTypeExtension, so I just followed that one.

}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$args = $methodCall->getArgs();
if (!isset($args[0])) {
return null;
}

$argType = $scope->getType($args[0]->value);

$doc = new DOMDocument();

foreach ($argType->getConstantStrings() as $constantString) {
try {
$doc->createElement($constantString->getValue());
} catch (DOMException) {
return null;
}

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

if (!$argType instanceof NeverType) {
return null;
}

$variant = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants());

return TypeCombinator::remove($variant->getReturnType(), new ConstantBooleanType(false));
}

}
57 changes: 57 additions & 0 deletions src/Type/Php/DomDocumentCreateElementDynamicThrowTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use DOMDocument;
use DOMException;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodThrowTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function extension_loaded;

#[AutowiredService]
final class DomDocumentCreateElementDynamicThrowTypeExtension implements DynamicMethodThrowTypeExtension
{

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return extension_loaded('dom')
&& $methodReflection->getDeclaringClass()->getName() === DOMDocument::class
&& $methodReflection->getName() === 'createElement';
}

public function getThrowTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
$args = $methodCall->getArgs();
if (!isset($args[0])) {
return new ObjectType(DOMException::class);
}

$argType = $scope->getType($args[0]->value);

$doc = new DOMDocument();

foreach ($argType->getConstantStrings() as $constantString) {
try {
$doc->createElement($constantString->getValue());
} catch (DOMException) {
return new ObjectType(DOMException::class);
}

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

if (!$argType instanceof NeverType) {
return new ObjectType(DOMException::class);
}

return null;
}

}
48 changes: 48 additions & 0 deletions tests/PHPStan/Analyser/nsrt/dom-document-create-element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace DomDocumentCreateElement;

use DOMDocument;
use function PHPStan\Testing\assertType;

class Foo
{

public function dynamicName(DOMDocument $doc, string $name): void
{
assertType('(DOMElement|false)', $doc->createElement($name));
}

public function validConstantNames(DOMDocument $doc): void
{
assertType('DOMElement', $doc->createElement('div'));
assertType('DOMElement', $doc->createElement('my-element'));
assertType('DOMElement', $doc->createElement('ns:tag'));
assertType('DOMElement', $doc->createElement('_private'));
assertType('DOMElement', $doc->createElement('h1'));
}

public function invalidConstantNames(DOMDocument $doc): void
{
assertType('(DOMElement|false)', $doc->createElement(''));
assertType('(DOMElement|false)', $doc->createElement('123element'));
assertType('(DOMElement|false)', $doc->createElement('my element'));
}

/**
* @param 'div'|'span' $validUnion
* @param 'div'|'' $mixedUnion
*/
public function unions(DOMDocument $doc, string $validUnion, string $mixedUnion): void
{
assertType('DOMElement', $doc->createElement($validUnion));
assertType('(DOMElement|false)', $doc->createElement($mixedUnion));
}

public function localVariable(DOMDocument $doc): void
{
$name = 'paragraph';
assertType('DOMElement', $doc->createElement($name));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public function testRule(): void
122,
];
}
$errors[] = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a dedicated test instead ?

'Method MissingExceptionMethodThrows\Foo::domCreateElementDoesThrow() throws checked exception DOMException but it\'s missing from the PHPDoc @throws tag.',
133,
];
$this->analyse([__DIR__ . '/data/missing-exception-method-throws.php'], $errors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,15 @@ public function dateTimeModifyDoesThrows(\DateTime $dt, \DateTimeImmutable $dti,
$dti->modify($m);
}

public function domCreateElementDoesNotThrow(\DOMDocument $doc): void
{
$doc->createElement('div');
$doc->createElement('my-element');
}

public function domCreateElementDoesThrow(\DOMDocument $doc, string $name): void
{
$doc->createElement($name);
}

}
Loading