Skip to content

Commit

Permalink
Add generic definition of DOMNamedNodeMap (#2703)
Browse files Browse the repository at this point in the history
* Fix #2638 - propagate phantom classes to recursive calls to Scanner::queueClassLikeForScanning()

* Add generic definition of DOMNamedNodeMap
  • Loading branch information
shiranai7 committed Jan 29, 2020
1 parent 10221f4 commit ecf85ae
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
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

0 comments on commit ecf85ae

Please sign in to comment.