Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Update tooling and types
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Mar 6, 2023
1 parent b013b71 commit 10047a8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
},
"require-dev": {
"phpunit/phpunit": "^8.0||^9.0",
"phpstan/phpstan": "^0.12.83",
"friendsofphp/php-cs-fixer": "^2.18"
"phpstan/phpstan": "^1.10",
"friendsofphp/php-cs-fixer": "^2.18",
"symfony/var-dumper": "^6.2"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 8 additions & 6 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Document extends \DOMDocument implements XPathAware

/**
* @param string $version
* @param mixed $encoding
* @param string $encoding
*/
public function __construct($version = '1.0', $encoding = null)
{
Expand Down Expand Up @@ -79,13 +79,13 @@ public function query($query, DOMNode $context = null): DOMNodeList
/**
* {@inheritdoc}
*/
public function queryOne($query, DOMNode $context = null)
public function queryOne($query, DOMNode $context = null): ?Element
{
return $this->xpath()->queryOne($query, $context);
}

/**
* {@inheritdoc}
* @return mixed
*/
public function evaluate($expression, DOMNode $context = null)
{
Expand All @@ -99,7 +99,7 @@ public function evaluate($expression, DOMNode $context = null)
public function dump(): string
{
$this->formatOutput = true;
$result = $this->saveXml();
$result = $this->saveXML();
$this->formatOutput = false;

if (false === $result) {
Expand All @@ -112,9 +112,11 @@ public function dump(): string
public function duplicate(): Document
{
$dom = new self();
$firstChild = $dom->importNode($this->firstChild, true);
if ($this->firstChild) {
$firstChild = $dom->importNode($this->firstChild, true);
$dom->appendChild($firstChild);
}

$dom->appendChild($firstChild);

return $dom;
}
Expand Down
20 changes: 5 additions & 15 deletions lib/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,15 @@ class Element extends \DOMElement implements XPathAware
{
/**
* Create and append a text-node with the given name and value.
*
* @param string $name
* @param string $value
*
* @return Element
*/
public function appendTextNode($name, $value)
public function appendTextNode(string $name, ?string $value): Element
{
$el = new self($name);
$element = $this->appendChild($el);
assert($element instanceof Element);

$element->appendChild(
$this->owner()->createTextNode($value)
$this->owner()->createTextNode($value ?? '')
);

return $element;
Expand All @@ -44,13 +39,8 @@ public function appendTextNode($name, $value)
* Create and append an element with the given name and optionally given value.
*
* Note: The value will not be escaped. Use DOMDocument::createTextNode() to create a text node with escaping support.
*
* @param string $name
* @param mixed $value
*
* @return Element
*/
public function appendElement($name, $value = null)
public function appendElement(string $name, ?string $value = null): Element
{
$element = $this->appendChild(new self($name, $value));
assert($element instanceof Element);
Expand All @@ -66,13 +56,13 @@ public function query($xpath, DOMNode $context = null): DOMNodeList
return $this->owner()->xpath()->query($xpath, $context ?: $this);
}

public function queryOne($xpath, DOMNode $context = null)
public function queryOne($xpath, DOMNode $context = null): ?Element
{
return $this->owner()->xpath()->queryOne($xpath, $context ?: $this);
}

/**
* {@inheritdoc}
* @return mixed
*/
public function evaluate($expression, DOMNode $context = null)
{
Expand Down
9 changes: 7 additions & 2 deletions lib/XPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ public function evaluate($expression, $contextnode = null, $registerNodeNS = tru
* @return DOMNodeList<DOMNode>
*/
#[\ReturnTypeWillChange]
public function query($expression, $contextnode = null, $registerNodeNS = true)
public function query($expression, $contextnode = null, $registerNodeNS = true): DOMNodeList
{
return $this->execute('query', 'query', $expression, $contextnode, $registerNodeNS);
$list = $this->execute('query', 'query', $expression, $contextnode, $registerNodeNS);
if (!$list instanceof DOMNodeList) {
throw new RuntimeException(sprintf('Expected XPAth expression to return DOMNodeList, got "%s"', is_object($list) ? get_class($list) : gettype($list)));
}

return $list;
}

public function queryOne(string $expr, DOMNode $contextEl = null, bool $registerNodeNs = false): ?Element
Expand Down

0 comments on commit 10047a8

Please sign in to comment.