Skip to content

Strict comparison using === between DOMElement and false #12278

@dimadeush

Description

@dimadeush

Bug report

Hello

First of all - Thanks for the great open source tool!

We have some issues with the latest phpstan

$cpd = $this->document->createElement(self::CPD_ELEMENT_NAME);

 if ($cpd === false) {
     $this->generateException("Can not create element: %s", self::CPD_ELEMENT_NAME);
 }

$result = $this->document->appendChild($cpd);

Error 1: "phpstan: Strict comparison using === between DOMElement and false will always evaluate to false."

Error 2: "phpstan: Parameter #1 $node of method DOMNode::appendChild() expects DOMNode, DOMElement|false given."

dom_c.php:

/**
     * Create new element node
     * @link https://php.net/manual/en/domdocument.createelement.php
     * @param string $localName <p>
     * The tag name of the element.
     * </p>
     * @param string $value [optional] <p>
     * The value of the element. By default, an empty element will be created.
     * You can also set the value later with DOMElement->nodeValue.
     * </p>
     * @return DOMElement|false A new instance of class DOMElement or false
     * if an error occurred.
     * @throws DOMException If invalid $localName
     */
    public function createElement(
        #[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $localName,
        #[LanguageLevelTypeAware(['8.0' => 'string'], default: '')] $value = ''
    ) {}

Code snippet that reproduces the problem

test1
test2

Expected output

no error

Did PHPStan help you today? Did it make you happy in any way?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions