-
Notifications
You must be signed in to change notification settings - Fork 554
Narrow DOMDocument::createElement() return type and throw type for valid constant names #5192
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
Open
janedbal
wants to merge
3
commits into
phpstan:2.1.x
Choose a base branch
from
janedbal:dom-document-create-element-return-type
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/Type/Php/DomDocumentCreateElementDynamicReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
janedbal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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
57
src/Type/Php/DomDocumentCreateElementDynamicThrowTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
48
tests/PHPStan/Analyser/nsrt/dom-document-create-element.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
janedbal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
|
|
||
| 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)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,10 @@ public function testRule(): void | |
| 122, | ||
| ]; | ||
| } | ||
| $errors[] = [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.