Skip to content
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

Add generic definition of DOMNamedNodeMap #2703

Merged
merged 2 commits into from Jan 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Psalm/Internal/Codebase/Scanner.php
Expand Up @@ -280,14 +280,16 @@ public function getClassLikeFilePath($fq_classlike_name_lc)
* @param string|null $referencing_file_path
* @param bool $analyze_too
* @param bool $store_failure
* @param array<string, mixed> $phantom_classes
*
* @return void
*/
public function queueClassLikeForScanning(
$fq_classlike_name,
$referencing_file_path = null,
$analyze_too = false,
$store_failure = true
$store_failure = true,
array $phantom_classes = []
) {
if ($fq_classlike_name[0] === '\\') {
$fq_classlike_name = substr($fq_classlike_name, 1);
Expand Down Expand Up @@ -325,7 +327,7 @@ public function queueClassLikeForScanning(
$property_type->queueClassLikesForScanning(
$this->codebase,
null,
[$fq_classlike_name_lc => true]
$phantom_classes + [$fq_classlike_name_lc => true]
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/PropertyMap.php
Expand Up @@ -268,7 +268,7 @@
'domelement' => [
'schemaTypeInfo' => 'bool',
'tagName' => 'string',
'attributes' => 'DOMNamedNodeMap',
'attributes' => 'DOMNamedNodeMap<DOMAttr>',
],
'tidynode' => [
'value' => 'string',
Expand Down
26 changes: 26 additions & 0 deletions src/Psalm/Internal/Stubs/CoreGenericClasses.php
Expand Up @@ -946,6 +946,32 @@ class DOMNodeList implements Traversable, Countable {
public function item($index) {}
}

/**
* @template-covariant TNode as DOMNode
* @template-implements Traversable<string, TNode>
*/
class DOMNamedNodeMap implements Traversable, Countable {
/**
* @var int
*/
public $length;

/**
* @return TNode|null
*/
public function getNamedItem(string $name): ?DOMNode {}

/**
* @return TNode|null
*/
public function getNamedItemNS(string $namespaceURI, string $localName): ?DOMNode {}

/**
* @return TNode|null
*/
public function item(int $index): ?DOMNode {}
}

/**
* The SplDoublyLinkedList class provides the main functionalities of a doubly linked list.
* @link https://php.net/manual/en/class.spldoublylinkedlist.php
Expand Down
12 changes: 8 additions & 4 deletions src/Psalm/Type/Atomic.php
Expand Up @@ -458,7 +458,8 @@ public function queueClassLikesForScanning(
$this->value,
$file_storage ? $file_storage->file_path : null,
false,
!$this->from_docblock
!$this->from_docblock,
$phantom_classes
);

if ($file_storage) {
Expand Down Expand Up @@ -487,7 +488,8 @@ public function queueClassLikesForScanning(
$this->fq_classlike_name,
$file_storage ? $file_storage->file_path : null,
false,
!$this->from_docblock
!$this->from_docblock,
$phantom_classes
);
if ($file_storage) {
$file_storage->referenced_classlikes[strtolower($this->fq_classlike_name)] = $this->fq_classlike_name;
Expand All @@ -500,7 +502,8 @@ public function queueClassLikesForScanning(
$this->as,
$file_storage ? $file_storage->file_path : null,
false,
!$this->from_docblock
!$this->from_docblock,
$phantom_classes
);
if ($file_storage) {
$file_storage->referenced_classlikes[strtolower($this->as)] = $this->as;
Expand All @@ -521,7 +524,8 @@ public function queueClassLikesForScanning(
$this->value,
$file_storage ? $file_storage->file_path : null,
false,
!$this->from_docblock
!$this->from_docblock,
$phantom_classes
);
if ($file_storage) {
$file_storage->referenced_classlikes[strtolower($this->value)] = $this->value;
Expand Down
6 changes: 6 additions & 0 deletions tests/PropertyTypeTest.php
Expand Up @@ -344,6 +344,12 @@ function foo(DOMElement $e) : void {
echo $e->attributes->length;
}',
],
'genericTypeFromPropertyMap' => [
'<?php
function foo(DOMElement $e) : ?DOMAttr {
return $e->attributes->item(0);
}'
],
'goodArrayProperties' => [
'<?php
interface I1 {}
Expand Down