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
4 changes: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1572,8 +1572,8 @@
'DOMElement::get_attribute_node' => ['DomAttribute', 'name'=>'string'],
'DOMElement::get_elements_by_tagname' => ['array', 'name'=>'string'],
'DOMElement::getAttribute' => ['string', 'name'=>'string'],
'DOMElement::getAttributeNode' => ['DOMAttr', 'name'=>'string'],
'DOMElement::getAttributeNodeNS' => ['DOMAttr', 'namespaceuri'=>'string', 'localname'=>'string'],
'DOMElement::getAttributeNode' => ['__benevolent<DOMAttr|false>', 'name'=>'string'],
'DOMElement::getAttributeNodeNS' => ['__benevolent<DOMAttr|null>', 'namespaceuri'=>'string', 'localname'=>'string'],
'DOMElement::getAttributeNS' => ['string', 'namespaceuri'=>'string', 'localname'=>'string'],
'DOMElement::getElementsByTagName' => ['DOMNodeList', 'name'=>'string'],
'DOMElement::getElementsByTagNameNS' => ['DOMNodeList', 'namespaceuri'=>'string', 'localname'=>'string'],
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/nsrt/DOMLegacyNamedNodeMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types = 1);

namespace DOMLegacyNamedNodeMap;

use DOMAttr;
use DOMElement;
use DOMNode;
use DOMNamedNodeMap;
use function PHPStan\Testing\assertType;

class Foo
{
public function basic_node(DOMNode $node): void {
if ($node->hasAttributes()) {
assertType('DOMNamedNodeMap<DOMAttr>', $node->attributes);
} else {
// Can be either null or an empty DOMNamedNodeMap
assertType('DOMNamedNodeMap<DOMAttr>|null', $node->attributes);
}
}

public function element_node(DOMElement $element): void
{
assertType('DOMNamedNodeMap<DOMAttr>', $element->attributes);
if ($element->hasAttribute('class')) {
$attribute = $element->getAttributeNode('class');
assertType('(DOMAttr|false)', $attribute); // Could be DOMAttr
assertType('string', $attribute->value);
} else {
$attribute = $element->getAttributeNode('class');
assertType('(DOMAttr|false)', $attribute); // Could be false
}
}

public function element_node_attribute_fetch_via_attributes_property(DOMElement $element): void
{
assertType('DOMNamedNodeMap<DOMAttr>', $element->attributes);
if ($element->hasAttribute('class')) {
$attribute = $element->attributes->getNamedItem('class');
if ($attribute === null) {
return;
}
assertType(DOMAttr::class, $attribute);
assertType('string', $attribute->value);
}
}
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,14 @@ public function testPrivatePropertyWithAllowedPropertyTagIsPublic(): void
$this->analyse([__DIR__ . '/data/private-property-with-allowed-property-tag-is-public.php'], []);
}

public function testDomExtensionLegacyTemplateNodes(): void
{
$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/data/dom-legacy-ext-template-nodes.php'], []);
}

public function testBug13537(): void
{
$this->checkThisOnly = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DOMNodeStubsAccessProperties;

function basic_node(\DOMNode $node): void {
var_dump($node->attributes);
}

function element_node(\DOMElement $element): void
{
if ($element->hasAttribute('class')) {
$attribute = $element->getAttributeNode('class');
echo $attribute->value;
}
}

function element_node_attribute_fetch_via_attributes_property(\DOMElement $element): void
{
$attribute = $element->attributes->getNamedItem('class');
if ($attribute === null) {
return;
}
echo $attribute->value;
}

function element_node_attribute_fetch_via_getAttributeNode(\DOMElement $element): void
{
$attribute = $element->getAttributeNode('class');
if ($attribute === null) {
return;
}
echo $attribute->value;
}
Loading