Skip to content

Commit

Permalink
[REFACTOR] PHP 8 Syntax, Add Types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Oct 16, 2023
1 parent d67995b commit 23f02ff
Show file tree
Hide file tree
Showing 21 changed files with 174 additions and 353 deletions.
8 changes: 2 additions & 6 deletions src/FluentDOM/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ public function __get(string $name): mixed {
};
}

/**
* @param string $name
* @param mixed $value
*/
public function __set(string $name, $value) {
public function __set(string $name, mixed $value): void {
switch ($name) {
case 'formatOutput' :
$this->_document->{$name} = $value;
Expand All @@ -84,7 +80,7 @@ public function __set(string $name, $value) {
* If the creator is cloned, a clone of the dom document is needed, too.
*
*/
public function __clone() {
public function __clone(): void {
$this->_document = clone $this->_document;
}

Expand Down
10 changes: 5 additions & 5 deletions src/FluentDOM/Creator/Nodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class Nodes implements Appendable, \OuterIterator {

private array|\Traversable $_iterable;
private iterable $_iterable;

private ?\Closure $_map = NULL;

Expand All @@ -37,12 +37,12 @@ public function getInnerIterator(): \Iterator {
if (NULL === $this->_iterator) {
if ($this->_iterable instanceof \Iterator) {
$this->_iterator = $this->_iterable;
} elseif (\is_array($this->_iterable)) {
} elseif (is_array($this->_iterable)) {
$this->_iterator = new \ArrayIterator($this->_iterable);
} else if ($this->_iterable instanceof \Traversable) {
$this->_iterator = new \IteratorIterator($this->_iterable);
} else {
$this->_iterator = (NULL !== $this->_iterable)
? new \IteratorIterator($this->_iterable)
: new \EmptyIterator();
$this->_iterator = new \EmptyIterator();
}
}
return $this->_iterator;
Expand Down
126 changes: 53 additions & 73 deletions src/FluentDOM/DOM/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* FluentDOM
*
* @link https://thomas.weinert.info/FluentDOM/
* @copyright Copyright 2009-2021 FluentDOM Contributors
* @copyright Copyright 2009-2023 FluentDOM Contributors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*/
Expand Down Expand Up @@ -61,19 +61,15 @@ class Document extends \DOMDocument implements Node\ParentNode {
'DOMEntityReference' => EntityReference::class
];

/**
* @param string $version
* @param string $encoding
*/
public function __construct($version = '1.0', $encoding = 'UTF-8') {
public function __construct(string $version = '1.0', ?string $encoding = 'UTF-8') {
parent::__construct($version, $encoding ?: 'UTF-8');
foreach (self::$_classes as $superClass => $className) {
$this->registerNodeClass($superClass, $className);
}
$this->_namespaces = new Namespaces();
}

public function __clone() {
public function __clone(): void {
$this->_namespaces = clone $this->_namespaces;
}

Expand Down Expand Up @@ -117,11 +113,9 @@ public function registerNamespace(string $prefix, string $namespaceURI): void {
*
* If the argument is provided ALL namespaces will be replaced.
*
* @param array|\Traversable $namespaces
* @return Namespaces
* @throws \LogicException
*/
public function namespaces($namespaces = NULL): Namespaces {
public function namespaces(iterable $namespaces = NULL): Namespaces {
if (NULL !== $namespaces) {
$this->_namespaces->assign([]);
foreach($namespaces as $prefix => $namespaceURI) {
Expand All @@ -140,18 +134,18 @@ public function namespaces($namespaces = NULL): Namespaces {
* If $content is an array, the $content argument will be merged with the $attributes
* argument.
*
* @param string $qualifiedName
* @param string|array $content
* @param array|NULL $attributes
* @return Element
*@throws \LogicException
* @throws \LogicException|\DOMException
*/
public function createElement($qualifiedName, $content = NULL, array $attributes = NULL): Element {
[$prefix, $localName] = QualifiedName::split($qualifiedName);
public function createElement(
string $name,
string|array $value = NULL,
array $attributes = NULL
): Element {
[$prefix, $localName] = QualifiedName::split($name);
$namespaceURI = '';
if ($prefix !== FALSE) {
if (empty($prefix)) {
$qualifiedName = $localName;
$name = $localName;
} else {
if ($this->namespaces()->isReservedPrefix($prefix)) {
throw new \LogicException(
Expand All @@ -164,27 +158,25 @@ public function createElement($qualifiedName, $content = NULL, array $attributes
$namespaceURI = (string)$this->namespaces()->resolveNamespace('#default');
}
if ($namespaceURI !== '') {
$node = $this->createElementNS($namespaceURI, $qualifiedName);
$node = $this->createElementNS($namespaceURI, $name);
} elseif (isset($this->_namespaces['#default'])) {
$node = $this->createElementNS('', $qualifiedName);
$node = $this->createElementNS('', $name);
} else {
$node = parent::createElement($qualifiedName);
$node = parent::createElement($name);
}
$this->appendAttributes($node, $content, $attributes);
$this->appendContent($node, $content);
$this->appendAttributes($node, $value, $attributes);
$this->appendContent($node, $value);
return $node;
}

/**
* @param string $namespaceURI
* @param string $qualifiedName
* @param string|NULL $content
* @return Element
*/
public function createElementNS($namespaceURI, $qualifiedName, $content = NULL): Element {
public function createElementNS(
string|null $namespace,
string $qualifiedName,
string $value = NULL
): Element {
/** @var Element $node */
$node = parent::createElementNS($namespaceURI, $qualifiedName);
$this->appendContent($node, $content);
$node = parent::createElementNS($namespace, $qualifiedName);
$this->appendContent($node, $value);
return $node;
}

Expand All @@ -194,12 +186,9 @@ public function createElementNS($namespaceURI, $qualifiedName, $content = NULL):
*
* Allow to add a attribute value directly.
*
* @param string $name
* @param string|NULL $value
* @return Attribute
* @throws \LogicException
* @throws \LogicException|\DOMException
*/
public function createAttribute($name, $value = NULL): Attribute {
public function createAttribute(string $name, string $value = NULL): Attribute {
[$prefix] = QualifiedName::split($name);
if (empty($prefix)) {
$node = parent::createAttribute($name);
Expand All @@ -215,25 +204,24 @@ public function createAttribute($name, $value = NULL): Attribute {
/**
* Overload appendElement to add a text content and attributes directly.
*
* @param string $name
* @param string $content
* @param array|NULL $attributes
* @return Element
* @throws \LogicException
* @throws \LogicException|\DOMException
*/
public function appendElement(string $name, $content = '', array $attributes = NULL): Element {
public function appendElement(
string $name,
string|array $value = '',
array $attributes = NULL
): Element {
$this->appendChild(
$node = $this->createElement($name, $content, $attributes)
$node = $this->createElement($name, $value, $attributes)
);
return $node;
}

/**
* @param \DOMElement $node
* @param string|array|NULL $content
* @param array|NULL $attributes
*/
private function appendAttributes(\DOMElement $node, $content = NULL, array $attributes = NULL): void {
private function appendAttributes(
\DOMElement $node,
string|array $content = NULL,
array $attributes = NULL
): void {
if (\is_array($content)) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$attributes = (NULL === $attributes) ? $content : \array_merge($content, $attributes);
Expand All @@ -245,11 +233,7 @@ private function appendAttributes(\DOMElement $node, $content = NULL, array $att
}
}

/**
* @param \DOMElement $node
* @param string|array|NULL $content
*/
private function appendContent(\DOMElement $node, $content = NULL): void {
private function appendContent(\DOMElement $node, string|array $content = NULL): void {
if (!((empty($content) && !\is_numeric($content)) || \is_array($content) )) {
$node->appendChild($this->createTextNode((string)$content));
}
Expand All @@ -259,24 +243,26 @@ private function appendContent(\DOMElement $node, $content = NULL): void {
* Allow to save XML fragments, providing a node list
*
* Overloading saveXML() with a removed type hint triggers an E_STRICT error,
* so we the function needs a new name. :-(
*
* @param \DOMNode|\DOMNodeList|NULL $context
* @param int $options
* @return string
* so the function needs a new name. :-(
*/
public function toXml($context = NULL, int $options = 0): string {
public function saveXML(\DOMNode|\DOMNodeList $context = NULL, int $options = NULL): string {
if ($context instanceof \DOMNodeList) {
$result = '';
foreach ($context as $node) {
$result .= $this->saveXML($node, $options);
$result .= parent::saveXML($node, $options ?: 0);
}
return $result;
}
return $this->saveXML($context, $options);
return parent::saveXML($context, $options ?: 0);
}

/**
* @deprecated
*/
public function toXml(\DOMNode|\DOMNodeList $context = NULL, int $options = NULL): string {
return $this->saveXML($context, $options);
}
/**
* Allow to cast the document to string, returning the whole XML.
*
* @return string
Expand All @@ -291,20 +277,16 @@ public function __toString(): string {
* This is an alias for the extended saveHTML() method. Make it
* consistent with toXml()
*
* @param \DOMNode|\DOMNodeList|NULL $context
* @return string
* @deprecated
*/
public function toHtml($context = NULL): string {
public function toHtml(\DOMNode|\DOMNodeList $context = NULL): string {
return $this->saveHTML($context);
}

/**
* Allow to save HTML fragments, providing a node list
*
* @param \DOMNode|\DOMNodeList|NULL $context
* @return string
*/
public function saveHTML($context = NULL): string {
public function saveHTML(\DOMNode|\DOMNodeList $context = NULL): string {
if ($context instanceof \DOMDocumentFragment) {
$context = $context->childNodes;
}
Expand Down Expand Up @@ -336,11 +318,9 @@ public function saveHTML($context = NULL): string {
/**
* Allow getElementsByTagName to use the defined namespaces.
*
* @param string $qualifiedName
* @return \DOMNodeList
* @throws \LogicException
*/
public function getElementsByTagName($qualifiedName): \DOMNodeList {
public function getElementsByTagName(string $qualifiedName): \DOMNodeList {
list($prefix, $localName) = QualifiedName::split($qualifiedName);
$namespaceURI = (string)$this->namespaces()->resolveNamespace((string)$prefix);
if ($namespaceURI !== '') {
Expand Down
8 changes: 3 additions & 5 deletions src/FluentDOM/DOM/DocumentFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,11 @@ public function appendXml(string $data, iterable|\DOMElement $namespaces = NULL)
/**
* Append an child element
*
* @param string $name
* @param string $content
* @param array|NULL $attributes
* @return Element
* @throws \LogicException
*/
public function appendElement(string $name, $content = '', array $attributes = NULL): Element {
public function appendElement(
string $name, string|array $content = '', array $attributes = NULL
): Element {
$this->appendChild(
$node = $this->ownerDocument->createElement($name, $content, $attributes)
);
Expand Down
Loading

0 comments on commit 23f02ff

Please sign in to comment.